473,687 Members | 3,340 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can pure virtual function be called in base class constructor?

Hi,

A pure function is called in the base function constructor. It generate
a run time error: "pure virtual method called".

My problem is that class A have some derived classes. I want A's
constructor change its behaviour accounting to the derived class.

I tried to make A::fun() not pure virtual but virtual. It doesn't
generate any error. But A::fun() is called in A's construction, while I
want B::fun() be called. I just don't want to define a default virtual
function.

I'm wondering if there is any work around to solve this problem.

Best wishes,
Peng
#include <iostream>

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

class B : public A{
public:
virtual void fun(){
std::cout << __PRETTY_FUNCTI ON__ << std::endl;
}
};

int main(int argc, char *argv[])
{
B b;
}

Oct 14 '05 #1
10 2974
Pe*******@gmail .com wrote:
A pure function is called in the base function constructor. It
generate a run time error: "pure virtual method called".

My problem is that class A have some derived classes. I want A's
constructor change its behaviour accounting to the derived class.

I tried to make A::fun() not pure virtual but virtual. It doesn't
generate any error. But A::fun() is called in A's construction, while
I want B::fun() be called. I just don't want to define a default
virtual function.
What you're trying to accomplish goes against a very basic principle:
member functions shall not be called for an object whose construction
hasn't completed. If you're trying to call B::fun from A::A, the B
object hasn't finished constructing. Java is notoriously bad about
this particular behaviour and it is a constant cause of trouble.
I'm wondering if there is any work around to solve this problem.


A work-around is a separate (maybe virtual) "initialise " member
function, which will be called by the _user_ of your A or B class,
after creating the object. If you want that sequence (construct,
then initialise) to be executed without client's knowing about it,
you need a factory (or several).

V
Oct 14 '05 #2
Pe*******@gmail .com wrote:
Hi,

A pure function is called in the base function constructor. It
generate a run time error: "pure virtual method called".

My problem is that class A have some derived classes. I want A's
constructor change its behaviour accounting to the derived class.

I tried to make A::fun() not pure virtual but virtual. It doesn't
generate any error. But A::fun() is called in A's construction, while
I want B::fun() be called. I just don't want to define a default
virtual function.

I'm wondering if there is any work around to solve this problem.


Assuming that you might be constructing any derived class and that you want
to call the most derived class's fun(), and only once, and you want to call
it from a constructor, the following illustrates a (not entirely
satisfactory) workaround:

class A
{
public:
A(int) { /* stuff */ }
virtual void fun() = 0;
};

class B : public A
{
public:
B() { fun();/* other stuff */ }
void fun();
protected:
B(int) { /* other stuff */ }
};

class C : public B
{
public:
C() : B(int) { fun();/* other stuff */ }
void fun();
protected:
C(int) : B(int) { /* other stuff */ }
};

class D : public C
{
public:
D() : C(int) { fun();/* other stuff */ }
void fun();
protected:
D(int) : C(int) { /* other stuff */ }
};

And so on...

DW
Oct 14 '05 #3
David White wrote:

class A
{
public:
A(int) { /* stuff */ }
virtual void fun() = 0;
};


[snip]

Sorry, rushed that a bit. Try this instead:

class A
{
public:
A() { /* stuff */ }
virtual void fun() = 0;
};

class B : public A
{
public:
B() { fun();/* other stuff */ }
void fun();
protected:
B(int) { /* other stuff */ }
};

class C : public B
{
public:
C() : B(0) { fun();/* other stuff */ }
void fun();
protected:
C(int) : B(0) { /* other stuff */ }
};

class D : public C
{
public:
D() : C(0) { fun();/* other stuff */ }
void fun();
protected:
D(int) : C(0) { /* other stuff */ }
};

And so on...

DW
Oct 14 '05 #4

Pe*******@gmail .com wrote:
Hi,

A pure function is called in the base function constructor. It generate
a run time error: "pure virtual method called".

My problem is that class A have some derived classes. I want A's
constructor change its behaviour accounting to the derived class.

I tried to make A::fun() not pure virtual but virtual. It doesn't
generate any error. But A::fun() is called in A's construction, while I
want B::fun() be called. I just don't want to define a default virtual
function. Yes, if you try to call B::fun(), you should define a explicit
constructor to invoke it. Since class B like you've wrote has no
constructor, compiler will sythesize a default construtor to invoke
A:A() for you. that wouldn't invoke B:fun() at all. Doing that is your
responsibility not compiler. I'm wondering if there is any work around to solve this problem.

Best wishes,
Peng
#include <iostream>

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

class B : public A{
public:
virtual void fun(){
std::cout << __PRETTY_FUNCTI ON__ << std::endl;
}
};

int main(int argc, char *argv[])
{
B b;
}


