473,786 Members | 2,737 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

calling pure virtual in abstract class's constructor

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

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
"vsgdp" <sp**@nospam.co m> wrote in message
news:LiiZe.1598 6$mH.13288@fed1 read07...
| 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

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
On Sat, 24 Sep 2005 13:04:52 -0700, "vsgdp" <sp**@nospam.co m> 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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
46595
by: Dario | last post by:
This is a technical C++ post regarding the Microsoft runtime error R6025 Pure Virtual Function Call that sometime occurs in programs compiled with Microsoft Visual C++ 6.0. Please consider the following simple illegal C++ program: class Listener { public: virtual void onEvent(int n) = 0;
11
4369
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining it inline. Like this.
37
4181
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
22
2414
by: ypjofficial | last post by:
Hello All, I have following doubt.. class abstractclass { public: abstractclass(){} virtual void method()=0; };
4
3926
by: skishorev | last post by:
and what is object delagation, and how it can implemented?
10
4808
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a pure virtual function body
4
1873
by: Eric | last post by:
I was wondering what people thought about the information found at: http://g.oswego.edu/dl/mood/C++AsIDL.html Specifically, I am interested in the following recommendation: ---- Since interface classes cannot be directly instantiated, yet serve as virtual base classes for implementations, the constructors should take no arguments and should be listed as protected. Also, for similar
6
4033
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it would be useful to have /some/ data defined in such an abstract class for reasons of forming a common block of data existing in or used by all descendant classes (see example below.) In such a case where a common block of data *always* exists,...
10
2099
by: Rahul | last post by:
Hi, I tried to create a abstract class by having a non-virtual member function as pure, but i got a compilation error saying "only virtual member functions can be pure"... I'm trying to think the reason behind this restriction... i just want to want a base class to be abstract so as to avoid object slicing into the base type from derived type, and in my case base class doesn't need to have a virtual function.
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9496
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9961
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8989
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.