answersLogoWhite

0

What is base class pointer?

Updated: 12/12/2022
User Avatar

Wiki User

10y ago

Best Answer

It is exactly what it says it is: a pointer to a base class. The assumption is that you have an object to a derived class, but actually hold a pointer to its base class. The following minimal example demonstrates this:

class base{ public: virtual ~base(){} };

class derived: public base{};

int main()

{

base* p = new derived; // base class pointer to a derived instance.

delete( p );

return(0);

}

Note that derived has an "is-a" relationship with base (derived is a base), thus the above code is perfectly legal. Moreover, because the base class destructor is declared virtual, when you delete p you automatically destroy the instance of derived before the instance of base, thus ensuring a clean teardown (without a virtual destructor, a dangling reference to derived would be left behind, which will only lead to problems further down the line).

Taking things further, calling any virtual methods upon the base class automatically invokes the override in your derived class, thus ensuring that your derived class behaves accordingly, polymorphically, even though you only hold a pointer to the base class. In other words, the base class provides a generic interface that is common to all its derivatives, and you can call those generic methods via the base class pointer without ever needing to know the actual derived type.

Remember that base classes should never know anything about their derivatives since a new derivative could be created at any time in the future and would therefore be impossible to predict in advance. But so long as the derivative makes use of the virtual functions (the generic interface) provided by the base class, there is never any need to know the actual type. The derivative's own v-table takes care of that for you, thus completely eliminating the need for expensive runtime type information and dynamic downcasts (which is always a sign of poor class design). This then makes it possible for your derived class overrides to call non-generic methods, thus extorting non-generic behaviour from what is essentially a generic, base class pointer.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is base class pointer?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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 pointer overloading?

Pointer overloading is another name for polymorphism. You declare a class virtual and then derive some classes from it. You delare a pointer to the base class and, at run time, assign a value to that pointer that refers to any of the classes in the derivation hierarchy. Deferencing that pointer, then, will properly choose the correct method based on which type of class was used, at run time. This works because each class type that is virtual contains a static v-table of methods that is used to choose the actual method to use at run time.


Is class a pointer?

No. In computer programming, a class is a data type while a pointer is a variable that can store a memory address.


Why converting a base class pointer to a derived class pointer is consider dangerous by the compiler?

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.


What is the order of construction and destruction in c plus plus?

The least-derived base classes are always constructed first, in the order specified by the derived class inheritance list. The most-derived class (the one you are actually instantiating) is always constructed last. Destruction is basically the reverse of construction. However, base class destructors must be declared virtual to ensure that the most-derived class destructor is called first, regardless of which class destructor is actually invoked. That is, if you hold a pointer to a base class that has no virtual destructor, deleting that pointer will only destroy the base class, not the derived class, leaving the derived class in an invalid state (because it no longer has an underlying base class) and with no way to recover the memory it consumes. It is important to remember that if you declare any virtual methods within a class, you must also declare the destructor virtual. A class without a virtual destructor is not intended to be derived from. If it has virtual methods, but no virtual destructor, it is not well-formed and must not be used as a base class.

Related questions

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.


Why pointer is not an object?

A pointer in itself is not an object, because it is not an instance of a class. Of course you can define a class which has only one member, which is a pointer. class Pointer { public void *ptr; }; Pointer p, q, r;


What is pointer overloading?

Pointer overloading is another name for polymorphism. You declare a class virtual and then derive some classes from it. You delare a pointer to the base class and, at run time, assign a value to that pointer that refers to any of the classes in the derivation hierarchy. Deferencing that pointer, then, will properly choose the correct method based on which type of class was used, at run time. This works because each class type that is virtual contains a static v-table of methods that is used to choose the actual method to use at run time.


Is class a pointer?

No. In computer programming, a class is a data type while a pointer is a variable that can store a memory address.


Why converting a base class pointer to a derived class pointer is consider dangerous by the compiler?

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.


What is the order of construction and destruction in c plus plus?

The least-derived base classes are always constructed first, in the order specified by the derived class inheritance list. The most-derived class (the one you are actually instantiating) is always constructed last. Destruction is basically the reverse of construction. However, base class destructors must be declared virtual to ensure that the most-derived class destructor is called first, regardless of which class destructor is actually invoked. That is, if you hold a pointer to a base class that has no virtual destructor, deleting that pointer will only destroy the base class, not the derived class, leaving the derived class in an invalid state (because it no longer has an underlying base class) and with no way to recover the memory it consumes. It is important to remember that if you declare any virtual methods within a class, you must also declare the destructor virtual. A class without a virtual destructor is not intended to be derived from. If it has virtual methods, but no virtual destructor, it is not well-formed and must not be used as a base class.


What is a pointer variable B?

pointer variable B holds base address of B


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 you have virtual constructor in cpp?

A virtual function is a method defined and implemented in a base class that we are expected to override in our derived classes.When we derive a class from a base class, we automatically inherit all the public and protected members of the base class, but we are free to override any and all the methods, including private methods (we can even change the access type if we wish).However, when we override a method in the base class, we can only call that override when we actually have a reference or pointer to the derived class itself. If we cast the reference or pointer to the base class we will end up calling the base class method. This is expected behaviour and is normally fine, but what happens if we have a pointer to a base class but we actually want to call the derived class method? This is particularly important when that call comes from the base class itself. How will it determine its actual type and make the correct call?We could use runtime information within the base class to determine the actual type, but this would break a fundamental rule of encapsulation: a base class should NEVER be concerned about the inner workings of any of its derived classes. If we allow this, we'd be forced to update the base class every time we derived a new class from it. Apart from the performance penalty incurred with accessing runtime information, maintaining the base class code will quickly become unmanageable.That is where virtual functions come in. Even though we are pointing at a base class, when we call a virtual function we actually call the derived class method instead. This is extremely powerful: we are no longer concerned with the actual type of object any more. We can treat the base class as if it were a generic data type, and the compiler will know exactly which version of a function to call regardless of whether the call was made from the base class or not.But what if we want to call the base class method? Simple: make an explicit call to it. We don't need runtime information for this since every derived class is also a base class as well. The only exception is when the virtual function is declared private in the base class. In this case, we are not expected to call the base class method at all (only the base class and friends of the base class can make an explicit call to its private methods).


Why can't we increment an array like a pointer?

once we initialize the array variable, the pointer points base address only & it's fixed and constant pointer


What is the use of keyword virtual?

New is used to dynamically allocate memory in C++. It will also call the default constructor of a class that is created with it. int *array; // First declare a pointer array = new int [5] // Allocate enough memory for a 5 element int array // To deallocate your memory when you are finished with it, you must call delete on your pointer delete[] array; myClassType *mct = new myClassType; // example for class ... delete mct; // class delete The virtual keyword means that the class has a static virtual table (vtable) containing function pointers for each type of the class. This enables polymorphism by allowing a pointer to the base class to be assigned an address of any instance of the class in the hierarchy, and then invoking a method through that pointer - which will properly choose the correct method for that class type. Virtual also ensures that the constructors and destructors are invoked in the correct order.


Why is it not advisable to use this pointer with static members?

It is not inadvisable, it is impossible. Static member methods do not have access to a this pointer since they are not associated with any instance. Static members are scoped to the class, not to an object (an instance of the class). Only instance members have access to the this pointer.