473,839 Members | 1,631 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Non-member vs. Member

Dear all,

Is there a clear distinction how to decide which functions to be
members of a class and which not

How is your attitude (Your general way from your experiences ...)

"If the function changes the state of the object, it ought to be a
member of that object." Reference Accelerated C++, A. Koenig, page 159.

Thx to you all.

Apr 17 '06 #1
39 3208
"utab" <um********@gma il.com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.com.. .
"If the function changes the state of the object, it ought to be a
member of that object." Reference Accelerated C++, A. Koenig, page 159.


Please note that "ought to be" is less strong than "should always be." It
means "make it a member unless you have a good reason to do otherwise."

One such reason might be a function that changes the state of two or more
objects of different classes. Such a function cannot possibly be members of
both objects. Moreover, such functions are not uncommon: operator>> is
probably the most widely used example.

So sometimes it's impossible to make state-changing functions members of
their objects. Still, it's nice to do so when there is a choice.
Apr 17 '06 #2

utab wrote:
Dear all,

Is there a clear distinction how to decide which functions to be
members of a class and which not
This isn't a C++ question....
How is your attitude (Your general way from your experiences ...)

"If the function changes the state of the object, it ought to be a
member of that object." Reference Accelerated C++, A. Koenig, page 159.


I'm sure there are examples where this doesn't make since, but in
general I agree.

Apr 17 '06 #3
In article <Me************ ******@bgtnsc04-news.ops.worldn et.att.net>,
"Andrew Koenig" <ar*@acm.org> wrote:
"utab" <um********@gma il.com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.com.. .
"If the function changes the state of the object, it ought to be a
member of that object." Reference Accelerated C++, A. Koenig, page 159.


Please note that "ought to be" is less strong than "should always be." It
means "make it a member unless you have a good reason to do otherwise."

One such reason might be a function that changes the state of two or more
objects of different classes. Such a function cannot possibly be members of
both objects. Moreover, such functions are not uncommon: operator>> is
probably the most widely used example.

So sometimes it's impossible to make state-changing functions members of
their objects. Still, it's nice to do so when there is a choice.


I would go so far as to say that if the function changes the state of
two or more objects, it should not be in the class, even if both/all
objects are of the same type.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 17 '06 #4
BigBrian wrote:
Is there a clear distinction how to decide which functions to be
members of a class and which not


This isn't a C++ question....


Mr. Manners reminds the Gentle Poster that replies should be on topic if
possible.

For example, C++ brings many special considerations to the question of
whether a function should be semantically a member of an object's
interface, or syntactically a member of its class. operator<<, as already
mention, is semantically a member of both the interfaces of your object and
of ostream. But it is syntactically not a member of your class's methods.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 17 '06 #5
Daniel T. wrote:
In article <Me************ ******@bgtnsc04-news.ops.worldn et.att.net>,
"Andrew Koenig" <ar*@acm.org> wrote:
"utab" <um********@gma il.com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.com.. .
"If the function changes the state of the object, it ought to be a
member of that object." Reference Accelerated C++, A. Koenig, page
159.


Please note that "ought to be" is less strong than "should always
be." It means "make it a member unless you have a good reason to do
otherwise."

One such reason might be a function that changes the state of two or
more objects of different classes. Such a function cannot possibly
be members of both objects. Moreover, such functions are not
uncommon: operator>> is probably the most widely used example.

So sometimes it's impossible to make state-changing functions
members of their objects. Still, it's nice to do so when there is a
choice.


I would go so far as to say that if the function changes the state of
two or more objects, it should not be in the class, even if both/all
objects are of the same type.


Actually, I strongly disagree. If it only concerns this particular type,
then the function _should_ be in the class. It should probably be static
of course.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 17 '06 #6

"utab" <um********@gma il.com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.com.. .
Dear all,

Is there a clear distinction how to decide which functions to be
members of a class and which not

How is your attitude (Your general way from your experiences ...)

"If the function changes the state of the object, it ought to be a
member of that object." Reference Accelerated C++, A. Koenig, page 159.

Thx to you all.


Koenig's quote notwithstanding , there is an undesirable aspect of member
functions: they can depend on the implementation. This is sometimes called
"breaking encapsulation". This tends to reduce the maintainability of the
code because if the implementation changes there is more to do and more
mistakes to make. Consider:

void
SomeClass::noti fy_and_print()
{
notify(); // member function
print(); // another member function;
}

Classes like this which can be written entirely in terms of the rest of an
object's interface can be refactored out:

void
notify_and_prin t(SomeClass &c)
{
c.notify();
c.print();
}

Although both of these functions will continue to work if the implementation
changes, in the second case I KNOW this to be the case because the second
function has no access to the private parts of SomeClass (i.e. doesn't
"break encapsulation") .

As a result of these considerations I usually don't allow member functions
which feature gratuitious access to the implementation. However, I certainly
make exceptions if the interface design seems to call for it.

Cy
Apr 17 '06 #7
In article <e2**********@n ews.datemas.de> ,
"Victor Bazarov" <v.********@com Acast.net> wrote:
Daniel T. wrote:
In article <Me************ ******@bgtnsc04-news.ops.worldn et.att.net>,
"Andrew Koenig" <ar*@acm.org> wrote:
"utab" <um********@gma il.com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.com.. .

"If the function changes the state of the object, it ought to be a
member of that object." Reference Accelerated C++, A. Koenig, page
159.

Please note that "ought to be" is less strong than "should always
be." It means "make it a member unless you have a good reason to do
otherwise."

One such reason might be a function that changes the state of two or
more objects of different classes. Such a function cannot possibly
be members of both objects. Moreover, such functions are not
uncommon: operator>> is probably the most widely used example.

So sometimes it's impossible to make state-changing functions
members of their objects. Still, it's nice to do so when there is a
choice.


I would go so far as to say that if the function changes the state of
two or more objects, it should not be in the class, even if both/all
objects are of the same type.


Actually, I strongly disagree. If it only concerns this particular type,
then the function _should_ be in the class. It should probably be static
of course.


There are readability/writability factors involved. If the function is a
static class member, then it helps scope the function, although the
parameters do that so humm... why should it be static in the class?

As an example:

class Object {
public:
// whatever
};

// returns true if 'a' and 'b' have intersecting areas.
bool hit( Object& a, Object& b );

Why exactly _should_ 'hit' be a static in Object?
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 17 '06 #8
Daniel T. wrote:
In article <e2**********@n ews.datemas.de> ,
"Victor Bazarov" <v.********@com Acast.net> wrote:
Daniel T. wrote:
In article
<Me************ ******@bgtnsc04-news.ops.worldn et.att.net>, "Andrew
Koenig" <ar*@acm.org> wrote:

"utab" <um********@gma il.com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.com.. .

> "If the function changes the state of the object, it ought to be a
> member of that object." Reference Accelerated C++, A. Koenig, page
> 159.

Please note that "ought to be" is less strong than "should always
be." It means "make it a member unless you have a good reason to
do otherwise."

One such reason might be a function that changes the state of two
or more objects of different classes. Such a function cannot
possibly be members of both objects. Moreover, such functions are
not uncommon: operator>> is probably the most widely used example.

So sometimes it's impossible to make state-changing functions
members of their objects. Still, it's nice to do so when there is
a choice.

I would go so far as to say that if the function changes the state
of two or more objects, it should not be in the class, even if
both/all objects are of the same type.


Actually, I strongly disagree. If it only concerns this particular
type, then the function _should_ be in the class. It should
probably be static of course.


There are readability/writability factors involved. If the function
is a static class member, then it helps scope the function, although
the parameters do that so humm... why should it be static in the
class?

As an example:

class Object {
public:
// whatever
};

// returns true if 'a' and 'b' have intersecting areas.
bool hit( Object& a, Object& b );

Why exactly _should_ 'hit' be a static in Object?


Simply because when I see

hit(a, b);

I really have no idea what 'a' and 'b' are and which of fourteen 'hit'
functions is going to be used. If I see

Object::hit(a, b)

there is no doubt which function is used.

