Advertisement Jump to content
Sign in to follow this  
Wavarian

Strange phenomenon with const methods

This topic is 1839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Anyone got an explanation for this oddity?  I could understand if both examples produced the same output, but this surprised me.

//
// Main.cpp
//



//
// Includes
//

#include <iostream>



//
// ExampleA structure definition
//

struct ExampleA
{
	operator const char*() const
	{
		return "ExampleA: const method invoked";
	}

	operator char*()
	{
		static char BUFFER[] = "ExampleA: non-const method invoked";

		return BUFFER;
	}
};

//
// ExampleB structure definition
//

struct ExampleB_Part1
{
	operator const char*() const
	{
		return "ExampleB: const method invoked";
	}

	virtual ~ExampleB_Part1() = 0 {}
};

struct ExampleB_Part2
{
	operator char*()
	{
		static char BUFFER[] = "ExampleB: non-const method invoked";

		return BUFFER;
	}

	virtual ~ExampleB_Part2() = 0 {}
};

struct ExampleB : public ExampleB_Part1, public ExampleB_Part2
{
	virtual ~ExampleB() {}
};



//=========================================================================
//
// DESCRIPTION
//
//		Test area
//
// PARAMETERS
//
// RETURNS
//
//=========================================================================

void Test(const char* str)
{
	std::cout << str << "\n";
}



//=========================================================================
//
// DESCRIPTION
//
//		Application entry point
//
// PARAMETERS
//
// RETURNS
//
//=========================================================================

int main()
{
	ExampleA a;
	ExampleB b;

	Test(a);			// ExampleA: non-const method invoked
	Test((const char*)a);		// ExampleA: non-const method invoked
	Test((char*)a);			// ExampleA: non-const method invoked
	Test((const ExampleA&)a);	// ExampleA: const method invoked

	std::cout << "\n";

	Test(b);			// ExampleB: const method invoked
	Test((const char*)b);		// ExampleB: const method invoked
	Test((char*)b);			// ExampleB: non-const method invoked
	Test((const ExampleB&)b);	// ExampleB: const method invoked

	std::cout << "\n";
	return 0;
}

Compiled using Microsoft Visual Studio 2008 (Version 9.0.21022.8 RTM)

 

EDIT: been doing some research on Google, and apparently the compiler is allowed to resolve for whichever overloaded operator it likes, regardless of the context (except in the case where the object itself is const). Resolution based on const has been ill-advised:

 

http://accu.org/index.php/journals/605

 

Another weird example:

 

http://stackoverflow.com/questions/6239630/trouble-with-const-non-const-overload-resolution

Edited by Wavarian

Share this post


Link to post
Share on other sites
Advertisement
When the compiler has the choice between a member function and an equivalent const member function the decision always goes according to the const-ness of the this pointer. Your second example muddies the waters by introducing the scourge of multiple inheritance.

I'd also strongly advise not to implement conversion operators like that. Experience has shown there are very good reasons for std::string having c_str instead of a conversion operator.

Edit: The first article you linked is from 1995, C++ was not standarized until 1998. Edited by BitMaster

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!