Interesting C ++ function Abstract

Why is this happening?

When u creates an abstract class in C ++ Ex: Class A (which has a pure virtual function) then class B inherits from class A

And if class A has a constructor named A () , suppose I created an Object of class B , then the compiler first initializes the base class, i.e. class A , and then initializes the class of class B Then .......?

First, we cannot access the constructor of any class without an object, and then how it initializes the constructor of an abstract class if we cannot create an object of an abstract class.

+3
source share
5 answers

Quick answer: constructors are special.

When constructor A is still working, then the constructed object is still not truly type A. It is still being constructed. When the constructor finishes, now it's A.

The same for derivative B. First, the constructor for A. is executed. Now this is a. Then the constructor for B starts working. During this, the object is still actually A. Only when constructor B ends, does it become B.

You can verify this by trying to call a pure virtual function from the constructors. If a function is defined in A and constructor B calls it, instead of overriding B, a runtime error will occur because the object does not yet have type B.

The compiler will not allow you to generate code that will build A, due to a pure virtual function. But it will generate code to build A as part of the process of building B. There is no magic. The rule that you cannot build A is imposed by language rules, not physics. Language raises this rule under the special circumstance of constructing objects B.

+8
source

class A is abstract, but class B not. To build class B , it must implement all the pure virtual member functions of class A

 class A { public: A() {} virtual ~A() {} virtual void foo() = 0; // pure virtual int i; }; class B : public A { public: B() {} virtual ~B() {} virtual void foo() {} int j; }; 

A class A layout might look something like this:

  + --------- + + --------- +
 |  vftable |  -> |  ~ A () |  -> address of A :: ~ A ()
 + --------- + + --------- +
 |  i |  |  foo () |  -> NULL, pure virtual
 + --------- + + --------- +

A class B layout might look something like this:

  + --------- + + --------- +
 |  vftable |  -> |  ~ B () |  -> address of B :: ~ B ()
 + --------- + + --------- +
 |  i |  |  foo () |  -> address of B :: foo ()
 + --------- + + --------- +
 |  j |
 + --------- +
+4
source
 struct A { A(int x) {..} virtual void do() = 0; }; struct B : public A { B() : A(13) {} // <--- there you see how we give params to A c'tor virtual void do() {..} }; 
+1
source
  And if class A has constructor called A() suppose i created an Object of class B then the compiler initializes the base class first ieclass A and then initialize the class B Then.......? 

Actually you are mistaken:

When you create an object of class B, constructor B is called. If you do not specify how constructor B calls constructor, then the compiler automatically inserts the default constructor call A as the first action in the initializer list.

If you do not want to use the default constructor, you must explicitly put the call into the corresponding constructor A as the first element in the list of initializers.

When construction A is completed, construction B will continue.

 First thing is we can not access a constructor of any class without an Object then how it is initialize the constructor of abstract class if we can not create an object of abstract class . 

You say above as if you were considering A and B different things. An object of class B is also an object of class A. It is a valid object as a whole. The whole object has class B, but it contains (as part of the same object) all the information received from class A.

0
source

Just because you cannot instantiate class A directly does not mean that it is not possible to instantiate class A You are not allowed to instantiate A , because the compiler knows that A is abstract and rejects any code you write that tries to directly instantiate A He forbids the following code:

 A a; new A(); 

What a class summary does is that it has pure virtual methods. However, there is nothing to prevent the creation of such a class. The C ++ standard simply says that it is forbidden. The compiler is very capable of generating instructions for instantiating an abstract class. All you need to do is to reserve the required amount of memory and then call the constructor, as for a non-abstract class.

When you create an instance of B , all the memory for the class gets immediately. Since all bytes are there, there is essentially an instance of A , ready for initialization by the constructor. (But note that memory is not formally regarded as an object of type A until constructor A finishes work.) Constructor A starts, and then constructor B starts.

0
source

All Articles