Lifetime of member variables in C++ object

N

nagashre

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?
 
V

Victor Bazarov

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
 
G

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?

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.
 
T

Tadeusz Kopec

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top