Solved

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

Posted on 2000-03-14
7
252 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need to retreive data from .TFS file exported by DVR 8 200
difference between String.subString() and String.subSequence() 6 200
fix34  challenge 9 121
changeXy challenge 13 82
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

810 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