By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,614 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

Pointer to class equals pointer to base class

P: n/a
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
Jun 19 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Alberto Luaces wrote:
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.

#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


Jun 19 '06 #2

P: n/a
Alberto Luaces wrote:
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)

Jun 19 '06 #3

P: n/a
Rolf Magnus posted:
Alberto Luaces wrote:
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.

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 ) )

--

Frederick Gotham
Jun 19 '06 #4

P: n/a
Rolf Magnus posted:
Alberto Luaces wrote:
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.

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 ) )

--

Frederick Gotham
Jun 19 '06 #5

P: n/a
Rolf Magnus posted:
Alberto Luaces wrote:
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.

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 ) )

--

Frederick Gotham
Jun 19 '06 #6

P: n/a
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


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Jun 19 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.