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

CLass members; public, private, protected and ???

Forgive me if I'm wrong but I think there is something like an extra
member scope in classes.

for example:

class abc {
ostream & operator << (ostream &, const abc &);
istream & operator >> (istream &, abc &);

private:
//...

public:
//...
};

-------

Now when I implement operator << and >>, note that I mustn't say they
are part of abc...

ostream & operator << (ostream & os, const abc & ABC) {
//but not ostream & abc::operator << (.............) {
//.......
}

-------

So when putting members in the part of the class with no specified
scope, they are not exactly part of the class anymore?

When I try to put those (<< and >>) in public scope, it won't work;
plus it wouldn't make sence to say (in the "complicated form")
ABC.operator << (cout, ABC);...

So am I right? Is there something like an extra scope? Wouldn't it be
the same if I just defined ostream & operator << (ostream &, const abc
& ABC); outside the class? Isn't it just overloading?

Of course all of the above might be wrong, but it is worth asking.

TIA,
cmad
Jul 22 '05 #1
6 2147

"Chris Mantoulidis" <cm****@yahoo.com> skrev i en meddelelse
news:a8**************************@posting.google.c om...
Forgive me if I'm wrong but I think there is something like an extra
member scope in classes.

for example:

class abc {
ostream & operator << (ostream &, const abc &);
istream & operator >> (istream &, abc &); friend ostream & operator << (ostream &, const abc &);
friend istream & operator >> (istream &, abc &);
private:
//...

public:
//...
};

-------

Now when I implement operator << and >>, note that I mustn't say they
are part of abc...

ostream & operator << (ostream & os, const abc & ABC) {
//but not ostream & abc::operator << (.............) {
//.......
}

-------

So when putting members in the part of the class with no specified
scope, they are not exactly part of the class anymore?
Yes they are - they are private.
When I try to put those (<< and >>) in public scope, it won't work;
plus it wouldn't make sence to say (in the "complicated form")
ABC.operator << (cout, ABC);...

So am I right? Is there something like an extra scope? Wouldn't it be
the same if I just defined ostream & operator << (ostream &, const abc
& ABC); outside the class? Isn't it just overloading?
"friend" designates that the operator or function is not part of the class
but is allowed access to the class' internals.
You could define it in the class to: for very simple functions this makes
sense. Eg:

friend ostream & operator << (ostream &os, const abc &elem) { return os <<
elem.a; }

Of course all of the above might be wrong, but it is worth asking.

TIA,
cmad


Kind regards
Peter
Jul 22 '05 #2
On 28 Dec 2003 23:11:56 -0800, cm****@yahoo.com (Chris Mantoulidis)
wrote:
Forgive me if I'm wrong but I think there is something like an extra
member scope in classes.

for example:

class abc {
ostream & operator << (ostream &, const abc &);
istream & operator >> (istream &, abc &);

private:
//...

public:
//...
};

-------

Now when I implement operator << and >>, note that I mustn't say they
are part of abc...

ostream & operator << (ostream & os, const abc & ABC) {
//but not ostream & abc::operator << (.............) {
//.......
}

-------

So when putting members in the part of the class with no specified
scope, they are not exactly part of the class anymore?

When I try to put those (<< and >>) in public scope, it won't work;
plus it wouldn't make sence to say (in the "complicated form")
ABC.operator << (cout, ABC);...

So am I right? Is there something like an extra scope? Wouldn't it be
the same if I just defined ostream & operator << (ostream &, const abc
& ABC); outside the class? Isn't it just overloading?

Of course all of the above might be wrong, but it is worth asking.

TIA,
cmad


You can declare non-member functions inside a class by declaring them
friend. In this case, it doesn't matter whether they come under
public, private or protected. It doesn't apply to them. You can even
define them inline, within the class, and yet they are still
non-member functions. Non-member functions that do not need access to
private members of a class should be declared non-friend, which
implies that they must be declared outside of the class.

Non-member declarations within a class declaration are useful when
functions are closely related to the class, and needing access to
private members, but not strongly enough to warrant membership.
Streaming ops are one example. Binary, relational operators, such as
<, ==, !=, etc., some argue, are better off not beintg member
functions, since they pertain to a relationship where neither side is
worthier than the other to officially evaluate it.

After the opening brace of a class declaration, there's an implicit
'private:'; just as there's an implicit 'public:' after the opening
brace of a struct declaration. So, it's not a 'no man's land' ;-)

Cheers!
Jul 22 '05 #3
"Dan W." <da**@raytron-controls.com> wrote in message
news:q4********************************@4ax.com...
On 28 Dec 2003 23:11:56 -0800, cm****@yahoo.com (Chris Mantoulidis)
wrote:
<<snip>>
You can declare non-member functions inside a class by declaring them
friend. In this case, it doesn't matter whether they come under
public, private or protected. It doesn't apply to them. You can even
define them inline, within the class, and yet they are still
non-member functions. Non-member functions that do not need access to
private members of a class should be declared non-friend, which
implies that they must be declared outside of the class.

Non-member declarations within a class declaration are useful when
functions are closely related to the class, and needing access to
private members, but not strongly enough to warrant membership.
Streaming ops are one example. Binary, relational operators, such as
<, ==, !=, etc., some argue, are better off not beintg member
functions, since they pertain to a relationship where neither side is
worthier than the other to officially evaluate it.


