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

J

Jason

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
 
I

Ian Collins

Jason said:
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....
 
J

Jonathan Mcdougall

Jason said:
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
 

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

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top