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 5 3007
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(X X);
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
>>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_cas t.
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
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 UnrelatedOtherC lass.
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 automagicall y - but I guess that's what RTTI is about :-/
...or at least the filename without the full path would be nice. Unfortunatel y __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
>>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 serialializatio n the "GetGUID" is used to get the right id into
the stream. On deserialization the id is taken as input for the
factory.
--
Torsten
> 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 UnrelatedOtherC lass.
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::Regist er("AX",1);
Factory::Regist er("BX",2);
Factory::Regist er("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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 manual mentions "overloading"
(http://no.php.net/manual/en/language.oop5.overloading.php), but it isn't
really overloading at all... Not in the sense it's used in other languages
supporting overloading (such as C++ and Java). As one of the...
|
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. Note that the overload is identical to the
specialization except, of course, for the missing "template <>".
I don't know if my questions will be a bit too broad or not, but I thought
I'd give it shot... When is overloading preferable to...
|
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 discussions of
new and delete is a pretty damn involved process but I'll try to stick to the main information I'm looking for
currently. I've searched around for about the last too weeks and have read up on new and overloading it to
some extent but...
|
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 C implementation supporting this feature?
I assume some of you will claim that there is no need in function
overloading, so I would like to know your arguments too.
Thanks,
|
by: JaSeong Ju |
last post by:
I would like to overload a C function. Is there any easy way to do this?
| |
by: |
last post by:
Hi,
Why can I not overload on just the return type?
Say for example.
public int blah(int x)
{
}
|
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 uses map<and
heavy overloading.
The problem is, the output file differs from input, and for the love of
me I can't figure out why ;p
#include <iostream>
#include <fstream>
#include <sstream>
|
by: lordkain |
last post by:
is it possible to do some kind of function overloading in c? and that
the return type is different
|
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
|
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
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |