Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > calling pure virtual in abstract class's constructor

Reply
Thread Tools

calling pure virtual in abstract class's constructor

 
 
vsgdp
Guest
Posts: n/a
 
      09-24-2005
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.


 
Reply With Quote
 
 
 
 
Axter
Guest
Posts: n/a
 
      09-24-2005
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.

 
Reply With Quote
 
 
 
 
Ian
Guest
Posts: n/a
 
      09-24-2005
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

 
Reply With Quote
 
Peter_Julian
Guest
Posts: n/a
 
      09-24-2005
"vsgdp" <(E-Mail Removed)> 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().




 
Reply With Quote
 
puzzlecracker
Guest
Posts: n/a
 
      09-24-2005

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

 
Reply With Quote
 
vsgdp
Guest
Posts: n/a
 
      09-24-2005

>
> 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.


 
Reply With Quote
 
Dave Rahardja
Guest
Posts: n/a
 
      09-25-2005
On Sat, 24 Sep 2005 13:04:52 -0700, "vsgdp" <(E-Mail Removed)> 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
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      09-25-2005

>
> 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).
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Abstract base class with pure virtual functions Arne Schmitz C++ 4 01-17-2007 06:16 PM
what is the difference between abstract class and pure abstract class? skishorev@yahoo.co.in C++ 4 05-17-2006 08:07 AM
paradox when constructor of an pure abstract base class called? ypjofficial@indiatimes.com C++ 22 01-24-2006 01:41 AM
abstract class and pure virtual cppsks C++ 6 11-05-2004 02:49 PM



Advertisments