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

Pointer to class equals pointer to base class

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
6 2489
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Abhijit Deshpande | last post by:
Is there any elegant way to acheive following: class Base { public: Base() {} virtual ~Base() {} virtual void Method() { cout << "Base::Method called"; return; } };
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
8
by: ceo | last post by:
Hi, Following is a program that doesn't give the expected output, not sure what's wrong here. I'm adding the size of derived class to the base class pointer to access the next element in the...
5
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)....
5
by: Scott | last post by:
Hi All, Am I correct in assuming that there is no way to have a base pointer to an object that uses multiple inheritance? For example, class A { /* ... */ }; class B { /* ... */ };
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.