The type of *this

G

George2

Hello everyone,


I am confused what is the type of *this? Type of reference to instance
or type of instance?

Looks like both code can compile, no warning messages. Any ideas?

Code:
class A{

	A& foo1()
	{
		return *this;
	}

	A foo2()
	{
		return *this;
	}
};


thanks in advance,
George
 
A

Abhishek Padmanabh

Hello everyone,

I am confused what is the type of *this? Type of reference to instance
or type of instance?

When you say type of reference - reference just an alias for an object
- so you can understand it to be the same as type of instance/object.
Looks like both code can compile, no warning messages. Any ideas?

Code:
class A{

A& foo1()
{
return *this;
}

A foo2()
{
return *this;
}};

Both compile because in one you return a reference to the object (no
copy of *this involved) and in the second case you are returning by
value. That is the difference. The type of *this is same as A.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top