Click here to Skip to main content
Licence 
First Posted 5 Apr 2000
Views 51,918
Bookmarked 16 times

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

By | 5 Apr 2000 | Article
When inheriting from a virtual base class the access specifier for the destructor is ignored

Introduction

This bug appears in VC6 SP3, and possibly earlier versions as well.

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

The offending code

#include "stdafx.h"

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

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

int main(int argc, char* argv[])
{
    B b;
    return 0;
}

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Roger Onslow



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralVC++ compiler bug Pinmemberisheu2:28 26 Aug '08  
QuestionShould be virtual? PinmemberAlexMarbus2:50 10 May '01  
AnswerRe: Should be virtual? PinmemberAnonymous6:09 10 May '01  
GeneralBeside the point PinsussRoger Onslow15:45 10 Apr '00  
GeneralC++ Compiler bug PinsussDave Lorde0:05 10 Apr '00  
GeneralC++ compiler BUG PinsussAlex Turc1:25 6 Apr '00  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 6 Apr 2000
Article Copyright 2000 by Roger Onslow
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid