[pooma-dev] signed == signed - sizeof(...), and other warnings... (non-POOMA C++ questions)
Jeffrey Oldham
oldham at codesourcery.com
Tue May 1 20:34:54 UTC 2001
On Tue, May 01, 2001 at 01:07:07PM -0700, James Crotinger wrote:
>
> Mark> Normally, that warning fires on code like:
> Mark>
> Mark> class C {
> Mark> ~C ();
> Mark> };
> Mark>
> Mark> Such a class isn't very useful, since you can't create an object of
> Mark> this type. (Why? Because when you construct an object, the
> Mark> destructor has to be accessible at the point of construction; so
> Mark> sayeth the standard.)
>
> GCC flags "C c;" as an error, but is happy with "C *pc = new C;". This
> is good or my code below wouldn't work, but I'm not sure I see the
> difference (for this simple case).
>
> Mark> However, the case you bring up (an overloaded operator `delete')
> Mark> should probably be an exception to the warning.
>
> Actually, I'm not overloading delete. Here's a short example code:
>
> class IA { public: virtual int release() = 0; };
>
> class A : public IA
> {
> public:
> A() : count_m(0) { }
>
> int release()
> {
> --count_m;
> if (count_m == 0) { delete this; return 0; }
> return count_m;
> }
>
> private:
> ~A() { }
> int count_m;
> };
>
> When I compile this:
>
> $ g++ -Wall -c bar.cpp
> bar.cpp:17: warning: \
> `class A' only defines a private destructor and has no friends
> bar.cpp:17: warning: \
> `class A' has virtual functions but non-virtual destructor
>
> You are right in that I can't do:
>
> int main()
> {
> A a;
> return 0;
> }
>
> But that is actually one of the reasons that I want to have the
> destructor private - I want to disallow this. You can however do:
>
> int main()
> {
> IA *pa = new A;
> pa->release();
> return 0;
> }
>
> This compiles and works just fine. The proper destructor is called
> since it is called through the virtual function release(). But I still
> get the warning.
To eliminate the warnings,
1) add "virtual" in front of the destructor and
2) use the command line option -Wno-ctor-dtor-privacy when compiling.
Thanks,
Jeffrey D. Oldham
oldham at codesourcery.com
More information about the pooma-dev
mailing list