answersLogoWhite

0


Best Answer

There are two methods of casting one type to another: static casting and dynamic casting (both of which apply to pointers and references to objects). Statically casting a derived class to a base class is typesafe as the base class is guaranteed to exist if the derived class exists. However, static casting from a base class to a derived class is always considered dangerous as the conversion is not typesafe. Dynamic casting exists to cater for this scenario, however it is only possible when the base class is polymorphic (thus ensuring the required runtime information is available). If the conversion is not possible, the return value is NULL. However, it is considered bad programming practice to dynamically cast from a base class to a derived class. If the base class is polymorphic (which it must be), there is NEVER any need to dynamically cast between types. Virtual methods ensure correct behaviour. Whenever you are forced to dynamically cast from a base class to a derived class, consider redesigning the base class interface instead, as it is a clear sign of bad design. It is not dangerous, however; only static casting from a base class to a derived class is considered dangerous.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why converting a base class pointer to a derived class pointer is consider dangerous by the compiler?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What are the importance of turbo c?

i came to know about that turbo c compiler is having 32 number of disadvantages........ i know about 10-12 disadvantages and i am sharing those wid u .... 1) Turbo c is a 16bit compiler ... means it can access about 16bits of data at a time... 2)Turbo c is based upon 8086 microprocessor... and it can run on x86family.... 3) Turbo c is derived from MS-DOS whereas GCC compiler which runs on linux..is a very good one as compared to Turbo C compiler..,.. 4)Also we cant do ODBC and Networking programming over the Turbo C comiler whereas we can do above programming in GCC compiler successfully by installing one ODBC library in it.... 5)we cant do graphics programming over here, rather we can do graphics programming. in GCC compiler using Glad Interface... 6) Turbo C compiler can access only 1mb of RAM, which is a demerit...whereas GCC compiler can access 4gb OF MEMORY.. 7) 7) Turbo C compiler is based upon MS DOS operating system.. which is a single process operating system..whereas GCC Compiler is based on 80386 processor(LINUX) and it is multiprocess operating system.....


What is late bound function call in C plus plus?

Late binding, or dynamic binding, occurs at runtime. Early binding, or static binding, occurs at compile time. The difference between the two is that, with early binding, the compiler knows exactly which function will be called in advance and can statically bind to that function during compilation. With late binding, the compiler does not know which function will be called in advance, it only knows the function signature. Thus the binding has to be done at runtime. For example, if you call a virtual function of a base class, the compiler cannot determine in advance the exact type of the derivative (in some cases it may not be derived at all). All it knows is that you've called the base class method and that an override may or may not exist at runtime. Thus the exact method to be called will ultimately be determined at runtime, via the derived class' virtual table. Note that although the derived class is itself late bound (because the compiler cannot know the exact type of a derivative that may be made available in the future), only the virtual methods of the base class need to be late bound. Non-virtual methods can be statically bound, since they are never expected to be overridden. Calling virtual methods is actually no different to using function pointers within your code (a virtual table is simply an array of function pointers, with one table per base class, and another for each of its derivatives). Again, the compiler cannot know in advance where a function pointer will actually be pointing at compile time, thus the call must be dynamically bound at runtime.


How does virtual function support run time polymorphism?

Virtual functions are used to suport runtime polymorphism.In C++,if we have inheritance and we have overridden functions in the inherited classes,we can declare a base class pointer and make it to point to the objects of derived classes.When we give a keyword virtual to the base class functions,the compiler will no do static binding,so during runtime ,the base class pointer can be used to call the functions of the derived classes.Thus virtual functions support dynamic polymorphism.


Can a constructor be declared as virtual?

A constructor cannot be virtual because at the time when the constructor is invoked the virtual table would not be available in the memory. Hence we cannot have a virtual constructor. ## Constructor called implicitly not explicitly so constructor is not virtual.


Can a derived class make a public base function private true or false?

True. A derived class can make a public base function private. The derived function is private, within the derived class, but public in other contexts.

Related questions

What do scientist consider when they perform a cladistic analysis?

They consider derived characters.


Is derived and translated the same thing?

No, they are not the same. "Derived" refers to something that comes from a source or origin. "Translated" refers to converting text or information from one language to another. So, while both involve the transformation of something, they involve different types of transformation.


What are the importance of turbo c?

i came to know about that turbo c compiler is having 32 number of disadvantages........ i know about 10-12 disadvantages and i am sharing those wid u .... 1) Turbo c is a 16bit compiler ... means it can access about 16bits of data at a time... 2)Turbo c is based upon 8086 microprocessor... and it can run on x86family.... 3) Turbo c is derived from MS-DOS whereas GCC compiler which runs on linux..is a very good one as compared to Turbo C compiler..,.. 4)Also we cant do ODBC and Networking programming over the Turbo C comiler whereas we can do above programming in GCC compiler successfully by installing one ODBC library in it.... 5)we cant do graphics programming over here, rather we can do graphics programming. in GCC compiler using Glad Interface... 6) Turbo C compiler can access only 1mb of RAM, which is a demerit...whereas GCC compiler can access 4gb OF MEMORY.. 7) 7) Turbo C compiler is based upon MS DOS operating system.. which is a single process operating system..whereas GCC Compiler is based on 80386 processor(LINUX) and it is multiprocess operating system.....


Does compile time depend on instance characteristics?

Yes. Although dynamic binding occurs at runtime, the compiler ensures that all runtime instances are covariant with compile time types. That is, you cannot instantiate a base class pointer to a derived type that is not covariant with the base class. The derived type must have an "is-a" relationship or the compilation will fail.


What does amok mean?

Amok means "out of control", especially when armed and dangerous, or in a frenzy of violence - derived literally from the Malay for "to go on a killing spree".


When you define a c plus plus class what items are considered part of the interface?

The interface of a C++ class is the public methods and attributes that are exposed by the class. In a pure abstract base class, the interface is enforced by the compiler in each of the derived child classes.


What is the origin of the word deliberate?

The word "deliberate" comes from the Latin word "deliberatus," which is derived from the verb "deliberare," meaning "to consider carefully."


What is the name origin of tsutsugamushi disease?

The name tsutsugamushi is derived from two Japanese words: tsutsuga, meaning something small and dangerous, and mushi, meaning creature.


Which star systems were derived from various Star Wars sources to put in the Essential Atlas?

Some of the star systems derived from various Star Wars sources and included in the Essential Atlas are Corellia, Coruscant, Tatooine, Hoth, Bespin, Endor, Naboo, and Kashyyyk. These star systems are well-known locations in the Star Wars universe and were featured prominently in various media such as films, books, and games.


Do pigs have utters?

Udder is probably derived from 'under'. You could consider that pigs, goats, and cows all have udders.


Do lines have to be straight?

Yes. Consider the drawing of a table. ~ Actually, a circle is the set of all points at a given distance from the center. It's technically not derived from a line.


What makes hydrochloride a dangerous substance?

Hydrochloride is derived from hydrochloric acid combining with an organic base. Hydrochloric acid is very corrosive, both to materials and to tissues of the human body.