compile error cannot convert 'this' pointer

Joined
Feb 12, 2008
Messages
108
Reaction score
0
Hello everyone,


Which C++ rule do I break in the following code? Why there is compile error?

Code:
class Foo{
public:
	Foo()
	{
	}

	int foo1()
	{
		return 100;
	}

	const Foo getCurrent1()
	{
		return *this;
	}

	const Foo& getCurrent2()
	{
		return *this;
	}
};

int main()
{
	Foo f;
	f.getCurrent1().foo1(); // error C2662: 'Foo::foo1' : cannot convert 'this' pointer from 'const Foo' to 'Foo &'
	f.getCurrent2().foo1(); // error C2662: 'Foo::foo1' : cannot convert 'this' pointer from 'const Foo' to 'Foo &'

	return 0;
}


thanks in advance,
George
 

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

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top