C++

Topic: Inheritance

Converting Derived* → Base* works OK; why doesn't Derived** → Base** work? 

Because converting Derived** → Base** would be invalid and dangerous.C++ allows the conversion Derived* → Base*, since a Derived object is a kind of a Base object. However trying to convert Derived** → Base** is flagged as an error. Although this error may not be obvious, it is nonetheless a good thing. For example, if you could convert Car** → Vehicle**, and if you could similarly convert NuclearSubmarine** → Vehicle**, you could assign those two pointers and end up making a Car* point at a NuclearSubmarine:class Vehicle {public:  virtual ~Vehicle() { }  virtual void startEngine() = 0;};class Car : public Vehicle {public:  virtual void startEngine();  virtual void openGasCap();};class NuclearSubmarine : public Vehicle {public:  virtual void startEngine();  virtual void fireNuclearMissle();};int main(){  Car   car;  Car*  carPtr = &car;  Car** carPtrPtr = &carPtr;  Vehicle** vehiclePtrPtr = carPtrPtr;  // This is an error in C++  NuclearSubmarine  sub;  NuclearSubmarine* subPtr = ⊂  *vehiclePtrPtr = subPtr;  // This last line would have caused carPtr to point to sub !  carPtr->openGasCap();  // This might call fireNuclearMissle()!  ...}In other words, if it were legal to convert Derived** → Base**, the Base** could be dereferenced (yielding a Base*), and the Base* could be made to point to an object of a different derived class, which could cause serious problems for national security (who knows what would happen if you invoked the openGasCap() member function on what you thought was a Car, but in reality it was a NuclearSubmarine!!). Try the above code out and see what it does — on most compilers it will call NuclearSubmarine::fireNuclearMissle()!(BTW you'll need to use a pointer cast to get it to compile. Suggestion: try to compile it without a pointer cast to see what the compiler does. If you're really quiet when the error message appears on the screen, you should be able to hear the muffled voice of your compiler pleading with you, "Please don't use a pointer cast! Pointer casts prevent me from telling you about errors in your code, but they don't make your errors go away! Pointer casts are evil!" At least that's what my compiler says.) 

Browse random answers:

Explain the ISA and HASA class relationships. How would you implement each in a class design?
Does c++ support multilevel and multiple inheritance?
What is RTTI?
What is the difference between composition and inheritance?
What is an accessor?
what is the use of volatile keyword? Give me one example?
Why Call by reference technique is used in case of Copy constructor & not call by value?
Is there any way to write a class such that no class can be inherited from it. Please include code
What do you mean by inheritance?
What does inheritance means in C++? What are the different forms of inheritance? Give an example for each.
How to create object for singleton class?
it possible to inherit the private member in drived class?
Can you prevent your class from being inherited and becoming a base class for some other classes?
what are the advantages and disadvantages of inheritance?
What is the difference between superclass and subclass?
Whether there will be a default constructor provided by the compiler in above case ?class A(){};int main(){A a;}
When should you use multiple inheritance?
What is a modifier?
What is a mutable member?
If a base class has a bunch of overloaded constructors, and an inherited class has another bunch of overloaded constructors, can you enforce a call from an inherited constructor to an arbitrary base constructor?
Can you allow class to be inherited, but prevent the method from being over-ridden?
how to select access modifier & what's need of access modifier?
Can you inherit multiple interfaces?
how many types of Inheritence ?
explain the real word example of multilevel inheritance?
Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a virtual function is a syntax error. Does c++ support multilevel and multiple inheritance?
When would I use inheritance?
How do you express inheritance in C++? 
Is it OK to convert a pointer from a derived class to its base class? 
What's the difference between public, private, and protected? 
Why can't my derived class access private things from my base class? 
How can I protect derived classes from breaking when I change the internal parts of the base class? 
I've been told to never use protected data, and instead to always use private data with protected access functions. Is that a good rule? 
What is inherited from the base class?
Explain about Inheritance between classes ?
Explain about Base & Derived Classes ?
Explain about the Access Control and Inheritance ?
How many Type of Inheritance ?
What are the FORMS OF INHERITANCE ?
Explain about execution of base class constructor ?
What are the Overriding of method(function) in inheritance ?
Explain about Virtual Base Class ?
Why java does not support Multiple Inheritance?
What are Encapsulation, Inheritance and Polymorphism?
What is the object-oriented design,inheritance and dynamic polymorphism in c++?
When should you use multiple inheritance?
What is public, protected, private in C++?
Should I hide member functions that were public in my base class? 
Converting Derived* → Base* works OK; why doesn't Derived** → Base** work? 
What would be printed out – and in what order?  execution order of the constructor and destructor in inheritance. 
Inheritance syntax
Explain about Inheritance Overview ?
Explain about Key Properties of C++ Inheritance