473,394 Members | 1,663 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Lifetime of member variables in C++ object

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 '07 #1
3 2308
na******@gmail.com 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
Nov 1 '07 #2
On 2007-11-01 21:10, na******@gmail.com 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
Nov 1 '07 #3
na******@gmail.com 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 (tk****@NOSPAMPLEASElife.pl)

First rule of optimization: Don't do it.
Second rule of optimization (advanced): Don't do it yet.
Nov 1 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
8
by: pt | last post by:
Hallo, i wonder how it is going to be of this code below regarding of the return of temporary object. Prototypes: =========== bool Activation(TCHAR *c); std::basic_string<TCHAR> GetFile();
15
by: Gabor Drasny | last post by:
Hi all, Could anyone tell me if the following code is guaranteed to work or not? #include <string> #include <iostream> int main() { const char* s = std::string("Hello World").c_str();
9
by: Chuck Cobb | last post by:
I am creating some static classes with static methods and static variables, but I have a question: What is the lifetime of static classes and static variables? Is there any risk that these...
3
by: Vagif Abilov | last post by:
I investigate further the problem I reported earlier as incorrectly set values of static variable instances. Actually it is reproduced with a little piece of code. I made a simlpe Web site...
9
by: David | last post by:
With a non-server app there is one instance of the program running and one user 'using' it at a time. With this scenario I'm pretty comfortable with variable scope and lifetime. With a server app...
3
by: mario semo | last post by:
Hello, What does the C++ Norm says about the lifetime of compiler generated temporary variables? #include <stdio.h> class BaseRef {...
6
by: better_cs_now | last post by:
Hello all, class Foo {/* Details don't matter */}; class Bar { public: Bar(): m_Foo(/* Construct a Foo however it wants to be constructed */); const Foo &GetFoo() const { return m_Foo; }...
7
by: Immortal Nephi | last post by:
My project grows large when I put too many member functions into one class. The header file and source code file will have approximately 50,000 lines when one class contains thousand member...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.