freepooma-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

signed == signed - sizeof(...), and other warnings... (non-POOMA C++ qu


From: James Crotinger
Subject: signed == signed - sizeof(...), and other warnings... (non-POOMA C++ questions)
Date: Tue, 1 May 2001 13:07:07 -0700

Hey Mark. Just a couple of C++ (non-Pooma related) questions. I was
somewhat surprised that -Wall flagged the following code with a
warning:

  void bar()
  {
    int a, b;
    (void)(a == b - sizeof(int));
  }

Compiling this gives:

  $ g++ -Wall -c foo.cpp
  foo.cpp: In function `void bar()':
  foo.cpp:4: warning: comparison between signed and unsigned

Does this mean operator-(int,unsigned int) returns unsigned? That seem
bizarre to say the least.

Also, on the subject of warnings, awhile back I wrote about some
destructor warnings and you replied:

    James> What it says is true and is by design. This particular
    James> class (and many like it) is reference counted and should
    James> never be deleted by an explicit call to delete. Rather,
    James> when all the references are released, the release function
    James> calls "delete this". This seems like a common enough
    James> idiom. Any idea why there is a warning?

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.

  Jim


reply via email to

[Prev in Thread] Current Thread [Next in Thread]