return *this

what's the use of *this ? i tried to do:

 
cout << *this;

but it doesn't print the value of the this, so what value returned by this statement:
 
return *this;
Last edited on
Works for me:

In this project I made a while ago I have a class called logger. I then return a logger& as the type:

1
2
3
4
5
6
7
8
9
logger& logger::operator()(unsigned mask_) 
{
	mask = mask_;
	nextline = true;

	if (mask & mask::ConsoleOut)	
		setColour();
	return *this;
}


Give me an example of how it doesn't work...
Last edited on
Oh, and if your overloading a << operator for a class with cout (or any ostream object) on the left, you'll want to return the ostream object, not this. That will let you chain more commands in a row.

In your class you'd declare:
friend ostream& operator<<(ostream& o, const MyClass& c);

Then outside of the class you'd define:
1
2
3
4
5
ostream& operator<<(ostream& o, const MyClass& c)
{
    o << c.member1 << c.member2;
    return o;
}
Last edited on
Here's another example of a class that simultaneously writes to a file and console. This one uses the << overloading, has the custom class on the left, and uses *this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class MyClass
{
	std::ofstream fout;
public:
	MyClass(const char* filename) 
	{ 
		fout.open(filename);
	}
	
	template<class T>
	MyClass& operator<<(T message)
	{
		std::cout << message;
		fout << message;
		return *this;
	}
	
	MyClass& operator<<(std::ostream&  (*manip)(std::ostream &))	
	{
		manip(std::cout); // Overloaded for output manipulators
		manip(std::fout); // std::endl, std::flush, std::ends
		return *this;
	}

	MyClass& operator<<(std::ios_base& (*manip)(std::ios_base&))	
	{
		manip(std::cout); // Overloaded for Basic format flags  
		manip(std::fout); // std::hex, std::scientific, std::fixed, etc...
		return *this;
	}

	template <class Arg>
	MyClass& operator<<(std::_Smanip<Arg> Smanip)	
	{
		(*Smanip._Pfun)(std::cout, Smanip._Manarg); // Overloaded for parameterized manipulators
		(*Smanip._Pfun)(fout     , Smanip._Manarg); //setw(), setprecision(), setbase(), setiosflags(), resetiosflags()
		return *this;
	}

	template <class Arg>
	MyClass& operator<<(std::_Fillobj<Arg> Fillobj)	
	{
		std::cout.fill(Fillobj._Fill); // Overloaded for parameterized manipulator 
		fout.fill(Fillobj._Fill);      // std::setfill()
		return *this;
	}
};


You can test it with this:
1
2
3
4
5
6
7
int main()
{
	MyClass log("output.txt");

	log << "Hello" << std::endl;
	log << std::setw(8) << std::setfill('0') << std::hex << 41 << std::endl;
}
Last edited on
what's the use of *this ? i tried to do:


cout << *this;



but it doesn't print the value of the this, so what value returned by this statement:

return *this;


this is pointer to a class object. And *this is the object itself. For example

1
2
3
4
5
6
7
8
9
10
struct A
{
   A( int i = 0 ) : x( i ) {}
   A & operator ++() { ++x; return ( *this ); }
   int x;
};

A a;
++a;
a.operator ++();


In the code above two statements ++a; and a.operator ++(); are equivalent. The both return the object itself by reference.
And *this is the object itself

so it doesn't point to its value, and we have to decided whether it will be returned by reference or by value? and this:

 
cout << *this;


doesn't show the value of it? instead, it points to the object, which is cannot be "cout"-ed? am i right?

CMIIW
Last edited on
this is a pointer to the object. *this is the object itself. You weren't wrong here.

If you have
1
2
3
4
class MyClass 
{
// ...
};

Then you do this:
1
2
MyClass a;
cout << a;

You will have problems because the cout has no idea what to do with this class. A class doesn't really have a value, it could contain hundreds of different containers or maybe none! Knowing which ones to output is not cout's job.

Doing: cout << *this is exactly the same thing. If you haven't manually defined the behavior of sticking your class into a cout, then you're not going to be able to compile. To define the behavior of sticking your class into a cout see this:
http://cplusplus.com/forum/beginner/64156/#msg347038

tl;dr
Your problem is not with *this, it's that you haven't overloaded the << operator to accept
ostream<<YourClass.
Last edited on
oh yeah... right... thanks stew... i totally didn't notice that part... thanks again man... i forgot that we cout a class, that's why the cout cannot determine which value of a class should be cout-ed...
Last edited on
Topic archived. No new replies allowed.