in Education by
I have defined an interface in C++, i.e. a class containing only pure virtual functions. I want to explicitly forbid users of the interface to delete the object through a pointer to the interface, so I declared a protected and non-virtual destructor for the interface, something like: class ITest{ public: virtual void doSomething() = 0; protected: ~ITest(){} }; void someFunction(ITest * test){ test->doSomething(); // ok // deleting object is not allowed // delete test; } The GNU compiler gives me a warning saying: class 'ITest' has virtual functions but non-virtual destructor Once the destructor is protected, what is the difference in having it virtual or non-virtual? Do you think this warning can be safely ignored or silenced? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
It's more or less a bug in the compiler. Note that in more recent versions of the compiler this warning does not get thrown (at least in 4.3 it doesn't). Having the destructor be protected and non-virtual is completely legitimate in your case. See here for an excellent article by Herb Sutter on the subject. From the article: Guideline #4: A base class destructor should be either public and virtual, or protected and nonvirtual.

Related questions

0 votes
    I have defined an interface in C++, i.e. a class containing only pure virtual functions. I want ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    I have defined an interface in C++, i.e. a class containing only pure virtual functions. I want ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    ________ function is usually used inside another function and throws a warning whenever a particular package is not ... R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    It is common practice to declare stack variables (variables allocated on the excecuting stack rather than dynamically ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I am trying to understand the usage of private const in the class. My understanding is that private const is used to make ... (void) { int const MyExample::x = 3; std::cout...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 10, 2022 in Education by JackTerrance
0 votes
    When compiling boost filesystem (1_46_1) with Intel 12 Release 4, and Visual Studio 10, I get this error ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    What does a compiler do? It translates source code into activities (which may comprise groups of machine instructions) ... only enables the programmer to edit the program files....
asked May 1, 2021 in Education by JackTerrance
0 votes
    Imagine a function which dynamically adds attributes to an object using setattr. The reason for doing so is that I want ... , 'some/attached/device') dev.some.subtree.element = 5...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    This is my pretty simple C++/CMake .travis.yml env: global: # The next declaration is the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I am coding a Gameboy Emulator, and for the CPU's instructions I use this struct here (in cpp.hpp ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
...