By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,887 Members | 1,150 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,887 IT Pros & Developers. It's quick & easy.

calling pure virtual in abstract class's constructor

P: n/a
I have an abstract class A:

class A
{
public:
A(){ f(); }
virtual void f() = 0;
};

class B : public A
{
public:
void f() {...}
};

B b;

However, the compiler does not like me calling f in the constructor of A.
But this seems like it should be okay since A is pure virtual, f will be
given in derived classes and exist.
Sep 24 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
vsgdp wrote:
I have an abstract class A:

class A
{
public:
A(){ f(); }
virtual void f() = 0;
};

class B : public A
{
public:
void f() {...}
};

B b;

However, the compiler does not like me calling f in the constructor of A.
But this seems like it should be okay since A is pure virtual, f will be
given in derived classes and exist.


This is not OK, because when you're inside constructor A, B has not
been constructed yet. So you can't call on something that doesn't
exist yet.
Remember, that constructor A, gets constructed before constructor B.
That means any functions in class B, is not avialable to constructor A.

Sep 24 '05 #2

P: n/a
Ian
vsgdp wrote:
I have an abstract class A:

class A
{
public:
A(){ f(); }
virtual void f() = 0;
};

class B : public A
{
public:
void f() {...}
};

B b;

However, the compiler does not like me calling f in the constructor of A.
But this seems like it should be okay since A is pure virtual, f will be
given in derived classes and exist.

During A's constructor, there is no B, thus no f() to call.

You can't call a virtual method form a base class constructor.

Ian

Sep 24 '05 #3

P: n/a

Ian wrote:
vsgdp wrote:
I have an abstract class A:

class A
{
public:
A(){ f(); }
virtual void f() = 0;
};

class B : public A
{
public:
void f() {...}
};

B b;

However, the compiler does not like me calling f in the constructor of A.
But this seems like it should be okay since A is pure virtual, f will be
given in derived classes and exist.

During A's constructor, there is no B, thus no f() to call.

You can't call a virtual method form a base class constructor.

Ian


Mayer's descusses it at length - READ IT

Sep 24 '05 #4

P: n/a
"vsgdp" <sp**@nospam.com> wrote in message
news:LiiZe.15986$mH.13288@fed1read07...
| I have an abstract class A:
|
| class A
| {
| public:
| A(){ f(); }
| virtual void f() = 0;
| };
|
| class B : public A
| {
| public:
| void f() {...}
| };
|
| B b;
|
| However, the compiler does not like me calling f in the constructor of
A.
| But this seems like it should be okay since A is pure virtual, f will
be
| given in derived classes and exist.
|

Consider that A's ctor is invoked and completed before B's ctor is
finally processed to completion. The vtable is available but unpopulated
during A's construction.

Recall the sequence of construction / destruction for an instance of B:

<- B's ctor is actually invoked here, vtable created but unpopulated
A()
<- populated vtable (at last)
B() // B's ctor is processed to completion

.... <- valid instance of B

~B()
~A()

If you call f() in B's ctor, the issue disappears since a populated
vtable is available for this instance of B.

#include <iostream>

class A
{
public:
A(){ std::cout << "A()\n"; }
virtual ~A(){ std::cout << "~A()\n"; }
virtual void f() = 0;
};

class B : public A
{
public:
B()
{
std::cout << "B()\n";
f();
}
~B(){ std::cout << "~B()\n"; }
void f() { std::cout << "B::f()\n"; }
};

int main()
{
B b;

return 0;
}

/*
A()
B()
B::f()
~B()
~A()
*/

Additionally, in C++ you could even have implemented the pure virtual
A::f() and called it from B::f().


Sep 24 '05 #5

P: n/a

Mayer's descusses it at length - READ IT


Do you mean Meyers? And do you know which tip # it is? I have his 3
effective books.
Sep 24 '05 #6

P: n/a
On Sat, 24 Sep 2005 13:04:52 -0700, "vsgdp" <sp**@nospam.com> wrote:
However, the compiler does not like me calling f in the constructor of A.
But this seems like it should be okay since A is pure virtual, f will be
given in derived classes and exist.


http://www.parashift.com/c++-faq-lit....html#faq-10.7
Sep 25 '05 #7

P: n/a

However, the compiler does not like me calling f in the constructor of A.
But this seems like it should be okay since A is pure virtual, f will be
given in derived classes and exist.


The standard specifically says this is undefined behavior (virtual call
to pure virtual constructor during contruction or destruction).
Sep 25 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.