answersLogoWhite

0

Casting from a base class to a derived class is dangerous because a base class may not always have all the variables a derived class will.

If you have a base class Foo with the following declared variables:

int x;

int y;

int z;

and a class derived from Foo called Bar with the following declared variables:

int a;

int b;

int c;

and you cast Foo to Bar, the memory for the extra 3 variables a, b, and c have not been set aside. So when you try to modify a, b, or c you will corrupt data and will possibly crash your program because you're touching memory that's not really yours. However, casting Bar to Foo is okay because Bar includes all the variables Foo has laid out the same way in memory. You just won't be able to access a, b, and c using a Foo pointer even though they'll be there.

User Avatar

Wiki User

9y ago

What else can I help you with?

Continue Learning about Engineering

What is the Object class parent?

Object is the topmost class in the Java Class hierarchy. There is no Class above Object. All classes in Java are implicitly derived from Object.


Explain the following tearm in the context of object oriented programming Also explain how these concept are implement in c by given an example program for each?

Explain the following terms in the context of object oriented programming. Also explain how these concepts are implemented in C++ by giving an example program for each.


The tick event is found only in which object in visual basic?

The timer object or any object derived from a timer object. However, you can also create your own objects with a tick event. How that event is actually triggered is entirely up to you.


What is the Practical application of a derived class object stored in base class pointer?

If you have base class derived object pointing by base class pointer, then you have the power of run time polymorphism in your hand, which gives you the ability to call the derived class implementation of the virtual member function. If we declare the member function as virtual in base class which needs to overridden in derived class, then you can decide at run time which implementation will be called at run time.


What is the main purpose in using a destructor?

Destructors are called automatically whenever an object falls from scope. Even if you don't actually declare a destructor, one is implied. The primary purpose of a destructor is to give the programmer one final chance to clean up an object's memory allocations before the object is destroyed forever. If an object has allocated memory associated with it (via member pointer variables), then that memory must be released before the object is destroyed, otherwise a memory leak is inevitable. Destructors are also important in class inheritance. When a derived object falls from scope, its immediate base class destructors are called, followed by their base class destructors. This is effectively the reverse of an object's construction, which always begins with the least-derived base classes, working up to the most-derived class, which is the object itself. Just as a derived class cannot be instantiated until all its base classes are constructed, a base class object cannot be destroyed until all its derived class objects are destroyed. All the destructors in a class hierarchy must be declared virtual to ensure the correct most-derived destructor is called whenever a base class reference falls from scope.

Related Questions

Why is there type casting in java?

Type casting exists to allow the use of base class or base interface reference but yet if you know that the object in hand is aspecific derived class, you can typecast and access methods available only in the derived class.


Using specific examples, explain the effect of different environmental factors on phenotype of crop plants and animals?

[object Object]


When is a shadow smaller than the object casting it?

A shadow can be smaller than the object casting it when the light source is close to the object. This causes the shadow to be more compressed and appear smaller in relation to the object.


What is an inorganic object?

An inorganic object is a substance that is not derived from living organisms and does not contain carbon-hydrogen (C-H) bonds. Examples include minerals, metals, ceramics, and most synthetic materials.


The definition casting are?

The definition of the word casting is " an object made by pouring molten metal or other material into a mold."


Describe three factors that determine the size of a shadow and explain how each one cam make the shadow larger or smaller?

The height of the object casting the shadow, the height of the sun in the sky, what angle you are at when looking at the shadow.


Can an object be accelerated if it is moving with constant speed If yes explain giving examples.?

Yes, it can. Perhaps the simplest example is when an object moves at constant speed, in a circle. In this case, the speed doesn't change; the velocity does.


What is bcnf explain with example?

define BCNF. Explain with appropriate example


What is one object in space casting a shadow on another?

A shadow cast on one object in space on another is ECLIPSE


Explain Long run economics?

[object Object]


What examples of payload does the writer provide?

What examples of payload does the writer provide?


What are the examples of diagonal communication?

What are the examples of diagonal communication regarding prcc