By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

a friend class

P: n/a
Hi

I need class A to access a method of class B using the friend
mechanism.

// B.h
#include "a.h"

class B
{
double balance;
....
public:
friend A;
double balance() { return balance; } //definition is in a .cpp file
....
};

// A.cpp
A::doing_it()
{
....
double f = B::balance - 5;
};

is this right?

thank you
Feb 22 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Gary Wessle wrote:
Hi

I need class A to access a method of class B using the friend
mechanism.

// B.h
#include "a.h"

class B
{
I am surprised that your compiler did not complain about having
a variable and a function having the same name.
double balance;
...
public:
friend A;
Like this is very confusing.
double balance() { return balance; } //definition is in a .cpp file
...
};

// A.cpp
A::doing_it()
{
...
Since balance (The member function) is part of the public
interface, you do not need friendship at all. You can call
balance from an instance of the class B.

B foo;
double f = foo.balance() - 5;
double f = B::balance - 5;
};

is this right?

thank you
Feb 22 '07 #2

P: n/a
On Feb 22, 3:12 pm, Gary Wessle <phd...@yahoo.comwrote:
Hi

I need class A to access a method of class B using the friend
mechanism.

// B.h
#include "a.h"

class B
{
double balance;
...
public:
friend A;
double balance() { return balance; } //definition is in a .cpp file
...

};

// A.cpp
A::doing_it()
{
...
double f = B::balance - 5;

};

is this right?

thank you
Seems the way you approached is right but I could see one problem in
this code.
Since Balance is not a static member of the class, you may need to
create object for accessing it.

Feb 22 '07 #3

P: n/a
On Feb 22, 3:44 pm, Piyo <cybermax...@yahoo.comwrote:
Gary Wessle wrote:
Hi
I need class A to access a method of class B using the friend
mechanism.
// B.h
#include "a.h"
class B
{

I am surprised that your compiler did not complain about having
a variable and a function having the same name.
double balance;
...
public:
friend A;

Like this is very confusing.
double balance() { return balance; } //definition is in a .cpp file
...
};
// A.cpp
A::doing_it()
{
...

Since balance (The member function) is part of the public
interface, you do not need friendship at all. You can call
balance from an instance of the class B.

B foo;
double f = foo.balance() - 5;
double f = B::balance - 5;
};

- Show quoted text -
Seems the interface name and member variable name is confusing and
meaning less.

Feb 22 '07 #4

P: n/a
"Gary Wessle" <ph****@yahoo.comwrote in message
news:m3************@localhost.localdomain
Hi

I need class A to access a method of class B using the friend
mechanism.

// B.h
#include "a.h"

class B
{
double balance;
...
public:
friend A;
Make it

friend class A;
double balance() { return balance; } //definition is in a .cpp file
You can't use balance as both a variable name and a function name. Try

double get_balance() { return balance; }

Better yet, make it

double get_balance() const { return balance; }

in order to declare that the function does not change the B object.
...
};

// A.cpp
A::doing_it()
Missing return type.
{
...
double f = B::balance - 5;
};

1. If you want to access a method, then you need brackets to indicate a
function call.

2. For a class to access a public function, which get_balance() is, it is
not necessary to be a friend.

3. For any function that is not part of the B hierarchy to access a
non-static member of B, you need a B object. B::balance or B::balance() only
works with static members. doing_it could get access to a B object by
various means, the most obvious being that a B object is passed to it as a
function argument. Thus you might have

void A::doing_it(const B&b)
{
// ...
double f = b.balance - 5;
}

or

void A::doing_it(const B&b)
{
// ...
double f = b.get_balance() - 5;
}
--
John Carson
Feb 22 '07 #5

P: n/a
Gary Wessle wrote:
Hi

I need class A to access a method of class B using the friend
mechanism.
Why? It's unnecessary here -- see below.
// B.h
#include "a.h"

class B
{
double balance;
...
public:
friend A;
double balance() { return balance; } //definition is in a .cpp file
...
};

// A.cpp
A::doing_it()
{
...
double f = B::balance - 5;
};

is this right?
No.

As Piyo pointed out, you can use the balance() member function. Also,
your code wouldn't compile. Is balance a member or a member function?
Plus, B::balance is not static, so it requires an object. Also, the
friend definition is incorrect. It should be "friend class A".
However, as said before, A does not need to be a friend of B.

Friendship needs to be carefully considered. Read the FAQ on friends.
Feb 22 '07 #6

P: n/a
On Feb 22, 7:12 am, Gary Wessle <phd...@yahoo.comwrote:
Hi

I need class A to access a method of class B using the friend
mechanism.

// B.h
#include "a.h"

class B
{
double balance;
...
public:
friend A;
friend class A;
// A.cpp
A::doing_it()
{
...
double f = B::balance - 5;
You need an instance of the object owning whatever it is you are
trying to access:

void A::doing_it(B& foo)
{
...
double f = foo.balance;
....
}

--
Erik Wikström

Feb 22 '07 #7

P: n/a
Gary Wessle wrote:
Hi

I need class A to access a method of class B using the friend
mechanism.

// B.h
#include "a.h"

class B
{
double balance;
...
public:
friend A;
double balance() { return balance; } //definition is in a .cpp file
...
};

// A.cpp
A::doing_it()
{
...
double f = B::balance - 5;
};

is this right?

thank you
You've misundestood friendship. Friendship is so that one class can
access the *private* parts of another class. But B::balance is public,
so you don't need friendship to access it. What you do need (and what
you haven't got) is a B object. In other words you need to find that
balance of something, you can't just conjure a balance out of thin air.

Probably you need something like this

void A::doing_it(B& some_b_object)
{
double f = some_b_object.balance() - 5;
}

In other words you pass the B object you need to get the balance of as a
parameter to your doing_it method.

But that is only a guess on my part. One of the problems with your code
is that names like A, B and doing_it don't help anyone understand what
you are trying to do.

john
Feb 22 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.