472,977 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,977 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 2121

"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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.