473,406 Members | 2,467 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,406 software developers and data experts.

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
May 13 '06 #1
2 3773
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Carlos Ribeiro | last post by:
I thought about this problem over the weekend, after long hours of hacking some metaclasses to allow me to express some real case data structures as Python classes. I think that this is something...
8
by: DaKoadMunky | last post by:
Please consider the following... <CODE> #include <string> using namespace std; typedef int PrimitiveType; typedef string ClassType;
6
by: gustav04 | last post by:
hi all i have a question: what is the difference between a c-function and an c++ class method (both do exactly the same thing). lets say, i have a function called print2std() and a class...
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
3
by: pauldepstein | last post by:
Sorry in advance if this message sounds imprecise but it's difficult to be precise when you don't really understand what's going on. I have a class called Parameters. The default constructor...
22
by: ypjofficial | last post by:
Is there any possibility of invoking the member functions of a class without creating an object (or even a pointer to ) of that class. eg. #include <iostream.h> class test { public: void...
11
by: Manuel | last post by:
Hi, I need implement a map of member functions of some class. This map is formed by a string and a pointer to the member function. The problem is that the map need that the object saved are...
13
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member...
4
by: sunderjs | last post by:
Hi, This is from a typical telecom software implementation. I have three subsystems (x, y, z) which exchange data amongst them. The arrangement is such that x talks to y over interface xy. y...
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: 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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.