Solved

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

Posted on 2000-03-14
7
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
lucky13 challenge 11 167
MaxSpan challenge 9 139
ASP.NET C# MessageBox.Show Showing a modal dialog box or form when the application ... 2 197
Making an alias 7 121
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

738 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