J
Jonathan Mcdougall
sure, I meant
X* x = ...
Not a runtime issue? ...but think of the following:
there is another class
class CX : public X
{
};
X* x = new CX();
MyClass c;
c.DoSomething(x)
This does not compiler neither. The _static_ type of 'x' is X* and
this cannot be converted implicitly to AX* or BX*. The _dynamic_ (or
runtime) type of 'x' is 'CX', but the compiler does not know that, the
runtime system does. So the compiler is only trying to convert X* to
AX* or BX* and it simply cannot. That's the same thing as converting
a MyClass to a UnrelatedOtherClass.
this will not work and can only be resolved at runtime because
MyClass doesn't have a "DoSomething(CX*)", right?
What do you mean by "dtor"?
destructor, sorry.
I have a list of object of type X. X is an abstract class so the objects
in this list are actually of type AX, BX or CX. Now I am trying to
serialize and deserialize them.
Serialize - no problem. X has a "Serialize" function and we are done.
But for deserialization I need to check what exact type of object I
need to create. Since RTTI is not available I thought about this
class ObjectFactory
{
static Object* NewInstance( DWORD guid );
DWORD GetGUID( AX* o );
DWORD GetGUID( BX* o );
DWORD GetGUID( CX* o );
}
So the overloading would help me to keep the GUID mapping in one
class. Having the "GetGUID" in each class would be a mess since
I need to assign them by hand :-/
What I really would need is to have the classname in each class
automagically - but I guess that's what RTTI is about :-/
...or at least the filename without the full path would be nice.
Unfortunately __FILE__ gives me the full path :-/
Something like that ?
class X
{
public:
void serialize();
virtual ~X();
// pure virtual, child must implement it
void deserialize() = 0;
};
class AX : public X
{
public:
void deserialize()
{
/* .. */
}
};
class BX : public X
{
public:
void deserialize()
{
/* .. */
}
};
int main()
{
X *x = new BX;
// calls X::serialize
// call resolved at compile time
x->serialize();
// calls BX::deserialize()
// call resolved at runtime
x->deserialize();
}
Jonathan