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;
} 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;
}
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? 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;
}
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. 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;
} 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 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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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>
|
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
|
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...
|
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...
| |
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;
|
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
|
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.
|
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...
|
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: 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...
|
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...
|
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: 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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |