473,698 Members | 2,246 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 someOtherMember Data;

bool Save(ConfigFile , Data);

or

//Example with member function parameters not as data members
class configWriter
string someDataMember;
bool Save(aConfigFil e, 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 3794
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 someOtherMember Data;

bool Save(ConfigFile , Data);

or

//Example with member function parameters not as data members
class configWriter
string someDataMember;
bool Save(aConfigFil e, 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
2062
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 with potential to be useful, but I would like to hear more opinions first. If this is deemed to be useful, I *may* try to write a PEP for it. This is not a promise or even a proposal, at this point. Broadly generalizing, classes in Python have...
8
3076
by: DaKoadMunky | last post by:
Please consider the following... <CODE> #include <string> using namespace std; typedef int PrimitiveType; typedef string ClassType;
6
2538
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 called CUtils with a static method called print2std()
5
8729
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 conceptually speaking : when do I define something as 'struct' and when as 'class' ? for example : if I want to represent a 'Time' thing, containing : - data members : hours, mins, secs
3
1608
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 Parameter:Parameter() contains various values such as interest = 0.05; My intent is for the user to be able to override these defaults so I have a function void Parameter::set(void) . This is supposed to give the user a chance
22
2760
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 fun() {
11
1824
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 the same type. So what type to specify in the declaration of the map? map<std::string,void *¿?
13
2524
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 functions in common implementations? And where is the 'this' ptr tucked away at for POD structs with member functions? John
4
1286
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 subsystem them talks to z over yz interface. In a typical scenario, y would receive a set of parameters from x (over xy). Some of these are meant for z subsys as well. So y needs to send these plus some more parameters to z. The implementation...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7739
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.