a friend class

G

Gary Wessle

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
 
P

Piyo

Gary said:
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;
 
S

Sarath

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.
 
S

Sarath

I am surprised that your compiler did not complain about having
a variable and a function having the same name.


Like this is very confusing.



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;




- Show quoted text -

Seems the interface name and member variable name is confusing and
meaning less.
 
J

John Carson

Gary Wessle said:
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;
}
 
R

red floyd

Gary said:
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.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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;
....
}
 
J

John Harrison

Gary said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,124
Latest member
JuniorPell
Top