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

Base class pointer in dervied class pointer

class BaseEx
{
public :
virtual void Display()
{
cout<<"BaseEx:: Display()"<<endl;
}
};

class DervEx : publjc BaseEx
{
public :
void Display()
{
cout<<"Derv:: Display()"<<endl;
}

void Print()
{
cout<<"Derv:: Print()"<<endl;
}
};

int main()
{
Derv *d = reinterpret_cast<Derv *> ( new BaseEx);

d->Print();
return 0;

}

I got the output as Derv:: Print() and not a crash as expected.

How does it work??
Feb 17 '06 #1
1 2415
Banfa
9,065 Expert Mod 8TB
I suspect this not a good thing to do having read the documentation of reinterpret_cast.

You need to remember that a simple class like this with no data members is just a list of function pointers that are the same for all instances of the class. Since there are no member variables for DervEx::Print to access the code works.

If you change DervEx to

Expand|Select|Wrap|Line Numbers
  1. class DervEx : public BaseEx
  2. {
  3. public :
  4.   int i;
  5.  
  6.   DervEx(void) : i(5) {}
  7.   void Display() 
  8.   {
  9.     cout<<"Derv:: Display()"<<endl;
  10.   }
  11.  
  12.   void Print()
  13.   {
  14.     cout<<"Derv:: Print(): "<<i<<endl;
  15.   }
  16. };
You will see that the output is garbage because the data actually pointed at is garbage and not an actual DervEx class.

You would only get a crash if you attempted to access a piece of memory outside the legal bounds for your platform, in MSVC (what I am using) since the heap starts at several k and all the used data is on the heap and very small the extra location accessed by DervEx::i is still valid even though it is not an actual part of the class.

The documentation that I have of reinterpret_cast states that you should not use pointers cast in this way as you have no garantee about there validity in fact you code works if you change

DervEx *d = reinterpret_cast<DervEx *> ( new BaseEx);

to

DervEx *d = reinterpret_cast<DervEx *> (new int);

I think the point is that you should only use reinterpret_cast in very special circumstances not in normal use and having used it if you have converted pointer types you should not be using the new pointer.
Feb 17 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: dumboo | last post by:
hi there i m having a base class class base { ... }; and two derived class, which have inherited 'base' but the inheritance is not virtual
7
by: Douglas Peterson | last post by:
Take a look at this code, it looks funny as its written to be as short as possible: -- code -- struct Base { ~Base() { *((char*)0) = 0; } }; struct Derived : public Base
6
by: Paul | last post by:
In real life situation, do we ever come across a situation where we would need two base objects in an object. A snippet is worth 1000 words (: so... class Base { }; class Derived1:public Base...
2
by: SunScreen | last post by:
Hi all, I would like to help with the following: // my conventions: // BC = Base Class // DC = Derived Class class BC // this ADT base class or interface {
15
by: Simon | last post by:
Hi, If I have two classes. Class A { public: A( const char *sz); ~A(); virtual void Foo() = 0;
10
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
3
by: John B | last post by:
I have a variable in a base class defined as: protected Control validationControl; How can I ensure that it is set is any dervied classes. I see that you cannot set variables to abstract. ...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
2
by: cmonthenet | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
Oralloy
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,...

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.