tags:

views:

794

answers:

5

Hi guys..... why this is happen ?

When u create abstract class in c++ Ex: Class A (which has a pure virtual function) after that class B is inherited from class A

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

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 .

+3  A: 

class A is abstract but class B is not. In order to construct 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;
};

The A class layout could be something like this:

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

The B class layout could be something like this:

+---------+     +---------+
| vftable | --> | ~B()    | --> address of B::~B()
+---------+     +---------+
| i       |     | foo()   | --> address of B::foo()
+---------+     +---------+
| j       |
+---------+
dalle
Thanks for the answer but i want to know that how it creates the internal object of abstract class??????
Access Denied
Ridiculously over elaborate answer to a simple question.
Martin York
Perhaps, I made an simple answer first, but that didn't answer Darshan's question.
dalle
+8  A: 

Quick answer: constructors are special.

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

It's the same for the derived B. The constructor for A runs first. Now it's an A. Then the constructor for B starts running. During this, the object is still really an A. Only when B's constructor finishes does it become a B.

You can verify this by trying to call the pure virtual function from the constructors. If the function is defined in A, and B's constructor calls it, there will be a runtime error instead of running B's override, because the object is not of type B yet.

The compiler will not allow you to generate code that will construct an A, due to the pure virtual function. But it will generate code to construct an A as part of the process of constructing a B. There's no magic involved in this. The rule that you cannot construct an A is imposed by the language rules, not by physics. The language lifts that rule under the special circumstance of constructing objects of B.

Daniel Earwicker
After trying to call the virtual method from the constructor and seeing the result, always remember not to call virtual methods in constructors :)
David Rodríguez - dribeas
And then forget that rule in C++/CLI where it doesn't apply!
Daniel Earwicker
+1  A: 
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() {..}
};
Drakosha
A: 
 And if class A has constructor called A() suppose i created an
 Object of class B then the compiler initializes the base class
 first i.e.class A and then initialize the class B
 Then.......?

Actually you have it the wrong way around:

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

If you do not want to use the default constructor you must explicitly put the call to the appropriate A constructor as the first element in the initializer list.

When the construction of A is complete the construction of 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 word the above as if you consider A and B different things. An object of class B is also an object of class A. It is the object as a whole that is valid. The whole object is of class B but this contains (as part of the same object) all the information that was from class A.

Martin York
A: 

Just because you can't instantiate class A directly doesn't mean it's impossible to instantiate class A. You're not allowed to instantiate A because the compiler knows that A is abstract and rejects any code you write that attempts to directly instantiate A. It forbids code like this:

A a;
new A();

What makes a class abstract is that it has pure virtual methods. There is nothing inherently preventing such a class from being instantiated, though. The C++ standard simply says it's not allowed. The compiler is perfectly capable of generating instructions to instantiate an abstract class. All it has to do is reserve the right amount of memory and then invoke the constructor, the same as it would for a non-abstract class.

When you instantiate B, all the memory for the class gets allocated at once. Since all the bytes are there, there is essentially an A instance there, ready to be initialized by the constructor. (But note that the memory isn't formally considered an object of type A until after the A constructor has finished running.) The A constructor runs, and then the B constructor runs.

Rob Kennedy