473,325 Members | 2,816 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,325 software developers and data experts.

Value of a pointer to base type when assigned to a derived type

A very simple question. I have never had occasion to think about this
before.

Consider the following:

class A
{
virtual unsigned long bar()
{
return reinterpret_cast<unsigned long> (this);
}
};

class B
{
};

void foo()
{
B* b = new B;
A* a = b;
std::cout << "b == a?" << std::boolalpha << (a == b) << std::endl;
}

I would have thought this would print

true

but it doesn't. Does this mean that "a" only points at the "A" part of
"b"?

But, of course, if I do "a->bar()" the correct function from "B" will
be invoked. This is due to the fact that bar is a virtual function
which means the compiler does the correct thing.

Can someone tell me then why the pointers are different?

thanks,

-vijai.

Sep 5 '05 #1
4 1295

"Vijai Kalyan" <vi**********@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
A very simple question. I have never had occasion to think about this
before.

Consider the following:

class A
{
virtual unsigned long bar()
{
return reinterpret_cast<unsigned long> (this);
Why are you trying to convert a pointer to an unsigned long?
What would that mean? (If it has any meaning at all, it will
be platform specific, not specified by the language).
}
};

class B
{
};

void foo()
{
B* b = new B;
A* a = b;
This is not valid, and a compiler should give an error.
There's no defined conversion from type 'B*' to type 'A*'
std::cout << "b == a?" << std::boolalpha << (a == b) << std::endl;
Same problem here with the expression 'a == b'.
}

I would have thought this would print

true
I wouldn't. I would not expect the code to compile.

but it doesn't. Does this mean that "a" only points at the "A" part of
"b"?
'b' is not part of 'a'. They're completely different, unrelated
types.

But, of course, if I do "a->bar()" the correct function from "B" will
be invoked.
No. 'B' has no member functions. And even if it did, calling an
'A' member function has nothing to do with 'B'. Why do
you think it does?
This is due to the fact that bar is a virtual function
which means the compiler does the correct thing.
The fact that 'bar()' is virtual is irrelevant. 'B' is not
derived from 'A'. It's a completely different, unrelated type.

Can someone tell me then why the pointers are different?


As soon as you post some compilable, valid code, we
can discuss how it should behave.

-Mike
Sep 5 '05 #2
Sorry,

class B : public virtual A
{
};

And no, there is no meaning to convert the value of (this) to unsigned
long. It just is for the sake of putting something in the constructor.

-vijai.

Sep 5 '05 #3
It does give an output "true"

Sep 5 '05 #4

"Vijai Kalyan" <vi**********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Sorry,

class B : public virtual A
{
};

And no, there is no meaning to convert the value of (this) to unsigned
long. It just is for the sake of putting something in the constructor.

-vijai.


Under Microsoft Visual C++ .net 2003 it returns:
b == a?true

with a compile warning:
'reinterpret_cast' : pointer truncation from 'A *const ' to 'unsigned long'

Code is:

#include<iostream>
class A
{
virtual unsigned long bar()
{
return reinterpret_cast<unsigned long> (this);
}
};
class B: public virtual A
{
};
void foo()
{
B* b = new B;
A* a = b;
std::cout << "b == a?" << std::boolalpha << (a == b) << std::endl;
}

int main()
{
foo();
char x;
std::cin >> x;
return 0;
}
Sep 7 '05 #5

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; } };
5
by: Vinodh Kumar | last post by:
I see that casting changes the value of a pointer in case of multiple inheritance.In single inheritance also it is the same know?Isn't it? Vinodh Kumar P
0
by: Dave | last post by:
Hi everyone, (I already posted this to the VS.NET IDE news group without any responses, so I'm attempting one more time in this group) The issue I'm having is occuring in the IDE of VS.NET...
10
by: homecurr | last post by:
Here is my code Class A; Class B; Class C : public A, public B { .... } Class D {
2
by: davidzada | last post by:
Hi guys ! I try to work with application's pointers in c++ , and .. I have some problems ! I have base class that contain definition of pointer to application. This application's pointer...
3
by: Nindi73 | last post by:
Hi, I am in need of a deep copy smart pointer (Boost doesn't provide one) which doesnt require the contained types to have a virtual copy constructor. I wrote a smart pointer class that I think...
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...
10
by: stefven blonqhern | last post by:
hello all, having a problem with derived classes. i'll show by pseudo code example. first the base class and derived classes: class Shape { // base class for all shapes public: virtual void...
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 {
13
by: Rahul | last post by:
Hi Everyone, I was just playing around virtual functions and landed up with the following, class Base1 { public: virtual void sample() { printf("base::sample\n");
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.