Pointer to class equals pointer to base class

A

Alberto Luaces

Hi,

can I always rely on the behaviour shown in the code? (A pointer to a class
is always the same as the pointer obtained from its base class with RTTI)

#include <iostream>

class A{};
class B : public A {};

int main(){
B b;

std::cout << (&b == dynamic_cast<A*>(&b)) << "\n"; // Always true?
return 0;
}

Thank you,

Alberto
 
R

Rolf Magnus

Alberto said:
Hi,

can I always rely on the behaviour shown in the code? (A pointer to a
class is always the same as the pointer obtained from its base class with
RTTI)

They don't need to point to the same address, but they must compare equal.
Btw: That doesn't have anything to do with RTTI. The dynamic_cast will only
add a runtime type check. If you replace it with a static_cast in your
program, the result will be the same.
 
M

Markus Schoder

Alberto said:
Hi,

can I always rely on the behaviour shown in the code? (A pointer to a class
is always the same as the pointer obtained from its base class with RTTI)

#include <iostream>

class A{};
class B : public A {};

int main(){
B b;

std::cout << (&b == dynamic_cast<A*>(&b)) << "\n"; // Always true?
return 0;
}

This is always true (if the base class is not unique it will fail to
compile though). What is not guaranteed to be true is

(void *)&b == (void *)dynamic_cast<A*>(&b)
 
F

Frederick Gotham

Rolf Magnus posted:
They don't need to point to the same address, but they must compare
equal.


That's pretty nifty! So basically the following will always be true:


&derived_object == static_cast<Base*>( &derived_object )


But that following need not necessarily be true:


static_cast<void*>(&derived_object) ==

static_cast<void*>( static_cast<Base*>( &derived_object ) )
 
F

Frederick Gotham

Rolf Magnus posted:
They don't need to point to the same address, but they must compare
equal.


That's pretty nifty! So basically the following will always be true:


&derived_object == static_cast<Base*>( &derived_object )


But that following need not necessarily be true:


static_cast<void*>(&derived_object) ==

static_cast<void*>( static_cast<Base*>( &derived_object ) )
 
F

Frederick Gotham

Rolf Magnus posted:
They don't need to point to the same address, but they must compare
equal.


That's pretty nifty! So basically the following will always be true:


&derived_object == static_cast<Base*>( &derived_object )


But that following need not necessarily be true:


static_cast<void*>(&derived_object) ==

static_cast<void*>( static_cast<Base*>( &derived_object ) )
 
F

Fraser Ross

A pointer to a base class is not necessarily equal to a pointer to a
class derived from it. You could see the free item 27 of Effective C++
3rd edition for an explanation.

Fraser.


"Alberto Luaces"
Hi,

can I always rely on the behaviour shown in the code? (A pointer to a class
is always the same as the pointer obtained from its base class with RTTI)

#include <iostream>

class A{};
class B : public A {};

int main(){
B b;

std::cout << (&b == dynamic_cast<A*>(&b)) << "\n"; // Always true?
return 0;
}

Thank you,

Alberto
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top