Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > typecasting the base class to derived class.

Reply
Thread Tools

typecasting the base class to derived class.

 
 
cman
Guest
Posts: n/a
 
      02-20-2004
#include <stdio.h>

class Base
{
public:
Base()
{
printf("Base()\n");
}
~Base()
{
printf("~Base()\n");
}
};

class Derivedublic Base
{
public:
Derived()
{
printf("Derived()\n");
}
void myConst() const
{
printf("Derived's myConst function x=%d",x);
}
~Derived()
{
printf("~Derived()\n");
}
int x;
};

main()
{
Derived *dptr;
dptr=(Derived *)new Base();
dptr->x=10;
dptr->myConst();
delete dptr;
}


In the above code "dptr->myConst()" succeeds without giving any error
on Solaris, but generates an exception when ran on Winodws after
compiling in VC++

My Question is Why does it not fail? since typecasting should not
define x... this call should fail. I replaced "x" with "x[1024]" and
then used the last element in the array above, to isolate the
possiblity of having a fluke. But it still worked !

Thanks.
 
Reply With Quote
 
 
 
 
Niels Dybdahl
Guest
Posts: n/a
 
      02-20-2004
> My Question is Why does it not fail?

Pure (un)luck
There are no mechanisms in C++ or the operating systems that guarantee that
you will get an error message, if you address outside the memory area of a
structure. The virtual memory management in the operating systems might in
some cases do it, but never always.
If you use something like BoundsChecker, then you can get a guaranteed error
message on the cost of execution speed.

Niels Dybdahl


 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      02-20-2004
On 20 Feb 2004 03:51:14 -0800 in comp.lang.c++, http://www.velocityreviews.com/forums/(E-Mail Removed)
(cman) was alleged to have written:
> dptr=(Derived *)new Base();


Lots of things that can produce errors are unchecked (except by you.)
But casts surpass them all. When you use a case, that is old C style
cast as above or reinterpret_cast<>, you are assuring the compiler that
you know what you are doing and everything will be all right.

When you cast a base pointer to a derived type it is your responsibility
to first ensure that it actually points to a derived object. If you
fail to do that then all guarantees are voided. See "undefined
behavior".

 
Reply With Quote
 
cman
Guest
Posts: n/a
 
      02-23-2004
The program ran fine on Windows, Solaris and Linux as well.

Also, to verify that the program is not running by chance...
I replaced "int x" with "int x[1024]" and then wrote to the last
element in main() and read back the value from (ie "x[1023]"), but it
still ran fine.

But when I increased the array size to 4096, it crashed...
may be because now it tried to write outside the data segment.

Is this right ?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting from base to derived class in base constructor pastbin@gmail.com C++ 2 02-07-2008 02:41 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) developereo@hotmail.com C++ 4 05-23-2007 09:32 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Format of compiler generated derived destructor when base has 'virtual ~base() throw():" qazmlp C++ 1 04-10-2005 03:09 PM



Advertisments