Lifetime of member variables in C++ object

Discussion in 'C++' started by nagashre@gmail.com, Nov 1, 2007.

  1. Guest

    class A
    {
    public:
    A():a(0), b(0){}

    handleMyMsg( char* aa, char*bb);

    private:

    processMessage();
    processMessage2();
    char* a;
    char*b;
    }

    Object of type A is created once and handleMyMsg is called several
    times with different values for the arguments. processMessage and
    processMessage2 are helper functions which needs to work on the
    arguments passed to handleMyMsg.

    Is it a bad idea to have member variables a and b which are
    initialized witht he values of aa and bb every time handleMyMsg is
    called?

    In other words, is it a bad idea to have member variables which do not
    have the same lifetime as the object which owns it?
     
    , Nov 1, 2007
    #1
    1. Advertising

  2. wrote:
    > class A
    > {
    > public:
    > A():a(0), b(0){}
    >
    > handleMyMsg( char* aa, char*bb);


    A return value type is missing here.

    >
    > private:
    >
    > processMessage();
    > processMessage2();
    > char* a;
    > char*b;
    > }


    A semicolon is missing.

    Both errors suggest you're not posting real C++ code. It is actually
    preferable that you do post real code, otherwise we're not sure what
    your problem is.

    >
    > Object of type A is created once and handleMyMsg is called several
    > times with different values for the arguments. processMessage and
    > processMessage2 are helper functions which needs to work on the
    > arguments passed to handleMyMsg.
    >
    > Is it a bad idea to have member variables a and b which are
    > initialized witht he values of aa and bb every time handleMyMsg is
    > called?


    They are not initialised. They are probably assigned some values.
    It's unclear _how_ it's done (since you omitted the implemetation of
    the 'handleMyMsg' function).

    > In other words, is it a bad idea to have member variables which do not
    > have the same lifetime as the object which owns it?


    No, it's not an inherently bad idea. It all depends on what you
    need/use them for.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 1, 2007
    #2
    1. Advertising

  3. On 2007-11-01 21:10, wrote:
    > class A
    > {
    > public:
    > A():a(0), b(0){}
    >
    > handleMyMsg( char* aa, char*bb);
    >
    > private:
    >
    > processMessage();
    > processMessage2();
    > char* a;
    > char*b;
    > }
    >
    > Object of type A is created once and handleMyMsg is called several
    > times with different values for the arguments. processMessage and
    > processMessage2 are helper functions which needs to work on the
    > arguments passed to handleMyMsg.
    >
    > Is it a bad idea to have member variables a and b which are
    > initialized witht he values of aa and bb every time handleMyMsg is
    > called?


    Yes, it is a very bad idea. If processMessage() and processMessage2()
    needs access to the arguments passed to handleMyMsg() then you should
    pass them as arguments to those functions too.

    > In other words, is it a bad idea to have member variables which do not
    > have the same lifetime as the object which owns it?


    That is not really what happens, lifetime is a well defined concept in
    C++ and does not mean what you are thinking of.

    --
    Erik Wikström
     
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, Nov 1, 2007
    #3
  4. wrote:
    > class A
    > {
    > public:
    > A():a(0), b(0){}
    >
    > handleMyMsg( char* aa, char*bb);
    >
    > private:
    >
    > processMessage();
    > processMessage2();
    > char* a;
    > char*b;
    > }
    >
    > Object of type A is created once and handleMyMsg is called several
    > times with different values for the arguments. processMessage and
    > processMessage2 are helper functions which needs to work on the
    > arguments passed to handleMyMsg.
    >
    > Is it a bad idea to have member variables a and b which are
    > initialized witht he values of aa and bb every time handleMyMsg is
    > called?
    >
    > In other words, is it a bad idea to have member variables which do not
    > have the same lifetime as the object which owns it?


    Your member variables are pointers and their lifetime is the same as the
    lifetime of the object which owns them. In function handleMyMsg you
    simply assign them new values which has nothing to do with their lifetime.
    But if their values are used only during the execution of handleMyMsg
    then it's a design flaw to keep them as members. Just pass them to the
    helper functions.

    --
    Tadeusz B. Kopec ()

    First rule of optimization: Don't do it.
    Second rule of optimization (advanced): Don't do it yet.
     
    Tadeusz Kopec, Nov 1, 2007
    #4
    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. Anargyros L. Papadopoulos

    lifetime of local variables

    Anargyros L. Papadopoulos, Jan 21, 2005, in forum: C++
    Replies:
    1
    Views:
    319
    Victor Bazarov
    Jan 21, 2005
  2. Vagif Abilov
    Replies:
    3
    Views:
    18,298
    Laurent Bugnion
    May 24, 2006
  3. Replies:
    1
    Views:
    346
    Puppet_Sock
    Nov 1, 2007
  4. Replies:
    9
    Views:
    995
  5. mario semo
    Replies:
    3
    Views:
    652
    James Kanze
    May 15, 2008
Loading...

Share This Page