Another argument: limiting the name scope is impossible with arguments.
And limiting the scope of every name is what we should be doing all the
time. Polluting namespace scope with functions that do something very
specific to objects of a single class is... well, polluting.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 17 '06 #9
In article <e2**********@n ews.datemas.de> ,
"Victor Bazarov" <v.********@com Acast.net> wrote:
Daniel T. wrote:
In article <e2**********@n ews.datemas.de> ,
"Victor Bazarov" <v.********@com Acast.net> wrote:
Daniel T. wrote:
In article
<Me************ ******@bgtnsc04-news.ops.worldn et.att.net>, "Andrew
Koenig" <ar*@acm.org> wrote:

> "utab" <um********@gma il.com> wrote in message
> news:11******** **************@ i39g2000cwa.goo glegroups.com.. .
>
>> "If the function changes the state of the object, it ought to be a
>> member of that object." Reference Accelerated C++, A. Koenig, page
>> 159.
>
> Please note that "ought to be" is less strong than "should always
> be." It means "make it a member unless you have a good reason to
> do otherwise."
>
> One such reason might be a function that changes the state of two
> or more objects of different classes. Such a function cannot
> possibly be members of both objects. Moreover, such functions are
> not uncommon: operator>> is probably the most widely used example.
>
> So sometimes it's impossible to make state-changing functions
> members of their objects. Still, it's nice to do so when there is
> a choice.

I would go so far as to say that if the function changes the state
of two or more objects, it should not be in the class, even if
both/all objects are of the same type.

Actually, I strongly disagree. If it only concerns this particular
type, then the function _should_ be in the class. It should
probably be static of course.
There are readability/writability factors involved. If the function
is a static class member, then it helps scope the function, although
the parameters do that so humm... why should it be static in the
class?

As an example:

class Object {
public:
// whatever
};

// returns true if 'a' and 'b' have intersecting areas.
bool hit( Object& a, Object& b );

Why exactly _should_ 'hit' be a static in Object?


Simply because when I see

hit(a, b);

I really have no idea what 'a' and 'b' are and which of fourteen 'hit'
functions is going to be used.


Seems to me that if you don't know what 'a' and 'b' are, you really
shouldn't be writing the code.

If I see

Object::hit(a, b)

there is no doubt which function is used.
The above is nothing more than a compiler enforced version of hungarian
notation.

Another argument: limiting the name scope is impossible with arguments.
And limiting the scope of every name is what we should be doing all the
time. Polluting namespace scope with functions that do something very
specific to objects of a single class is... well, polluting.


Unless, of course, the namespace is for "things that work with Objects"
presumably...
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 17 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
4441
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few characters as possible will be matched. the regular expression module fails to perform non-greedy matches as described in the documentation: more than "as few characters as possible"
5
3764
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence their dtor is called immediately and not at the end of the function. to be able to use return objects (to avoid copying) i often assign them to a const reference. now, casting a const return object from a function to a non-const reference to this...
3
12279
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in nonblocking mode in c++? I did something ugly like --- c/c++ mixture --- mkfifo( "testpipe", 777);
25
7697
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
4539
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
22
12080
by: Steve - DND | last post by:
We're currently doing some tests to determine the performance of static vs non-static functions, and we're coming up with some odd(in our opinion) results. We used a very simple setup. One class had a static function, and the one class had a non-static function. Both of these functions did the exact same thing. The test function: public void Test(){ decimal y = 2; decimal x = 3;
8
3526
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I cannot load/unload/reload extensions into my large and slow-to-load application during development without restarting the process then the disadvantages may outweigh the advantages. I've got a mixed-mode program in which I create a new AppDomain...
0
2350
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome jobs. When a non-secure page references a secure page with relative URL, the web server generates error until absolute URL with https prefix is used. On the other hand when a secure page references a non-secure page, the non-secure page will be...
399
13068
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or to python-3000@python.org In summary, this PEP proposes to allow non-ASCII letters as identifiers in Python. If the PEP is accepted, the following identifiers would also become valid as class, function, or variable names: Löffelstiel,...
12
29938
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
9698
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,...
1
10654
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10297
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9426
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
7833
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
7021
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4493
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
4066
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.