Solved

FYI: BUG in Visual C++ access for destructors with virtual base classes

Posted on 2000-03-14
7
244 Views
Last Modified: 2013-11-20
FYI: BUG in Visual C++ access for destructors with virtual base classes

In VC 6 SP3 (and probably earlier, don't know).

When inheriting from a virtual base class the access specifier for the destructor is ignored

To demonstrate, if the word 'virtual' is commented out as shown below in the declaration of class B, this program correctly produces the error message
 xxx.cpp(28) : error C2248: 'B::~B' : cannot
 access protected member declared in class 'B'
 xxx.cpp(23) : see declaration of 'B::~B'

if the word 'virtual' is un-commented then the program builds without error messages despite the destructor being declared private

#include "stdafx.h"

class A {
protected:
    ~A() {}
};

class B : virtual public A {
private:
    ~B() {}
};

int main(int argc, char* argv[])
{
    B b;
    return 0;
}
0
Comment
Question by:RONSLOW
7 Comments
 
LVL 10

Author Comment

by:RONSLOW
ID: 2617863
As a bit of history, I am implementing some smart pointers and want to ensure that the smart-pointer target object cannot be created locally on the stack, nor do I want to be able to explicitly call delete on a pointer to such an object.  Hence I want the destructor to be private.  However, this fails (as shown above) when I am inheriting from a virtual base class.
0
 
LVL 3

Expert Comment

by:SamHobbs
ID: 2618850
Where is that documented?
0
 
LVL 12

Expert Comment

by:migel
ID: 2618894
Hi!
why virtual inheritance?
may be you want pure virtual base class? or one base class can be multy inherited in the derived one in your hieararchy?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 10

Author Comment

by:RONSLOW
ID: 2619387
migel .. I do not havea problem with virtual inheritance.  In my real world application it IS required (diamond-shaped hierachy).

However, this sample is the minimum to reproduce the problem.

The problem is that, because the destructor for B is private, it should not be able to be called, even IMPLICITLY when an object of class B goes out of scope (also cannot explicitly delete a B* pointer).

That is the desired behaviour for my app, as I do NOT want to be able to have local variables of my class, nor allow direct calls to delete pointers (the object deletes itself so a private destructor works ok).  I want to see the expected compiler error if I attempt to do this in my code.

But when there is a virtual base class somewhere in the hierachy, the 'privacy' of the destructor is ignored.  BUG!!!

0
 
LVL 12

Expert Comment

by:migel
ID: 2619436
ok I see.
BUG is BUG :-)
you also can declare your default constructor (and any other) as protected to avoid stack object :-)
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 0 total points
ID: 2619455
migel:

If I decalre the contructor as private, then I canno create objects using new.

I know I could write a static member function to call new for me, but I'd rather not have to do that.

eg.

class B {
private:
  B() {}
public:
  static B* New() {
    return new B;
  }
};

then calling
  B* p = B::New();
would work and
  B* p = new B;
would fail.

But, despite other techniques (which I am using as a work-around) the bug on the desturctor access is still a BUG !!
0
 
LVL 5

Expert Comment

by:ianB
ID: 2663908
I have closed this question with RONSLOW's comment since we may want to keep the question in the arhives.

IAn
Community Support @ Experts Exchange
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now