String operations in member initialisation list

Discussion in 'C++' started by Samuele Armondi, Jun 25, 2003.

  1. Hi,
    I'm writing some exception class for a project, and I've got a class called
    BasicException that looks like this:
    class BasicException
    {
    private:
    std::string Description;
    int Number;

    public:
    BasicException(std::string, int);
    friend std::eek:stream& operator << (std::eek:stream&, BasicException&);
    };
    where the string and the int in the constructor initialize the description
    and the error number respectively.
    I also have a derived class called InvalidCommand:
    class InvalidCommand : public Exceptions::BasicException
    {
    private:
    std::string tmp;
    public:
    InvalidCommand(std::string s) : tmp((std::string("The command ") + s +
    std::string(" is invalid."))), BasicException(tmp, 3) {};
    };
    the string s is the command name, which is then incorporated into the error
    description. This compiles fine in Dev-Cpp (which I always thought had
    problems handling string types) but it produces a raft of stupid errors in
    VC++ (such as template already defined in <algorithm> and other errors of
    the sort.
    Is it correct to use the string like I'm using it in the constructor of
    InvalidCommand? If it is not, can someone think of another way of doing it?
    Thanks in advance,
    S. Armondi

    --
    To contact me by email, remove _NOSPAM_ from the address.


    --
    To contact me by email, remove _NOSPAM_ from the address.
    Samuele Armondi, Jun 25, 2003
    #1
    1. Advertising

  2. Samuele Armondi escribió:

    > class InvalidCommand : public Exceptions::BasicException
    > {
    > private:
    > std::string tmp;
    > public:
    > InvalidCommand(std::string s) : tmp((std::string("The command ") + s +
    > std::string(" is invalid."))), BasicException(tmp, 3) {};
    > };


    BasicException is constructed before tmp, the order used in the
    initializtion list is ignored (some compilers put a warning). And you
    probably not need it tmp at all.

    Regards.
    =?iso-8859-1?Q?Juli=E1n?= Albo, Jun 25, 2003
    #2
    1. Advertising

  3. "Samuele Armondi" <> wrote...
    > Hi,
    > I'm writing some exception class for a project, and I've got a class

    called
    > BasicException that looks like this:
    > class BasicException
    > {
    > private:
    > std::string Description;
    > int Number;
    >
    > public:
    > BasicException(std::string, int);
    > friend std::eek:stream& operator << (std::eek:stream&, BasicException&);
    > };
    > where the string and the int in the constructor initialize the description
    > and the error number respectively.
    > I also have a derived class called InvalidCommand:
    > class InvalidCommand : public Exceptions::BasicException
    > {
    > private:
    > std::string tmp;
    > public:
    > InvalidCommand(std::string s) : tmp((std::string("The command ") + s +
    > std::string(" is invalid."))), BasicException(tmp, 3) {};
    > };
    > the string s is the command name, which is then incorporated into the

    error
    > description. This compiles fine in Dev-Cpp (which I always thought had
    > problems handling string types) but it produces a raft of stupid errors in
    > VC++ (such as template already defined in <algorithm> and other errors of
    > the sort.
    > Is it correct to use the string like I'm using it in the constructor of
    > InvalidCommand? If it is not, can someone think of another way of doing

    it?


    For you, the only way to do it right is

    InvalidCommand(const std::string& s) :
    BasicException(std::string("The command ") + s + " is invalid", 3),
    tmp(std::string("The command ") + s + " is invalid")
    {
    }

    The base classes are constructed before members, and member are
    constructed in the declaration order. The order in which you
    place them in the initialisation list does NOT matter. You relied
    on the 'tmp' being constructed before the base class. It is not.

    Victor
    Victor Bazarov, Jun 25, 2003
    #3
  4. "Victor Bazarov" <> wrote in message
    news:...
    > "Samuele Armondi" <> wrote...
    > > Hi,
    > > I'm writing some exception class for a project, and I've got a class

    > called
    > > BasicException that looks like this:
    > > class BasicException
    > > {
    > > private:
    > > std::string Description;
    > > int Number;
    > >
    > > public:
    > > BasicException(std::string, int);
    > > friend std::eek:stream& operator << (std::eek:stream&, BasicException&);
    > > };
    > > where the string and the int in the constructor initialize the

    description
    > > and the error number respectively.
    > > I also have a derived class called InvalidCommand:
    > > class InvalidCommand : public Exceptions::BasicException
    > > {
    > > private:
    > > std::string tmp;
    > > public:
    > > InvalidCommand(std::string s) : tmp((std::string("The command ") + s +
    > > std::string(" is invalid."))), BasicException(tmp, 3) {};
    > > };
    > > the string s is the command name, which is then incorporated into the

    > error
    > > description. This compiles fine in Dev-Cpp (which I always thought had
    > > problems handling string types) but it produces a raft of stupid errors

    in
    > > VC++ (such as template already defined in <algorithm> and other errors

    of
    > > the sort.
    > > Is it correct to use the string like I'm using it in the constructor of
    > > InvalidCommand? If it is not, can someone think of another way of doing

    > it?
    >
    >
    > For you, the only way to do it right is
    >
    > InvalidCommand(const std::string& s) :
    > BasicException(std::string("The command ") + s + " is invalid",

    3),
    > tmp(std::string("The command ") + s + " is invalid")
    > {
    > }
    >
    > The base classes are constructed before members, and member are
    > constructed in the declaration order. The order in which you
    > place them in the initialisation list does NOT matter. You relied
    > on the 'tmp' being constructed before the base class. It is not.
    >
    > Victor
    >
    >

    Thanks for the replies. After reading the post by john Harrison (calling
    member funcs from an initialiser list) i tried to following, which compiles
    in VC++:
    class InvalidCommand : public Exceptions::BasicException
    {
    private:
    std::string& MakeString(std::string& s)
    {
    s.insert(0, "The command ");
    s.append(" is invalid");
    return s;
    }
    public:
    InvalidCommand(std::string s) : BasicException(MakeString(s), 3) {};
    };
    I gather that since MakeString is not accessing any unitialised class
    members, the code is ok. Am I correct?
    Thanks in advance,
    S. Armondi
    Samuele Armondi, Jun 25, 2003
    #4
  5. "Samuele Armondi" <> wrote...
    > "Victor Bazarov" <> wrote in message
    > news:...
    > > "Samuele Armondi" <> wrote...
    > > > Hi,
    > > > I'm writing some exception class for a project, and I've got a class

    > > called
    > > > BasicException that looks like this:
    > > > class BasicException
    > > > {
    > > > private:
    > > > std::string Description;
    > > > int Number;
    > > >
    > > > public:
    > > > BasicException(std::string, int);
    > > > friend std::eek:stream& operator << (std::eek:stream&, BasicException&);
    > > > };
    > > > where the string and the int in the constructor initialize the

    > description
    > > > and the error number respectively.
    > > > I also have a derived class called InvalidCommand:
    > > > class InvalidCommand : public Exceptions::BasicException
    > > > {
    > > > private:
    > > > std::string tmp;
    > > > public:
    > > > InvalidCommand(std::string s) : tmp((std::string("The command ") + s

    +
    > > > std::string(" is invalid."))), BasicException(tmp, 3) {};
    > > > };
    > > > the string s is the command name, which is then incorporated into the

    > > error
    > > > description. This compiles fine in Dev-Cpp (which I always thought had
    > > > problems handling string types) but it produces a raft of stupid

    errors
    > in
    > > > VC++ (such as template already defined in <algorithm> and other errors

    > of
    > > > the sort.
    > > > Is it correct to use the string like I'm using it in the constructor

    of
    > > > InvalidCommand? If it is not, can someone think of another way of

    doing
    > > it?
    > >
    > >
    > > For you, the only way to do it right is
    > >
    > > InvalidCommand(const std::string& s) :
    > > BasicException(std::string("The command ") + s + " is invalid",

    > 3),
    > > tmp(std::string("The command ") + s + " is invalid")
    > > {
    > > }
    > >
    > > The base classes are constructed before members, and member are
    > > constructed in the declaration order. The order in which you
    > > place them in the initialisation list does NOT matter. You relied
    > > on the 'tmp' being constructed before the base class. It is not.
    > >
    > > Victor
    > >
    > >

    > Thanks for the replies. After reading the post by john Harrison (calling
    > member funcs from an initialiser list) i tried to following, which

    compiles
    > in VC++:
    > class InvalidCommand : public Exceptions::BasicException
    > {
    > private:
    > std::string& MakeString(std::string& s)
    > {
    > s.insert(0, "The command ");
    > s.append(" is invalid");
    > return s;
    > }
    > public:
    > InvalidCommand(std::string s) : BasicException(MakeString(s), 3) {};
    > };
    > I gather that since MakeString is not accessing any unitialised class
    > members, the code is ok. Am I correct?


    Yes. And to be on the safe side, make it 'static' (since you don't
    need anything from any particular object).

    Victor
    Victor Bazarov, Jun 25, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jesus M. Salvo Jr.
    Replies:
    2
    Views:
    4,043
    robert
    Feb 11, 2006
  2. Jason Heyes
    Replies:
    6
    Views:
    368
    Norbert Riedlin
    Dec 4, 2003
  3. Tim
    Replies:
    2
    Views:
    1,497
  4. santosh

    member const initialisation

    santosh, May 23, 2005, in forum: C++
    Replies:
    3
    Views:
    424
    Lionel B
    May 23, 2005
  5. Tim Clacy
    Replies:
    8
    Views:
    386
    Tim Clacy
    May 30, 2006
Loading...

Share This Page