Oct 14 '05 #5
When a well respected expert such as Meyers includes item #9
in his Effectice C++, Third Edition, titled "Never call a virtual
function during construction or destruction", you have to have
to have a very good reasons to do it :). That book should be
read by every C++ programmer.

Sutter and Alexandrescu's C++ Coding Standards item #49 gives an
example of how to get around this issue by using an initialize
function.

Oct 14 '05 #6
* Pe*******@gmail .com:

My problem is that class A have some derived classes. I want A's
constructor change its behaviour accounting to the derived class.


That's FAQ item 23.4, e.g. at
<url: http://www.parashift.c om/c++-faq-lite/strange-inheritance.htm l#faq-23.4>

(I often answer this question because I argued hard to get that into the FAQ,
so it's sort of 50% "my" item even though the wording is Marshall's...)

(But do people point to the FAQ anyway? No, they either don't understand the
problem, perhaps because of hasty reading of the news article, like Peter, or
give for once in a million Bad Advice, like Victor (sorry, but it was really
bad) did this time, it's very rare but happens, also for me, or give bad
advice with an Appeal To Authority fallacy added on top, like the anonymous
AnonMail. I hope people will point more to the FAQ, both because it reduces
discussion that can muddy waters, and because it helps people find the FAQ.)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 14 '05 #7
* Victor Bazarov:
Pe*******@gmail .com wrote:
A pure function is called in the base function constructor. It
generate a run time error: "pure virtual method called".

My problem is that class A have some derived classes. I want A's
constructor change its behaviour accounting to the derived class.

I tried to make A::fun() not pure virtual but virtual. It doesn't
generate any error. But A::fun() is called in A's construction, while
I want B::fun() be called. I just don't want to define a default
virtual function.
What you're trying to accomplish goes against a very basic principle:
member functions shall not be called for an object whose construction
hasn't completed. If you're trying to call B::fun from A::A, the B
object hasn't finished constructing. Java is notoriously bad about
this particular behaviour and it is a constant cause of trouble.
I'm wondering if there is any work around to solve this problem.


A work-around is a separate (maybe virtual) "initialise " member
function, which will be called by the _user_ of your A or B class,
after creating the object.


Don't do that: it allows for zombie objects, double init and whatnot.

See FAQ item 23.4.

If you want that sequence (construct,
then initialise) to be executed without client's knowing about it,
you need a factory (or several).


Nope.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 14 '05 #8
In message <43************ ****@news.indiv idual.net>, Alf P. Steinbach
<al***@start.no > writes
* Pe*******@gmail .com:

My problem is that class A have some derived classes. I want A's
constructor change its behaviour accounting to the derived class.
That's FAQ item 23.4, e.g. at
<url: http://www.parashift.c om/c++-faq-lite/strange-inheritance.htm l#faq-23.4>


You mean the item that's moved to 23.5 (and 23.6) ? ;-)

http://www.parashift.com/c++-faq-lit....html#faq-23.5
http://www.parashift.com/c++-faq-lit....html#faq-23.6
(I often answer this question because I argued hard to get that into the FAQ,
so it's sort of 50% "my" item even though the wording is Marshall's...)

(But do people point to the FAQ anyway? No, they either don't understand the
problem, perhaps because of hasty reading of the news article, like Peter, or
give for once in a million Bad Advice, like Victor (sorry, but it was really
bad) did this time, it's very rare but happens, also for me, or give bad
advice with an Appeal To Authority fallacy added on top, like the anonymous
AnonMail.
.... or point to the wrong FAQ ;-(
I hope people will point more to the FAQ, both because it reduces
discussion that can muddy waters, and because it helps people find the FAQ.)


--
Richard Herring
Oct 18 '05 #9
* Richard Herring:
... or point to the wrong FAQ ;-(


The FAQ numbering changed after my posting. Don't be such a whiner. ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
13455
by: cppaddict | last post by:
Hi, I know that it is illegal in C++ to have a static pure virtual method, but it seems something like this would be useful when the following 2 conditions hold: 1. You know that every one of your Derived classes will need to implement some method, but implement it differently, and that the base class cannot implement it. This is where pure virtual comes in.
11
4356
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
4162
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 ]
10
7299
by: Martin Vorbrodt | last post by:
Example code in one of my books intrigues me: class B { public: B* Clone() const { B* p = DoClone(); assert(typeid(*p) == typeid(*this)); return p; }
6
3482
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
22
2394
by: ypjofficial | last post by:
Hello All, I have following doubt.. class abstractclass { public: abstractclass(){} virtual void method()=0; };
21
2562
by: sks | last post by:
Hi , could anyone explain me why definition to a pure virtual function is allowed ?
10
4789
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
14
2748
by: Jack | last post by:
Hi, I meet a question with it , I did not get clear the different betteen them, for example: #include <iostream>
0
8590
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
8528
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
9072
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8782
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8786
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
5807
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4547
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2964
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
3
1950
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.