Solved

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

Posted on 2000-03-14
7
253 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
notReplace  challenge 53 141
EvenOdd challenge 10 125
wordappend challenge 8 199
how to split multiple lines delimiter : 8 109
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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…
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

808 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