Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Class Member Data and Member Function Parameters - Should Parameters Be Data Members? (http://www.velocityreviews.com/forums/t454006-class-member-data-and-member-function-parameters-should-parameters-be-data-members.html)

Jason 05-13-2006 06:24 AM

Class Member Data and Member Function Parameters - Should Parameters Be Data Members?
 
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

Ian Collins 05-13-2006 06:44 AM

Re: Class Member Data and Member Function Parameters - Should ParametersBe Data Members?
 
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.

Jonathan Mcdougall 05-13-2006 07:11 AM

Re: Class Member Data and Member Function Parameters - Should Parameters Be Data Members?
 
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



All times are GMT. The time now is 10:06 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.