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

typecasting the base class to derived class.

#include <stdio.h>

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

class Derived:public 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.
Jul 22 '05 #1
3 3485
> 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
Jul 22 '05 #2
On 20 Feb 2004 03:51:14 -0800 in comp.lang.c++, co*********@yahoo.com
(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".

Jul 22 '05 #3
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 ?
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gabriel Genellina | last post by:
Hi In the following code sample, I have: - a Worker class, which could have a lot of methods and attributes. In particular, it has a 'bar' attribute. This class can be modified as needed. - a...
3
by: Kapil Khosla | last post by:
Hi, I have been trying to understand this concept for quite sometime now somehow I am missing some vital point. I am new to Object Oriented Programming so maybe thats the reason. I want to...
1
by: Dave | last post by:
Hello NG, Regarding access-declarations and member using-declarations as used to change the access level of an inherited base member... Two things need to be considered when determining an...
10
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
4
by: Jeff | last post by:
The derived class below passes a reference to an object in its own class to its base calss constructor. The code compiles and will run successfully as long as the base class constructor does not...
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;
5
by: Dennis Jones | last post by:
Hello, I have a couple of classes that look something like this: class RecordBase { }; class RecordDerived : public RecordBase {
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
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...
1
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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.