Can someone explain why a call to a virtual function within a
constructor is illegal?
The class can't be instantiated because functions are abstract, so
whatever extends it implements them which means they should be
implemented when the constructor for the base is called.
Is it because the base gets constructed before the rest of the object
and the vftable doesn't exist?
I think something like this works in Java.
class AbstractBase
{
public:
AbstractBase();
virtual ~AbstractBase() { }
virtual void disable() = 0;
virtual void enable() = 0;
virtual bool enabled() = 0;
};
AbstractBase::A bstractBase()
{
disable();
}
--Paul 9 1553
Paul Bilnoski wrote: Can someone explain why a call to a virtual function within a constructor is illegal?
It's not.
The class can't be instantiated because functions are abstract, so whatever extends it implements them which means they should be implemented when the constructor for the base is called. Is it because the base gets constructed before the rest of the object and the vftable doesn't exist? I think something like this works in Java.
Yes, Java allows constructors to call virtual functions on the derived
class, which has not been constructed at the time that the function is
called. I suppose there's a sense in which you could say that it
"works", but it can easily lead to surprises. class AbstractBase { public: AbstractBase(); virtual ~AbstractBase() { }
virtual void disable() = 0; virtual void enable() = 0; virtual bool enabled() = 0; };
AbstractBase::A bstractBase() { disable(); }
If you provide a definition for AbstractBase::d isable it will be called.
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com)
Paul Bilnoski wrote: Can someone explain why a call to a virtual function within a constructor is illegal?
Because the virtual function call depends on the type of object, and the
derived object is not yet constructed. In the example of your message, the
function invoked does not exist (conceptually, it can be a function that
aborts with a message such "pure virtual function invoked", for example).
--
Salu2
Pete Becker wrote: Paul Bilnoski wrote:
Can someone explain why a call to a virtual function within a constructor is illegal?
It's not.
My G++ and MSVC both gave errors for it. It's looking for a definition
that shouldn't exist because it's abstract. The class can't be instantiated because functions are abstract, so whatever extends it implements them which means they should be implemented when the constructor for the base is called. Is it because the base gets constructed before the rest of the object and the vftable doesn't exist? I think something like this works in Java.
Yes, Java allows constructors to call virtual functions on the derived class, which has not been constructed at the time that the function is called. I suppose there's a sense in which you could say that it "works", but it can easily lead to surprises.
class AbstractBase { public: AbstractBase(); virtual ~AbstractBase() { }
virtual void disable() = 0; virtual void enable() = 0; virtual bool enabled() = 0; };
AbstractBase::A bstractBase() { disable(); }
If you provide a definition for AbstractBase::d isable it will be called.
I want the extended classes to provide the definition for it. I could
put an empty body here, but then extensions have to know to override
because it's no longer pure virtual.
Paul Bilnoski wrote: My G++ and MSVC both gave errors for it. It's looking for a definition that shouldn't exist because it's abstract.
It doesn't exist because you didn't write it. In general you don't have
to provide a definition for a pure virtual function, but if you call it,
of course you have to define it. I want the extended classes to provide the definition for it. I could put an empty body here, but then extensions have to know to override because it's no longer pure virtual.
If it's marked with "=0" it's pure virtual. Doesn't matter whether you
provide a definition.
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com)
Pete Becker wrote: If it's marked with "=0" it's pure virtual. Doesn't matter whether you provide a definition.
It's undefined behavior to make a virtual call to a pure virtual
function during constructor (doesn't matter if it's defined or not).
Ron Natalie wrote: Pete Becker wrote:
If it's marked with "=0" it's pure virtual. Doesn't matter whether you provide a definition.
It's undefined behavior to make a virtual call to a pure virtual function during constructor (doesn't matter if it's defined or not).
Good point. Still, it doesn't affect what I just said. <g>
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com)
Pete Becker wrote: Paul Bilnoski wrote:
My G++ and MSVC both gave errors for it. It's looking for a definition that shouldn't exist because it's abstract.
It doesn't exist because you didn't write it. In general you don't have to provide a definition for a pure virtual function, but if you call it, of course you have to define it.
Why can't the compiler realize that there will be a definition for it at
runtime in a class that doesn't exist yet?
I just think it should compile the abstract base and just link to the
appropriate implementation of the virtual function, not the one defined
locally. I want the extended classes to provide the definition for it. I could put an empty body here, but then extensions have to know to override because it's no longer pure virtual.
If it's marked with "=0" it's pure virtual. Doesn't matter whether you provide a definition.
Paul Bilnoski wrote: Why can't the compiler realize that there will be a definition for it at runtime in a class that doesn't exist yet? I just think it should compile the abstract base and just link to the appropriate implementation of the virtual function, not the one defined locally.
When you call a function from a constructor or a destructor the compiler
does not dispatch virtually. The derived portion of the class has not
been constructed, so calling functions that belong to it is risky.
* Paul Bilnoski: My G++ and MSVC both gave errors for it. It's looking for a definition that shouldn't exist because it's abstract.
You're talking about a pure virtual function.
For a direct call the compiler knows there's no implementation when the call
is executed, so it can detect this (I'm not sure if it's required to).
For an indirect call via some other member function, it can in general not
detect the error -- even in principle.
I want the extended classes to provide the definition for it. I could put an empty body here, but then extensions have to know to override because it's no longer pure virtual.
For a newbie this typically indicates a design error.
However, technically it is sometimes needed.
So, the technical how-to is a FAQ:
(I'm not sure what Google has done, but the C++ FAQ main site is no longer
in the first page of results, and for a popular C++ GUI library its main web
page isn't any longer in the results at all: first Google screw up their
usenet interface, and now, it's evidently the web search engine)
<url:
http://www.parashift.c om/c++-faq-lite/strange-inheritance.htm l#faq-23.3>
<url:
http://www.parashift.c om/c++-faq-lite/strange-inheritance.htm l#faq-23.4>
--
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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: DarkSpy |
last post by:
code here:
struct A
{
int _i, _j;
A() { cout<<"call default ctor"<<endl; }
A(int i, int j) : _i(i), _j(j) { cout<<"call paramater ctor"<<endl;
}
};
|
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;
|
by: Derek |
last post by:
The following simple program causes a runtime error
(pure virtual method called):
struct Base
{
Base() { init(); }
void init() { badIdea(); }
virtual void badIdea() = 0;
};
|
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.
|
by: Prawit Chaivong |
last post by:
Hi All,
There is code here.
------------------------------------------------------------------
class Base{
public:
Base(){}
virtual ~Base(){}
private:
int a;
| |
by: Oleksii |
last post by:
Hello,
I'm rather new to the advanced topics, therefore I cannot explain the
following myself. Could anyone give me a hint on this one?
I'm trying to avoid link-time dependencies on (a test version of)
certain classes. In other words I don't want to link stuff that I don't
use. One thing that worked for me was replacing instance members
(MyClass myClass) within a class with auto_ptr (auto_ptr<MyClass>
myClass) and initializing it with...
|
by: vsgdp |
last post by:
I have an abstract class A:
class A
{
public:
A(){ f(); }
virtual void f() = 0;
};
class B : public A
|
by: Heinz Ketchup |
last post by:
Hello,
I'm looking to bounce ideas off of anyone, since mainly the idea of using
Multiple Virtual Inheritance seems rather nutty. I chalk it up to my lack
of C++ Experience.
Here is my scenario...
I have 5 Derived Classes
I have 3 Base Classes
|
by: Dennis Jones |
last post by:
Hello,
I have a couple of classes that look something like this:
class RecordBase
{
};
class RecordDerived : public RecordBase
{
|
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...
|
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,...
| |
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |