473,511 Members | 16,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2979


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
4694
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
6448
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
5213
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
2192
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
3217
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
31
2236
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
2
2233
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
2736
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
28108
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
3470
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
7242
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
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7353
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7418
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5662
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,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
446
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.