Hi,
That's not how polymorphism works...
Code: Select all
user@localhost:/void/cygwin/osdev/zbz$ cat ~/tst.cpp
#include <stdlib.h>
#include <stdio.h>
class animal {
public:
virtual void foo(void) { printf("animal::foo()"); };
void bar(void) { printf("animal::bar()"); };
};
class bear
: public animal {
public:
void foo(void) { printf("bear::foo()"); };
void bar(void) { printf("bear::bar()"); };
};
/* The purpose of polymorphism (as done in C++) is to allow you to use a
* pointer or reference to a base class and assign it to an instance of a
* derived class, and even though the _type_ of the pointer being used may be
* of the base class, if the object being pointed to is a derived class with an
* implementation of one of the base class's virtual functions, the _derived_
* class object's implementation of that function will be called, in spite of
* type of the pointer being used to refer to the object. Consider:
**/
animal *pAnim;
bear bearArr[16];
int main(void)
{
for (int i=0; i<16; i++)
{
pAnim = static_cast<animal *>( &bearArr[i] );
pAnim->foo();
};
return EXIT_SUCCESS;
}
user@localhost:/void/cygwin/osdev/zbz$
user@localhost:/void/cygwin/osdev/zbz$ g++ ~/tst.cpp -o ~/tst
user@localhost:/void/cygwin/osdev/zbz$ ~/tst
bear::foo()bear::foo()bear::foo()bear::foo()bear::foo()bear::foo()bear::foo()bear::foo()bear::foo()bear::foo()bear::foo()bear::foo()bear::foo()bear::foo()bear::foo()bear::foo()user@localhost:/void/cygwin/osdev/zbz$
Notice how even though the pointer I dereferenced to call foo() was of type "animal", the bear:: implementation of foo() was called. The idea is so you can deal with a list of derived class objects all using one pointer to a base class object, and have all the objects invoke their own internal method, regardless of the pointer used to invoke. The key is that you're casting from a derived class to a base class; not the other way around. Your example:
Code: Select all
ClassA BaseClass; // ClassA only contains virtual functions
#ifdef _USE_CLASS_B
// Want to change ClassA to ClassB (ClassB : public ClassA)
#elif _USE_CLASS_C
// Want to change ClassA to ClassC (ClassC : public ClassA)
#endif
BaseClass.VirtualFunction(); // Call the function (BaseClass now is ClassB or ClassC, Can't be ClassA becuase all functions are virtual)
Takes a base class and tried to cast it to a derived class, and then use polymorphism to do something, though it's not clear what, so it's misdirected on two accounts: your attempt to polymorphism isn't really cogent, and you're casting from a base class to a derived class.
To cast from a base class to a derived class, what you need is "dynamic_cast<>()". Casting up from base to derived has the risk of the class memory space difference causing bugs. Dynamic_cast<> asserts that the two classes are the same size at runtime before casting, so it makes it "safe" to cast upwards. Of course, if dynamic_cast<> determines that there would be a problem casting upwards, it will return an error. Dynamic_cast also requires RTTI, which implies overhead. Bascally, you've probably designed somethign wrong somewhere.
So to answer your original question,
can i convert ClassA to ClassB (ClassB : public ClassA)? without using new, and RTTI (typecast)
No, you can't do it (properly) without RTTI. You can just reinterpret_cast<> and go your way, but whatever result that yields depends on how similar the base and derived classes are in size and layout, and whatever eccentricities your compiler may have in its handling of inheritance. Also, a common mistake people make is to assume that online "tutorials" on C/C++ are as good as a well known, well reputed book (like K&R or Stroustrup): they are not
--Have fun
gravaera