Re: Overloaded Constructors

From: Cy Edmunds (cedmunds_at_spamless.rochester.rr.com)
Date: 03/25/04


Date: Thu, 25 Mar 2004 04:47:24 GMT


"Siemel Naran" <SiemelNaran@REMOVE.att.net> wrote in message
news:D8k8c.14644$tY6.427566@bgtnsc04-news.ops.worldnet.att.net...
> "Cy Edmunds" <cedmunds@spamless.rochester.rr.com> wrote in message
> news:Cnj8c.53506
> > "Siemel Naran" <SiemelNaran@REMOVE.att.net> wrote in message
>
> > > > > class User {
> > > > > public:
> > > > > ~User();
> > > > > User (string full_username);
> > > > > User (string user, string domain);
> > > > > User (const User&);
> > > > > User& operator=(const User&);
> > > > > private:
> > > > > struct Imp;
> > > > > std::auto_ptr<Imp> d_imp;
> > > > > };
>
> > So why did you use a std::auto_ptr at all?
>
> Good question. Just a style choice to make it clear to the reader of the
> header file that this class does in fact own the pointer, and it's not a
> just a pointer to an existing object.

The reader of your header file should try to ignore implementation details
and focus on your interface and documentation. However, real people don't
necessarily take that advice, and when they see std::auto_ptr may feel it
necessary to check over your code to make sure of the copy semantics.

> The boost::shared_ptr gives me
> reference counting for free, but if I don't need that feature I won't use
> it. Big deal -- I save about 4 bytes, whereas the size of Imp may be so
> large, that saving 4 bytes is nothing. Anyway, my style is to prefer the
> object that is minimal for my purpose.

In this case, that would be a pointer.

> In a big project I will adapt my
> style in order to have a single rule for all objects, as a uniform coding
> standard will make code easier to maintain down the road.
>

Your design seems to be technically sound. However, when perusing a class
definition I find a pointer to be scary but a std::auto_ptr to be even
scarier. Since you seemed concerned about how your class "reads" (good for
you!) you might consider eliminating this unneccesary complication.

-- 
Cy
http://home.rochester.rr.com/cyhome/


Relevant Pages

  • Re: Overloaded Constructors
    ... Just a style choice to make it clear to the reader of the ... header file that this class does in fact own the pointer, ... just a pointer to an existing object. ...
    (comp.lang.cpp)
  • Re: Overloaded Constructors
    ... Just a style choice to make it clear to the reader of the ... >>header file that this class does in fact own the pointer, ... >>just a pointer to an existing object. ...
    (comp.lang.cpp)
  • Re: [TOMOYO #10 (linux-next) 7/8] File operation restriction part.
    ... whenever the reader fetches an element in a list. ... You will also need the ACCESS_ONCEon the pointer fetch in order ... Macros to force memory ordering. ... That will indeed make the assignment to "a" happen before the ...
    (Linux-Kernel)
  • Pointers, DLL Header files and lots of exceptions?
    ... I have the C header file that I used Dr. Bob's ... append line to the memo box. ... I've spent many hours investigating this on-line and reading docs on pointer ...
    (comp.lang.pascal.delphi.misc)
  • Re: Editors, royalty, and people with tapeworm
    ... person plural in all my lab reports, even when I had performed the ... trying to give the reader a sense that we are working together. ... name) costs us one pointer in space, ... pointer that can aim at external strings */ ...
    (alt.usage.english)