- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
the code you provided does not follow the standard, the compiler is right:
A virtual base class needs to be initialized in (every!) most derived class's constructor list.
Means: Both classes B & C need to construct the possible sub-object of class A, even though there will be only one reasonable way (via class C).
Rationale:
C++ does not know the concept of an interface. You can use classes with pure virtual methods to achieve a similar result. However, for C++ it's still a sub-object which needs to be constructed.
You already proposed a possible solution. Another way would be to create a default constructor for class A and assert during run-time that it's never been used. AFAIK it cannot be asserted during compilation time because of the missing interface concept (distinction between ordinary class/object and interface not possible). A trivial implementation could look like this:
[bash]#include
Best regards,
Georg Zitzlsberger
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Means: Both classes B & C need to construct the possible sub-object of class A, even though there will be only one reasonable way (via class C).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Asked and answered athttp://stackoverflow.com/a/11693977/1558356. In essence: the code does not follow the standard, but the compiler is wrong. The problem is not the missing A::A(), the problem is missing B::B(). The latter must be deleted (not generated) according to 12.1/5 "A defaulted default constructor for class X is defined as deleted if [...] any [...] virtual base class [...] has class type M [...] and [...] M has no default constructor [...]."
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Asked and answered athttp://stackoverflow.com/a/11693977/1558356. In essence: the code does not follow the standard,
but the compiler is wrong. The problem is not the missing A::A(), the problem is missing B::B(). The latter must be deleted
(not generated) according to 12.1/5 "A defaulted default constructor for class X is defined as deleted if [...] any [...] virtual
base class [...] has class type M [...] and [...] M has no default constructor [...]."
Hi Vladimir,I have a question:
Are you doing R&D on how to breaksome C++ compiler(s)?
I would like to note that if a project needs to beVERYportablea developerneeds to follow more than 20-year-old C++ standards.
In that case a complex C++ projectcould be compiled even with a Turbo C++compiler v3.x ( an example of a legacy technolody ).
A C++ constructor is needed to construct an object. It is an Essense of OOP.If you don'twant to call a constructor,
in case of a high-performance applications of your codes in a Real-Time environment, than you could create
an instance of the object with a CRT 'malloc' function ( but your class needs to be a simple one / there are limitations / etc).
Here is an example:
...
class A
{
public:
A(){};
~A(){};
void Init(){ /* some initialization on demand when it is needed*/ };
};
...
A *pA[ 65536 ] = { NULL };
for( int i=0; i<65536; i++)
{
pA=( A * )malloc( sizeof( A ) );
}
...
pA[12345]->Init(); // Initialization of object's members some time later
...
That trick is used in a high-performance Strassen's Heap-Based algorithm for matrix multiplication optimized for embedded platforms.
Best regards,
Sergey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It is never a bad thing when a person tries to verify some standards! Thank you for the response.
Best regards,
Sergey

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page