answersLogoWhite

0


Best Answer

Yes, via a static up-cast. Up-casting to a base class is always safe so there's no need to dynamically cast (dynamic casting is only a requirement when down-casting, but even then that's usually a sign of a poorly-designed virtual interface).

Consider the following classes:

class A{};

class B : public class A{};

If we create an instance of B we can then point at that instance using a derived class pointer:

B b;

B* pb = &b;

To "convert" pb to a base class pointer we statically cast it like so:

A* pa = (A*) pb;

Or, more simply, we can just point at the derivative's address:

A* pa = &b;

Note that we don't actually convert anything. Although pa points to the base class, the actual instance we're pointing at is still a derived class. This is what enables us to pass pointers and references to derived classes into functions that actually expect pointers or references to base classes. Even though those functions can have no prior knowledge of what derivatives it might receive in the future, they simply don't have to. As far as the functions are concerned, you've passed a base class, not a derivative. But if you call a virtual method of the base class then the derived class method will be called instead -- and that's precisely what one expects of a derivative. This is how inheritance and polymorphism work.

If you really want to convert to a base class (completely eliminating the derived class), then you must call the base class copy constructor, passing the derived class as the argument:

B b;

A a(b);

Note that a and b are now completely separate instances and a is nothing more than the base class component of b, devoid of all its derived class components. Thus if you were to mutate a, you will not mutate b, and vice versa. The casts shown previously do not create separate instances -- there is only the one instance -- so any mutations affect all pointers to that one instance, whether they be base class pointers or derived class pointers.

You can also use the copy constructor to point at a new instance of the base class, creating the new instance on the heap rather than the stack:

A* pa = new A(b);

In order to make use of A's assignment operator, you must provide a cast operator in B:

class B : public A

{

public:

operator A(){return(A(*this));}

};

Now you can perform the following assignment:

A a;

B b;

a = b;

Note that a and b remain completely separate instances.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can a derived class pointer be converted into a pointer to its public base class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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.


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 base class pointer?

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.


What is derived class in brief?

A derived class is any class that inherits from one or more other classes, known as base classes. The derived class inherits the sum total of all public and protected members of all its base classes, including their base classes. The derived class is a more specialised form of its base classes. Any members of the base classes that are declared virtual can be overridden, such that calling the base class method directly actually invokes the derived class method, thus enabling polymorphic behaviour.


What do you mean by protected derivation of a sub class from base class?

When you derive a class (the sub-class) from a base class using protected access, all public members of the base class become protected members of the derived class, while protected members of the base class will remain protected. Private members are never inherited so they remain private to the base class. By contrast, if you use public inheritance, the public members of the base class remain public to the derived class, while protected members of the base class remain protected in the derived class. If you use private inheritance, both the public and protected members of the base class become private to the derived class. Note that accessibility cannot be increased, only reduced or left the same. That is, a protected member of a base class cannot be inherited as a public member of a derived class -- it can only be declared private or remain protected. Note also that accessibility is viewed from outside of the derived class. That is, all members of a base class other than the private members are inherited by the derived class and are therefore fully accessible to the derived class. But from outside of the derived class, all base class accessibility is determined by the access specified by the type of inheritance.

Related questions

Give an example of constructor and destructor in derived class?

class superclass { public: superclass() {... } // c'tor public: virtual ~superclass() {... } // d'tor }; // superclass class derived: public superclass { public: derived() : superclass() { ... } // derived c'tor public: virtual ~derived() {... } // derived d'tor }; // derived class


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;


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.


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 base class pointer?

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.


What is derived class in brief?

A derived class is any class that inherits from one or more other classes, known as base classes. The derived class inherits the sum total of all public and protected members of all its base classes, including their base classes. The derived class is a more specialised form of its base classes. Any members of the base classes that are declared virtual can be overridden, such that calling the base class method directly actually invokes the derived class method, thus enabling polymorphic behaviour.


What do you mean by protected derivation of a sub class from base class?

When you derive a class (the sub-class) from a base class using protected access, all public members of the base class become protected members of the derived class, while protected members of the base class will remain protected. Private members are never inherited so they remain private to the base class. By contrast, if you use public inheritance, the public members of the base class remain public to the derived class, while protected members of the base class remain protected in the derived class. If you use private inheritance, both the public and protected members of the base class become private to the derived class. Note that accessibility cannot be increased, only reduced or left the same. That is, a protected member of a base class cannot be inherited as a public member of a derived class -- it can only be declared private or remain protected. Note also that accessibility is viewed from outside of the derived class. That is, all members of a base class other than the private members are inherited by the derived class and are therefore fully accessible to the derived class. But from outside of the derived class, all base class accessibility is determined by the access specified by the type of inheritance.


Which base class member functions are not inherited by a derived class?

Derived classes only inherit the protected and public members of their base classes. Private member functions cannot be inherited by a derived class.


What is a public class?

A public class is a base class declared with public inheritance: class base { // ... }; class derived : public base { // ... }; In the above example, base is a public class of derived, thus derived is regarded as being a type of base. The derived class inherits all the public and protected methods of its base. Protected methods are accessible to the derived class, its derivatives and their friends. If base were declared protected, its public methods become protected methods of derived. The base class is then an implementation detail of derived; only members of derived, its derivatives and their friends can treat derived as being a type of base. If declared private, the public and protected methods of base become private methods of derived. The base class is then an implementation detail of derived; only members of derived and its friends can treat derived as a type of base.


What are Public and Private inheritance in c plus plus?

Public, protected and private inheritance determine how the public and protected base class members are inherited by the derived class. Private members are never inherited and are therefore unaffected by the type of inheritance (they remain private to the base class). The following table summarises how inheritance affects accessibility of base class members with respect to the derived class: public inheritanceprotected inheritanceprivate inheritancepublic member of base classpublic member of derived classprotected member of derived classprivate member of derived classprotected member of base classprotected member of derived classprotected member of derived classprivate member of derived classprivate member of base classprivate member of base classprivate member of base classprivate member of base class Note that accessibility to individual public and protected base class members can be overridden within the derived class, regardless of the type of inheritance specified.


Is it true that a derived class inherits all the members of its base class?

False. A derived class inherits the public and protected members of its base class. Private members of the base class cannot be inherited.


How can a base class protect derived classes so that changes to the base class will not affect them explain with example?

In order for a base class to protect derived classes, so that changes to the base class do not affect the derived classes, you must make sure that the public interface exposed by the base class does not change when the implementation of those public methods do change. You can also prevent inadvertant access to the base class attributes from the derived class, by making them private. class base { public: base(...) {...}; /* constructor */ base~(...) {...}; /* destructor */ method(...) {...}; /* other public methods */ private: ... etc. } class child : base { public: ... private: ... } So long as the calling sequence and functionality (interface) of the base class public methods do not change, the implementation of those public methods can change, and the private methods and attributes can change, without impacting any child class.