By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 1,038 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

Class Member Data and Member Function Parameters - Should Parameters Be Data Members?

P: n/a
Hello:

First, if this is one of those "questions asked a million times" just say so
and I'll dig a little deeper. If not, then...

I'm curious, is it typical to use member data (properties, attributes, etc.)
as parameters to the class's member functions or would you normally have
parameters that are not data members of the class? Or is it normally a
combination, just depending on circumstances? Did I make sense? If anyone
knows of any books that talk about this, I'd appreciate a title, etc. I
guess I'm asking which is the better design.

Example:

Is it better to have a class like:

//Example with member function parameters as data members
class configWriter
string ConfigFile;
string Data;
string someOtherMemberData;

bool Save(ConfigFile, Data);

or

//Example with member function parameters not as data members
class configWriter
string someDataMember;
bool Save(aConfigFile, someData);

(I left out precise syntax and scope as I didn't think it relevant to the
topic.)

Thanks for your advice,

-Jason
May 13 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Jason wrote:
Hello:

First, if this is one of those "questions asked a million times" just say so
and I'll dig a little deeper. If not, then...

I'm curious, is it typical to use member data (properties, attributes, etc.)
as parameters to the class's member functions or would you normally have
parameters that are not data members of the class? Or is it normally a
combination, just depending on circumstances? Did I make sense? If anyone
knows of any books that talk about this, I'd appreciate a title, etc. I
guess I'm asking which is the better design.
You don't have to pass class data members to a class method, they are
part of the class so the methods can just use them.
Example:

Is it better to have a class like:

//Example with member function parameters as data members
class configWriter
string ConfigFile;
string Data;
string someOtherMemberData;

bool Save(ConfigFile, Data);

or

//Example with member function parameters not as data members
class configWriter
string someDataMember;
bool Save(aConfigFile, someData);

Well that all depends what the class does with the data. If the class
is responsible for saving data, the file would probably be a member of
the class and you would just pass the data. Conversely, it the class
was responsible for the data, you might pass it the file....

--
Ian Collins.
May 13 '06 #2

P: n/a
Jason wrote:
I'm curious, is it typical to use member data (properties, attributes, etc.)
as parameters to the class's member functions or would you normally have
parameters that are not data members of the class? Or is it normally a
combination, just depending on circumstances? Did I make sense?
The less information a class has, the more you have to give it. That's
a compromise between ease of use and genericity. It depends on your
design and you tastes.

Usually, if the data is "part of" the class (it conceptually makes
sense to bundle the data with the class), you'll make it a member
object. If the data is "irrelevant" to the class (for example, a stream
writes data, but *what* the data is is irrelevant), you'll probably
want it to be a parameter.

If you find yourself passing the same parameter again and again to many
member functions, it would probably be better to keep a reference to
that data inside the class.
(I left out precise syntax and scope as I didn't think it relevant to the
topic.)


Don't. This is a C++ group, post compilable C++ code.
Jonathan

May 13 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.