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

Discussion in 'C++' started by Jason, May 13, 2006.

  1. Jason

    Jason Guest

    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
     
    Jason, May 13, 2006
    #1
    1. Advertising

  2. Jason

    Ian Collins Guest

    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.
     
    Ian Collins, May 13, 2006
    #2
    1. Advertising

  3. 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
     
    Jonathan Mcdougall, May 13, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,786
    Thomas Weidenfeller
    Oct 12, 2005
  2. CoolPint
    Replies:
    8
    Views:
    987
    Jeff Schwab
    Dec 14, 2003
  3. hdixon
    Replies:
    3
    Views:
    648
    hdixon
    Jul 9, 2006
  4. Replies:
    2
    Views:
    816
    Noah Roberts
    Jun 9, 2006
  5. r.z.
    Replies:
    6
    Views:
    371
    red floyd
    Mar 22, 2007
Loading...

Share This Page