Const member initialization list question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Howard

    #16
    Re: Const member initialization list question


    "Wolfram Humann" <whumann@gmx.de > wrote in message
    news:oprvmzg6x0 1udsjr@netnews. ..[color=blue]
    > On 16 Sep 2003 19:00:07 GMT, Howard <alicebt@hotmai l.com> wrote:
    >[color=green]
    > > If you're just inquiring for information about an existing book,
    > > then simply return a reference to one [...], or else return a
    > > pointer instead of a reference, and make it nil when the
    > > specified book doesn't exist.[/color]
    >
    > I see your point and I agree that this is a clean solution. Two remarks:
    >
    > 1. I still could not find any statement in the standard that
    > references/pointers to map entries will always remain valid when the map
    > grows/shrinks. As I said, I don't think the current implementation of a
    > map will reallocate memory (like e.g. the vector does) but I don't want to
    > rely on this unless the standard defines that it's illegal for a map to
    > reallocate memory for its entries.
    >
    > 2. Using references or pointes may be more logical than creating
    > "redundant" objects but at least for my current project I won't be able to
    > do this. My code has to remain consistent with existing code.
    >
    > Wolfram[/color]

    Well, then just write a proper copy-constructor. Let findByTitle return a
    const reference (throwing an exception if not found), and pass its result as
    the parameter for the constructor, like this:

    try
    {
    BOOK aBook(findByTit le("The Hobbit"));
    }
    catch(...)
    {
    //...error?
    }

    or

    try
    {
    const BOOK& refBook = BOOK::findByTit le("The Hobbit");
    BOOK* pPook = new BOOK(refBook);
    }
    catch(...)
    {
    // ...error?
    }

    -Howard




    Comment

    Working...