By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,290 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

overloading

P: n/a
Let's assume I have a base class

class X
{
};

and the the following classes inheriting

class BX : public X
{
};

class AX : public X
{
};

and this class

class MyClass
{
void DoSomething( AX* );
void DoSomething( BX* );
}

and this code

X* = ...
MyClass c;
c.DoSomething(X)

Do I have to use a reinterpret cast to
make this work? Isn't this a runtime
issue?
--
Torsten

Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a


Torsten Curdt wrote:
Let's assume I have a base class

class X
{
};

and the the following classes inheriting

class BX : public X
{
};

class AX : public X
{
};

and this class

class MyClass
{
void DoSomething( AX* );
void DoSomething( BX* );
}

and this code

X* = ...
MyClass c;
c.DoSomething(X)

Do I have to use a reinterpret cast to
make this work? Isn't this a runtime
issue?


I guess you meant something like:

X* XX = ...
MyClass c;
c.DoSomething(XX);

You will get an error message that c.DoSomething( X*)
is not defined.
You need a dynamic_cast here.
Would be dangerous if it would be a runtime issue.
What should be done, if XX is actually of type X?

marc

Jul 19 '05 #2

P: n/a
>>Let's assume I have a base class

class X
{
};

and the the following classes inheriting

class BX : public X
{
};

class AX : public X
{
};

and this class

class MyClass
{
void DoSomething( AX* );
void DoSomething( BX* );
}

and this code

X* = ...
sure, I meant

X* x = ...
MyClass c;
c.DoSomething(X)
c.DoSomething(x)

Do I have to use a reinterpret cast to
make this work?

Or dynamic_cast, which I would prefer.


ok
Isn't this a runtime
issue?

No. X* and AX*/BX* are unrelated, since X has no direct link with
them. The contrary would be ok, since AX/BX inherit from 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 will not work and can only be resolved at runtime because
MyClass doesn't have a "DoSomething(CX*)", right?
Downcasts cannot be made implicitly, so static_cast won't work. If
you add a virtual dtor to X (which I hope it has in your code, else
What do you mean by "dtor"?
it's undefined behavior), you can dynamic_cast `x` to `AX` or `BX`.
Or you can simply do a reinterpret_cast.
Hm.. ok
But this is not a good idea. If you have two different functions
taking AX and BX as arguments, it means they probably do something
different (or else you could just have one taking a X*). Now, to what
type would you cast X to ?

I think this is a flawed design. What exactly are you attempting ?


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 :-/

Any other ideas?
--
Torsten

Jul 19 '05 #3

P: n/a
On Thu, 31 Jul 2003 18:30:57 +0200, Torsten Curdt
<tc***********@web.de> wrote:
Let's assume I have a base class

class X
{
};

and the the following classes inheriting

class BX : public X
{
};

class AX : public X
{
};

and this class

class MyClass
{
void DoSomething( AX* );
void DoSomething( BX* );
}

and this code

X* = ...
sure, I meant

X* x = ...
MyClass c;
c.DoSomething(X)
c.DoSomething(x)

Do I have to use a reinterpret cast to
make this work?

Or dynamic_cast, which I would prefer.


ok
Isn't this a runtime
issue?

No. X* and AX*/BX* are unrelated, since X has no direct link with
them. The contrary would be ok, since AX/BX inherit from 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?
Downcasts cannot be made implicitly, so static_cast won't work. If
you add a virtual dtor to X (which I hope it has in your code, else


What do you mean by "dtor"?


destructor, sorry.
But this is not a good idea. If you have two different functions
taking AX and BX as arguments, it means they probably do something
different (or else you could just have one taking a X*). Now, to what
type would you cast X to ?

I think this is a flawed design. What exactly are you attempting ?


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

Jul 19 '05 #4

P: n/a
>>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

Why is it not available?


I am using eVC++ 4.0 and there is no such setting.
The help states something about a compiler switch
which I *maybe* could add by hand. Haven't tried yet
though.

Anyway I would prefer to use a pattern.
I need the same serialization/deserialization
in other languages (namely php), too. I'd prefer
to use the same mechanism.
I thought about this

class ObjectFactory
{
static Object* NewInstance( DWORD guid );
DWORD GetGUID( AX* o );
DWORD GetGUID( BX* o );
DWORD GetGUID( CX* o );

Those three could also be 'static', no?


Yes
What in your deserialisation code controls what object is created?
Is there some kind of code or value that, when read from the stream,
tells you what object to create?
Exactly
Is GUID your "code"? If you read
a GUID from the stream, why do you need those GetGUID() functions?
They are for used on serialization.
How does 'NewInstance' use the 'GetGUID'? Does it? It seems that
'GetGUID' is only needed during serialisation part, not de-...


Exactly

On serialialization the "GetGUID" is used to get the right id into
the stream. On deserialization the id is taken as input for the
factory.
--
Torsten

Jul 19 '05 #5

P: n/a
> 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.
Hm... couldn't I tell the compiler that this should be resolved
at runtime?

<snip/>
Something like that ?

class X
{
public:
void serialize();
virtual ~X();

// pure virtual, child must implement it
void deserialize() = 0;
};


<snip/>

That doesn't help because the information of which
object needs to be created comes from the stream.
So I need a factory that knows about

Classname = GUID
AX = 1
BX = 2
CX = 3
Whateverclass = 4

So it can create the appropriate object on runtime.
The GUID -> class is done in the factory. But for
not messing up with the ids I also should ask the
factory for class -> GUID.

Even if I register the classes to the factory

Factory::Register("AX",1);
Factory::Register("BX",2);
Factory::Register("CX",3);

I need to get the textual representation of
the object on serialization.

AX* a = new AX();
a -?-> "AX"

Only way I see up to now is having a virtual
"GetClassname()" function in each object
returning the classname.
--
Torsten

Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.