View Single Post
Re: C++ overriding methods using virtual
Old
  (#5)
Cheeseh
[rcbot]
 
Cheeseh's Avatar
 
Status: Offline
Posts: 361
Join Date: Dec 2003
Location: China
Default Re: C++ overriding methods using virtual - 21-06-2004

lol!

Found my problem...

Anyone know how to call super class constructor WITHOUT messing up the bloody class???

Check this test program I made...

Code:
#include <stdio.h>

class CSuperClass
{
public:
	virtual void override_me ()
	{
		printf("WRONG METHOD CALLED...WTF???\n");
		return;
	}
};

class CSubClass : public CSuperClass
{
public:

	// mess_up is true, it will call the super classes constructor on me
	// it isn't very useful in this case, but is very useful in other cases
	CSubClass(bool mess_up)
	{
		if ( mess_up )
			this->CSuperClass::CSuperClass();
	}

	void override_me ()
	{
		printf("CORRECT METHOD CALLED...WOOOT!!!\n");
		return;
	}
};

int main ( int argc, char **argv )
{
	CSuperClass *pInstance1 = new CSubClass(false);
	CSuperClass *pInstance2_messed = new CSubClass(true);

	pInstance1->override_me();

	pInstance2_messed->override_me();

	delete pInstance1;
	delete pInstance2_messed;

	return 1;
}
You'll see that when you initialise when calling the super classes constructor messes up the class for some reason or another...

Now I need to make my own method to initialise instead of calling the super class constructor....

...
  
Reply With Quote