473,657 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why dynamic_cast fail here?

In the example below, why does the dynamic_cast fail (return NULL)? It
should be able to cast between sibling classes ...
#include <iostream>

class A
{
public:
virtual const int get() const = 0;
};

class B : public A
{
public:
virtual const int get() const { return 0; }
};

class C : public A
{
public:
virtual const int get() const { return 1; }
};

int main()
{
A *a;

a = new B();
std::cout << a->get() << std::endl;

a = dynamic_cast<C* >(a);
if(a)
std::cout << a->get() << std::endl;

return 0;
}

Mar 15 '07 #1
8 5426
pi*******@gmail .com wrote:
In the example below, why does the dynamic_cast fail (return NULL)?
Because a B is not a C.
It should be able to cast between sibling classes ...
No, it shouldn't. An instance of class B only provides the A interface and
the B interface. So why would you want to access it through the C
interface? What would you expect to happen if C had another member function
and you'd try to call that on your B?
#include <iostream>

class A
{
public:
virtual const int get() const = 0;
};

class B : public A
{
public:
virtual const int get() const { return 0; }
};

class C : public A
{
public:
virtual const int get() const { return 1; }
};

int main()
{
A *a;

a = new B();
std::cout << a->get() << std::endl;

a = dynamic_cast<C* >(a);
if(a)
std::cout << a->get() << std::endl;

return 0;
}
Mar 15 '07 #2
On Mar 15, 12:12 pm, Rolf Magnus <ramag...@t-online.dewrote:
pietro...@gmail .com wrote:
In the example below, why does the dynamic_cast fail (return NULL)?

Because a B is not a C.
It should be able to cast between sibling classes ...

No, it shouldn't. An instance of class B only provides the A interface and
the B interface. So why would you want to access it through the C
interface? What would you expect to happen if C had another member function
and you'd try to call that on your B?
#include <iostream>
class A
{
public:
virtual const int get() const = 0;
};
class B : public A
{
public:
virtual const int get() const { return 0; }
};
class C : public A
{
public:
virtual const int get() const { return 1; }
};
int main()
{
A *a;
a = new B();
std::cout << a->get() << std::endl;
a = dynamic_cast<C* >(a);
if(a)
std::cout << a->get() << std::endl;
return 0;
}
Yes, I see how that could case problems. So does that mean that the
following is not true (http://www.acm.org/crossroads/xrds3-1/
ovp3-1.html):

"The dynamic_cast Operator

The dynamic_cast operator takes the form

dynamic_cast<T( expr)

and can be used only for pointer or reference types to navigate a
class hierarchy. The dynamic_cast operator can be used to cast from a
derived class pointer to a base class pointer, cast a derived class
pointer to another derived (sibling) class pointer, or cast a base
class pointer to a derived class pointer. Each of these conversions
may also be applied to references. In addition, any pointer may also
be cast to a void*."

Or else, what are the conditions for casting between sibling classes?

Mar 15 '07 #3
pi*******@gmail .com wrote:
On Mar 15, 12:12 pm, Rolf Magnus <ramag...@t-online.dewrote:
>pietro...@gmai l.com wrote:
In the example below, why does the dynamic_cast fail (return NULL)?

Because a B is not a C.
It should be able to cast between sibling classes ...

No, it shouldn't. An instance of class B only provides the A interface
and the B interface. So why would you want to access it through the C
interface? What would you expect to happen if C had another member
function and you'd try to call that on your B?
#include <iostream>
class A
{
public:
virtual const int get() const = 0;
};
class B : public A
{
public:
virtual const int get() const { return 0; }
};
class C : public A
{
public:
virtual const int get() const { return 1; }
};
int main()
{
A *a;
a = new B();
std::cout << a->get() << std::endl;
a = dynamic_cast<C* >(a);
if(a)
std::cout << a->get() << std::endl;
return 0;
}

Yes, I see how that could case problems. So does that mean that the
following is not true (http://www.acm.org/crossroads/xrds3-1/
ovp3-1.html):

"The dynamic_cast Operator

The dynamic_cast operator takes the form

dynamic_cast<T( expr)

and can be used only for pointer or reference types to navigate a
class hierarchy. The dynamic_cast operator can be used to cast from a
derived class pointer to a base class pointer, cast a derived class
pointer to another derived (sibling) class pointer, or cast a base
class pointer to a derived class pointer. Each of these conversions
may also be applied to references. In addition, any pointer may also
be cast to a void*."

Or else, what are the conditions for casting between sibling classes?
I think it means just that those are the cases where you may use a
dynamic_cast. It might fail (returning a null pointer or throwing an
exception), but you're allowed to use it.
And there is also multiple inheritance:

class A
{
public:
virtual ~A() {}
};

class B
{
public:
virtual ~B() {}
};

class C: public A, public B
{
};

int main()
{
A* a = new C;
B* b = dynamic_cast<B* >(a); // shoulnd't fail
delete b;
}