More importantly, and in this case, if the function signature doesn't
include the hidden this pointer, you can't have the overloaded operator
function unless it is a non-member function that has been made a friend of
the class. The operator>> and operator<< functions that you are overloading
for the iostream classes has a left-hand operand of stream type and a
right-hand operand of the overloaded type. A this pointer would mess up the
signature.
--
Gary
Jul 22 '05 #4
Chris Mantoulidis wrote:
Forgive me if I'm wrong but I think there is something like an extra
member scope in classes.

for example:

class abc {
ostream & operator << (ostream &, const abc &);
istream & operator >> (istream &, abc &);

private:
//...

public:
//...
};

-------

Now when I implement operator << and >>, note that I mustn't say they
are part of abc...

ostream & operator << (ostream & os, const abc & ABC) {
//but not ostream & abc::operator << (.............) {
//.......
}

-------

So when putting members in the part of the class with no specified
scope, they are not exactly part of the class anymore?

When I try to put those (<< and >>) in public scope, it won't work;
plus it wouldn't make sence to say (in the "complicated form")
ABC.operator << (cout, ABC);...

So am I right? Is there something like an extra scope? Wouldn't it be
the same if I just defined ostream & operator << (ostream &, const abc
& ABC); outside the class? Isn't it just overloading?

Of course all of the above might be wrong, but it is worth asking.

TIA,
cmad


I see friend functions used frequently in responses to your original
query, and in other code posted here. Imho, there are very few valid
uses for "friend." If operators or functions outside the class need to
manipulate the class's member data, just provide the appropriate
accessor functions.

ostream & operator << ( ostream & os, const Complex& c ) {
return os << '(' << c.real_part( ) << ','
<< c.imaginary_part( ) << ')';
}

Jul 22 '05 #5
> > the same if I just defined ostream & operator << (ostream &, const abc
& ABC); outside the class? Isn't it just overloading?

Of course all of the above might be wrong, but it is worth asking.

TIA,
cmad


I see friend functions used frequently in responses to your original
query, and in other code posted here. Imho, there are very few valid
uses for "friend." If operators or functions outside the class need to
manipulate the class's member data, just provide the appropriate
accessor functions.

ostream & operator << ( ostream & os, const Complex& c ) {
return os << '(' << c.real_part( ) << ','
<< c.imaginary_part( ) << ')';
}


Yeah, that's what I was thinking about :)

There's no need for friend, since I DO provide accessor functions in all my classes.

But still, thank you all cuz I now do understand more about friend...

But I like Jeff's way more... Plain overloading
Jul 22 '05 #6
Chris Mantoulidis wrote:
the same if I just defined ostream & operator << (ostream &, const abc
& ABC); outside the class? Isn't it just overloading?

Of course all of the above might be wrong, but it is worth asking.

TIA,
cmad
I see friend functions used frequently in responses to your original
query, and in other code posted here. Imho, there are very few valid
uses for "friend." If operators or functions outside the class need to
manipulate the class's member data, just provide the appropriate
accessor functions.

ostream & operator << ( ostream & os, const Complex& c ) {
return os << '(' << c.real_part( ) << ','
<< c.imaginary_part( ) << ')';
}

Yeah, that's what I was thinking about :)

There's no need for friend, since I DO provide accessor functions in all my classes.

hmm.
"accessor" functions - are accessible by *ANYONE* from 'outside', on the
other hand friendship gives the access *ONLY* to those who really need it.

But still, thank you all cuz I now do understand more about friend...

But I like Jeff's way more... Plain overloading

__________________________________________________ _____________________________
Posted Via Uncensored-News.Com - Accounts Starting At $6.95 - http://www.uncensored-news.com
<><><><><><><> The Worlds Uncensored News Source <><><><><><><><>

Jul 22 '05 #7

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

Similar topics

11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
4
by: Steve | last post by:
I'll be the first to admit, I'm not entirely clear on the appropriate usage of either. From what I am reading in my books, a Struct and a Class are pretty much the same, with the difference being,...
1
by: Matthias Kaeppler | last post by:
Sorry if this has been discussed before (I'm almost certain it has), but I didn't know what to google for. My problem is, I have a class, a gtkmm widget, and I want it to serve as a base class...
12
by: Manolis | last post by:
Hi, I was wondering if there is any way to make two objects of the same class to be able to access each other's private data, like this: class A { public: void access( const A& a )...
19
by: hamil | last post by:
I have a form with one button, Button1, and a Textbox, Textbox1 I have a class, class1 as follows. Public Class Class1 Public DeForm As Object Sub doit() DeForm.Textbox1.text = "It works"...
9
by: olanglois | last post by:
Hi, I am not sure if I have found a compiler bug (I am using VC++.NET2003) or if this is the correct behavior defined by the language but I am sure someone can clear up my confusion. Suppose the...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
3
by: Curious | last post by:
I have too similar methods in two classes: -----------------------DistributionFoo--------------------- class DistributionFoo : FooBase { private...
8
by: Mayur H Chauhan | last post by:
All, For my knowledge, if I declare Class as follow, then it thows compilation error. Protected Class Book End Class Even same for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...
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.