A few questions regarding classes
I ran into two peculiar issues in a program I've been writing regarding class usage. Rather than posting pages upon pages of code, here's a few simplified examples to illustrate the issues:
Code:
class foo
{
public:
foo()
{
//Do initialization stuff...
}
foo(bool bar)
{
if(bar)
foo(); //Problem here....
}
};
The problem is that when the version of foo() that takes an argument calls the version that doesn't, the "this" pointer in that function is invalid. I know an easy workaround would be having an extra DoInitialization() member that both versions of the constuctor could call, but I'm still curious as to why the "this" pointer is getting messed up.
Next issue:
Code:
class foo
{
public:
foo() {}
DoStuff()
{
//Stuff getting done...
}
};
class foobar : public foo
{
public:
foobar() {}
DoStuff()
{
//Different stuff getting done...
}
};
DoFoobarStuff(foo& foobar)
{
//When you pass this function a class of the foobar type typecast as a foo type, the foobar version
//of DoStuff() should get called
foobar.DoStuff();
//However, no matter which type is passed, only the foo version of DoStuff() is ever used
}
The question is, how do you make sure the appropriate DoStuff() function gets called corresponding to the actual class that get's passed as an argument, and not the type as percieved by the function that calls DoStuff()? Keep in mind that dynamically determining the actual class type using RTI and casting it to that type first is not an option for me with my current code structure.