473,322 Members | 1,480 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,322 software developers and data experts.

Which cast in mutiple inheritance?

In the following example, I want that ABC::f() be called. Is it possible?
Here I have simplified the problem, but the point is that I have to
store the pointer on the ABC object in a pointer on a A object (line [1]).
and when I use it (line [2]), I know that pp points on a AC object but
not on a ABC object.

I have tried many casts (static_cast, dynamic_cast, even
reinterpret_cast) but I do not success.

Is there a pure C++ solution, and else is there a MS VC6++ solution?

Thanks in advance

Pierre Couderc
class A
{
public:
A(){ ;}
virtual ~A(){;}
int dummy;
};
class AB : public virtual A
{
public:
AB() {;}
};
class AC : public virtual A
{
public:
AC() {;}
virtual void f(){;}
};
class ABC : public AB, public AC
{
public:
~ABC(){;}
void f(){println();}
};
int main(int argc, char* argv[])
{
ABC* p= new ABC;
A* pp=p; //[1]
AC* ppp= ????_cast<AC*> (pp); // [2]
ppp->f();

return 0;
}
Jul 22 '05 #1
4 1095
"Pierre Couderc" <pi****@couderc.ccNOSPAM> wrote in message
news:ch***********@biggoron.nerim.net...
In the following example, I want that ABC::f() be called. Is it possible?
Here I have simplified the problem, but the point is that I have to store
the pointer on the ABC object in a pointer on a A object (line [1]).
and when I use it (line [2]), I know that pp points on a AC object but not
on a ABC object.

I have tried many casts (static_cast, dynamic_cast, even reinterpret_cast)
but I do not success.

Is there a pure C++ solution, and else is there a MS VC6++ solution?
Only essential code preserved below: class A
{
public:
virtual ~A(){;}
}; .... class AB : public virtual A .... class AC : public virtual A
{
public:
virtual void f(){;}
}; .... class ABC : public AB, public AC
{
public:
~ABC(){;}
void f(){println();}
};
int main(int argc, char* argv[])
{
ABC* p= new ABC;
A* pp=p; //[1]
AC* ppp= ????_cast<AC*> (pp); // [2]
Unless I missed something, dynamic_cast should work
here. In what way did this cast fail?
ppp->f();

-> ok, shall call ABC::f();

Salutations-Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 22 '05 #2
One joyful day (Tue, 07 Sep 2004 10:59:57 +0200 to be precise), Pierre
Couderc <pi****@couderc.ccNOSPAM> decided that the Usenet community
would benefit from this remarkable comment:
In the following example, I want that ABC::f() be called. Is it possible?
Here I have simplified the problem, but the point is that I have to
store the pointer on the ABC object in a pointer on a A object (line [1]).
and when I use it (line [2]), I know that pp points on a AC object but
not on a ABC object.

I have tried many casts (static_cast, dynamic_cast, even
reinterpret_cast) but I do not success.

Is there a pure C++ solution, and else is there a MS VC6++ solution?

<...>

Please define success. Other than the println() it compiles fine on VC++
6 using dynamic_cast<AC *>. Do you have RTTI enabled in the project
options?

Also, running the code shows that the call to f() does indeed call
ABC::f() as it should.

Mark Wright
- ma**@giallo.demon.nl

================Today's Thought====================
"In places where books are burned, one day,
people will be burned" - Heinrich Heine, Germany -
100 years later, Hitler proved him right
================================================== =
Jul 22 '05 #3

That was the problem. RTTI was not enabled.
Mmm, I am glad that my problem was a MSVC problem and not a pure C++
problem...

Thank you all.
Pierre Couderc

Mark Wright wrote:
One joyful day (Tue, 07 Sep 2004 10:59:57 +0200 to be precise), Pierre
Couderc <pi****@couderc.ccNOSPAM> decided that the Usenet community
would benefit from this remarkable comment:

In the following example, I want that ABC::f() be called. Is it possible?
Here I have simplified the problem, but the point is that I have to
store the pointer on the ABC object in a pointer on a A object (line [1]).
and when I use it (line [2]), I know that pp points on a AC object but
not on a ABC object.

I have tried many casts (static_cast, dynamic_cast, even
reinterpret_cast) but I do not success.

Is there a pure C++ solution, and else is there a MS VC6++ solution?


<...>

Please define success. Other than the println() it compiles fine on VC++
6 using dynamic_cast<AC *>. Do you have RTTI enabled in the project
options?

Also, running the code shows that the call to f() does indeed call
ABC::f() as it should.

Mark Wright
- ma**@giallo.demon.nl

================Today's Thought====================
"In places where books are burned, one day,
people will be burned" - Heinrich Heine, Germany -
100 years later, Hitler proved him right
================================================== =

Jul 22 '05 #4
On Tue, 07 Sep 2004 10:59:57 +0200, Pierre Couderc
<pi****@couderc.ccNOSPAM> wrote:
In the following example, I want that ABC::f() be called. Is it possible?
Here I have simplified the problem, but the point is that I have to
store the pointer on the ABC object in a pointer on a A object (line [1]).
and when I use it (line [2]), I know that pp points on a AC object but
not on a ABC object.

I have tried many casts (static_cast, dynamic_cast, even
reinterpret_cast) but I do not success.
dynamic_cast is the correct cast to use when casting down a virtual
heirarchy.
Is there a pure C++ solution, and else is there a MS VC6++ solution?


Have you got RTTI enabled? If so, it should work as is.

Tom
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: AriZOnA | last post by:
Hi! I have the following scenario: class QWidget {} class GeneratedClass : public QWidget {} class AbstractPanel
2
by: sudhirlko2001 | last post by:
Can anybody explain me the side effects of Mutiple inheritance in C++; ~Sudhir
4
by: hazz | last post by:
I have inherited code but don't know exactly what this one line of code is doing? What it is the purpose of (ClassFileName)ClassFileName......... > ClassFileName vs2 = (ClassFileName...
0
by: deepak | last post by:
i have set multiple selection property in bith listboxes(html control) to true. i have taken 2 buttons(html control) say Button1,Button2.now i want to add mutiple selected items to another listbox...
14
by: Craig Buchanan | last post by:
If I have two custom vb.net classes, where 80% of the properties are alike and there is one method with a matching signature, can i cast between one and the other? do i need to have each class...
13
by: Just Me | last post by:
I have two forms. Each contains a property, say Prop. I do Public FormBeingUsed as Form .. .. .. Then I do FormBeingUsed= Form1 or maybe
4
by: Egbert Nierop \(MVP for IIS\) | last post by:
Hi, I have a CWindowImpl derived class , see below at ***, that needs to subclass simple controls like textboxes. like this...: m_MyControl.SubclassWindow(GetDlgItem(IDC_MyControl).m_hWnd); ...
11
by: axel22 | last post by:
Please observe this simple model of multiple inheritance: void main() { class A { public: virtual void print() { cout << "A" << endl; }; class Support1 : virtual public A {
0
by: saravana | last post by:
I have to display the mutiple xml filename path in treeview control using c#. net some thing like this In treeview control....... ========= =C:\\a.xml=
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.