Mar 15 '07 #4
On Mar 15, 12:37 pm, Rolf Magnus <ramag...@t-online.dewrote:
pietro...@gmail .com wrote:
On Mar 15, 12:12 pm, Rolf Magnus <ramag...@t-online.dewrote:
pietro...@gmail .com wrote:
In the example below, why does the dynamic_cast fail (return NULL)?
Because a B is not a C.
It should be able to cast between sibling classes ...
No, it shouldn't. An instance of class B only provides the A interface
and the B interface. So why would you want to access it through the C
interface? What would you expect to happen if C had another member
function and you'd try to call that on your B?
#include <iostream>
class A
{
public:
virtual const int get() const = 0;
};
class B : public A
{
public:
virtual const int get() const { return 0; }
};
class C : public A
{
public:
virtual const int get() const { return 1; }
};
int main()
{
A *a;
a = new B();
std::cout << a->get() << std::endl;
a = dynamic_cast<C* >(a);
if(a)
std::cout << a->get() << std::endl;
return 0;
}
Yes, I see how that could case problems. So does that mean that the
following is not true (http://www.acm.org/crossroads/xrds3-1/
ovp3-1.html):
"The dynamic_cast Operator
The dynamic_cast operator takes the form
dynamic_cast<T> (expr)
and can be used only for pointer or reference types to navigate a
class hierarchy. The dynamic_cast operator can be used to cast from a
derived class pointer to a base class pointer, cast a derived class
pointer to another derived (sibling) class pointer, or cast a base
class pointer to a derived class pointer. Each of these conversions
may also be applied to references. In addition, any pointer may also
be cast to a void*."
Or else, what are the conditions for casting between sibling classes?

I think it means just that those are the cases where you may use a
dynamic_cast. It might fail (returning a null pointer or throwing an
exception), but you're allowed to use it.
And there is also multiple inheritance:

class A
{
public:
virtual ~A() {}

};

class B
{
public:
virtual ~B() {}

};

class C: public A, public B
{

};

int main()
{
A* a = new C;
B* b = dynamic_cast<B* >(a); // shoulnd't fail
delete b;

}
Okay, thanks for the quick help.

Mar 15 '07 #5
pi*******@gmail .com wrote:
On Mar 15, 12:12 pm, Rolf Magnus <ramag...@t-online.dewrote:
>pietro...@gmai l.com wrote:
In the example below, why does the dynamic_cast fail (return NULL)?

Because a B is not a C.
It should be able to cast between sibling classes ...

No, it shouldn't. An instance of class B only provides the A interface
and the B interface. So why would you want to access it through the C
interface? What would you expect to happen if C had another member
function and you'd try to call that on your B?
#include <iostream>
class A
{
public:
virtual const int get() const = 0;
};
class B : public A
{
public:
virtual const int get() const { return 0; }
};
class C : public A
{
public:
virtual const int get() const { return 1; }
};
int main()
{
A *a;
a = new B();
std::cout << a->get() << std::endl;
a = dynamic_cast<C* >(a);
if(a)
std::cout << a->get() << std::endl;
return 0;
}

Yes, I see how that could case problems. So does that mean that the
following is not true (http://www.acm.org/crossroads/xrds3-1/
ovp3-1.html):

"The dynamic_cast Operator

The dynamic_cast operator takes the form

dynamic_cast<T( expr)

and can be used only for pointer or reference types to navigate a
class hierarchy. The dynamic_cast operator can be used to cast from a
derived class pointer to a base class pointer, cast a derived class
pointer to another derived (sibling) class pointer, or cast a base
class pointer to a derived class pointer. Each of these conversions
may also be applied to references. In addition, any pointer may also
be cast to a void*."

Or else, what are the conditions for casting between sibling classes?
I think it means just that those are the cases where you may use a
dynamic_cast. It might fail (returning a null pointer or throwing an
exception), but you're allowed to use it.
And there is also multiple inheritance:

class Base
{
public:
virtual ~Base() {}
};

class A : public Base
{
};

class B : public Base
{
};

class C: public A, public B
{
};

int main()
{
A* a = new C;
B* b = dynamic_cast<B* >(a); // shoulnd't fail
delete b;
}

Mar 15 '07 #6
pi*******@gmail .com wrote:
In the example below, why does the dynamic_cast fail (return NULL)? It
should be able to cast between sibling classes ...
No, it should not and is not. See [5.2.7] for the conversions supported by
dynamic_cast<>. Specifically [5.2.7/8]

The run-time check logically executes as follows:
? If, in the most derived object pointed (referred) to by v, v points
(refers) to a public base class subobject of a T object, and if only one
object of type T is derived from the sub-object pointed (referred) to by
v, the result is a pointer (an lvalue referring) to that T object.
? Otherwise, if v points (refers) to a public base class sub-object of the
most derived object, and the type of the most derived object has a base
class, of type T, that is unambiguous and public, the result is a
pointer (an lvalue referring) to the T sub-object of the most derived
object.
? Otherwise, the run-time check fails.

