Re: Abstract and concrete classes in c++

From: Dave Moore (dtmoore_at_email.unc.edu)
Date: 02/01/05


Date: Tue, 1 Feb 2005 19:16:32 +0100


"Jerry Coffin" <jcoffin@taeus.com> wrote in message
news:1107276123.375047.196240@f14g2000cwb.googlegroups.com...
> Dave Moore wrote:
>
> [ ... ]
>
> > No, this is not true ... concrete classes can certainly be used as
> base
> > classes in a properly designed C++ program.
>
> How do you implement operator= safely for this situation?
>

Well, if your base class has a copy assignment that can throw, then you
cannot write a copy-assignment operator that satisfies the "strong
guarantee" wrt exception safety. There is of course also a item in Scott
Meyers "More Effective C++" entitled "Make all non-leaf classes abstract",
which refutes my claim. So, I probably over-qualified my initial statement
by saying "properly designed C++ program", because strong exception safety
might be a requirement for "proper design" in many cases.

The fact is that I myself do not require the strong guarantee for my own
applications (scientific, numerical programs for which I am currently the
only user), and so I don't always think in terms of strong exception safety.
I will consider myself reprimanded 8*).

Actually this brings up a question ... If all of the data members in a class
(including any base classes) are statically allocated objects of built-in
type (i.e. no pointers or ctors), then does the implicitly generated copy
assignment operator implicitly provide the strong guarantee? Or even
better, the no-throw guarantee? It seems like it should, because the "only
thing that can go wrong" during construction in such a case is a stack
overflow (in which case we are dead anyway), or am I oversimplifying things.

Dave Moore



Relevant Pages

  • Re: WaitForSingleObject() will not deadlock
    ... Even though the assignment of value 2 is guaranteed ... compiler's guarantee), without the specific guarantee from the thread ... the *compiler* does not guarantee this. ... Any data written to memory after the new thread is ...
    (microsoft.public.vc.mfc)
  • Re: Template-based implementation of Sutters exception-safe operator= idiom
    ... For those who do need the strong guarantee at a higher level, ... Take your example where a client wants an assignment followed by ... template <typename C, typename T> ... C temp(src); ...
    (comp.lang.cpp)
  • Virtual Method Question
    ... "Because ToString() is a virtual method in the base class Object, ... the overide keyword was used in the derived method's declaration. ... How does NOT using virtual NOT guarantee the method's availability? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Constructor
    ... MyClass object = new MyClass; ... Do I have a guarantee that object is not null when I call doStuff? ... For that read the JLS on "definite assignment". ... Foo foo; // I hate including 'Class' or 'object' in identifiers ...
    (comp.lang.java.programmer)
  • Re: Constructor
    ... MyClass object = new MyClass; ... Do I have a guarantee that object is not null when I call doStuff? ... For that read the JLS on "definite assignment". ... Foo foo; // I hate including 'Class' or 'object' in identifiers ...
    (comp.lang.java.programmer)