473,396 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

overloading

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
5 2965


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
>>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
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
>>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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Terje Slettebø | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
39
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any...
45
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
31
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
15
by: lordkain | last post by:
is it possible to do some kind of function overloading in c? and that the return type is different
11
by: placid | last post by:
Hi all, Is it possible to be able to do the following in Python? class Test: def __init__(self): pass def puts(self, str): print str
10
by: Matthew | last post by:
Am I correct in thinking there is no method/function overloading of any kind in any version of PHP? Thanks, Matthew
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.