#include <iostream>

class A
{
public:
virtual const int get() const = 0;
};

class B : public A
{
public:
virtual const int get() const { return 0; }
};

class C : public A
{
public:
virtual const int get() const { return 1; }
};

int main()
{
A *a;

a = new B();
std::cout << a->get() << std::endl;

a = dynamic_cast<C* >(a);
The dynamic type of *a is B. The dynamic_cast<is specifically designed to
catch the error in trying to regard this object as anything that it not
truly is. Therefore, dynamic_cast<wi ll fail if you cast the object to any
type that is not a base class of its dynamic type.

if(a)
std::cout << a->get() << std::endl;

return 0;
}

Best

Kai-Uwe Bux
Mar 15 '07 #7
pi*******@gmail .com wrote:
Okay, thanks for the quick help.
Damn, I wrote a supersede for my posting in the hope that it hadn't already
spread to too many usenet servers, but you were too quick :-)
I guess you didn't notice that in my example, the two classes aren't
actually siblings. See my other posting for a better example.

Mar 15 '07 #8
On Mar 15, 8:51 pm, pietro...@gmail .com wrote:
In the example below, why does the dynamic_cast fail (return NULL)? It
should be able to cast between sibling classes ...

#include <iostream>

class A
{
public:
virtual const int get() const = 0;

};

class B : public A
{
public:
virtual const int get() const { return 0; }

};

class C : public A
{
public:
virtual const int get() const { return 1; }

};

int main()
{
A *a;

a = new B();
std::cout << a->get() << std::endl;

a = dynamic_cast<C* >(a);
if(a)
std::cout << a->get() << std::endl;

return 0;}
a points an object which is type of B and you are trying to convert it
to C* it should not work.

Mar 15 '07 #9

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

Similar topics

13
4960
by: GianGuz | last post by:
Everyone knows about the complex and cpu-expensive procedures taken by dynamic_cast to find the right function call in a virtual classes hierarchy. The question I would to rise is when dynamic_cast is really necessary? A wise usage of templates and/or non dynamic_cast operators can grant the compiler with any information it needs to understand at compile time what is the right method to call. In the following example I tested the usage of...
8
2591
by: Thomas Lorenz | last post by:
Hello, first, I didn't find any reference to my question through googling. dynamic_cast uses RTTI to determine if the cast expression is valid. Invalid casts of pointers give a '0'-result. As it is the case in the following example: ----------------------------------------------------------- #include <iostream>
3
1990
by: Axter | last post by:
I'm wondering about the practical use of dynamic_cast in reusable or generic code. I'm currently working on a smart pointer that can be used on vector and other STL containers. See following link: http://code.axter.com/arr_ptr.h In above code, there's a function called clone_func_ptr_interface within the func_ptr_holder class. In this function, I need to cast from a base pointer to a derived
5
2030
by: tthunder | last post by:
Hi @all, Perhaps some of you know my problem, but I had to start a new thread. The old one started to become very very confusing. Here clean code (which compiles well with my BCB 6.0 compiler). You can find a problem there, which cannot be solved by dynamic_cast, because the pointers can be NULL, and I only want to know, if the pointer type is derived from another pointer type. BTW: I cannot create a temporary instance, because used...
22
4798
by: Boris | last post by:
I'm porting code from Windows to UNIX and ran into a problem with dynamic_cast. Imagine a class hierarchy with three levels: class Level2 derives from Level1 which derives from Base. If you look now at this code: Base *b = new Level2(); Level1 *l1 = dynamic_cast<Level1*>(b); Should dynamic_cast return a valid pointer or 0? I wonder as Visual Studio 2005 returns a valid pointer while g++ 3.4.6 returns 0. Both compilers work as expected...
15
2815
by: Grizlyk | last post by:
Hello. Returning to question of manual class type identification, tell me, for ordinary inheritance is C++ garantee that dynamic_cast<Derived*>(Base*) can be implemented similarly to return (Base*->type_fild >= Derived_typeid)? Base*: 0;
25
3127
by: lovecreatesbea... | last post by:
Suppose I have the following three classes, GrandBase <-- Base <-- Child <-- GrandChild The following cast expression holds true only if pBase points object of type of ``Child'' or ``GrandChild'', i.e. types not upper than Child in the above class hierarchy, dynamic_cast<Child*>pBase
18
5276
by: Eric | last post by:
Ok...this seems to be treading into some really esoteric areas of c++. I will do my best to explain this issue, but I don't fully understand what is happening myself. I am hoping that the problem comes down to standard c++ stuff and is not specific to Mac OS X compiler&linker. I have put together a simple test project which can be found at: http://ericgorr.net/LibraryLoading.zip which demonstrates the problem.
0
8420
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
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,...
0
8842
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, 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...
0
8740
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8617
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5642
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();...
0
4173
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...
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.