|
-
April 20th, 2010, 02:29 PM
#1
Help with friend function
I am learning C++ using the book "C++ Primer", and write some small code to test the concepts I am learning.
According to the book, a friend a class can be:
(1) a non-member function
(2) a member function of another class
(3) another class
I am testing how to write a friend of class A , which is a member function of class B. Below is my code:
Code:
class A;
class B{
public:
int getValue(A* pa) {
return pa->va_;
}
};
class A {
friend int B::getValue(A* pa);
public:
A():va_(1){}
private:
int va_;
};
I always got the error of: error C2027: use of undefined type 'A'
I used forward declaration of class A, and pointer to A in class B, why this does not work?
Thanks a lot for answering a newbie's question.
-
April 20th, 2010, 02:33 PM
#2
Re: Help with friend function
Code:
int getValue(A* pa) {
return pa->va_;
}
The fact that A is a pointer here becomes irrelevant, once you try to de-reference it.
Viggy
-
April 20th, 2010, 02:36 PM
#3
Re: Help with friend function
Thanks for replying.
What do you mean irrelevant? If not use pointer, the forward declaration of class A will not work, right?
-
April 20th, 2010, 02:40 PM
#4
Re: Help with friend function
That doesn't matter. By trying to return the 'va_' member variable, the compiler needs to know the declaration of the class. Try it this way:
Code:
class A;
class B{
public:
int getValue(A* pa);
};
class A {
friend int B::getValue(A* pa);
public:
A():va_(1){}
private:
int va_;
};
inline int B::getValue(A* pa) {
return pa->va_;
}
Viggy
-
April 20th, 2010, 05:14 PM
#5
Re: Help with friend function
Got it! The key point here is whether I de-reference the pointer.
Another question. The reason I use pointer here is forward declaration. From what I learned, generally forward declaration works only with pointer or reference to the class.
I tried to replace A* with A&, the code is still ok and this is understandable. However, If I use pass by value, I was surprised to see the code still works.
Code:
class A;
class B{
public:
int getValue(A pa);
};
class A {
friend int B::getValue(A pa);
public:
A():va_(1){}
private:
int va_;
};
inline int B::getValue(A pa) {
return pa.va_;
}
So, since the inline getValue(A pa) is defined at the end (after definition of class A), it does not matter we use point, reference, or pass by value, with respect of forward declaration, right?
Last edited by featips; April 20th, 2010 at 05:52 PM.
-
April 20th, 2010, 06:04 PM
#6
Re: Help with friend function
Actually, it's just a quirk of the language: function parameters are always allowed to be forward-declared, no matter how they're passed.
The reason is that you only need the actual definition when you need either (a) the size of the object or (b) some member of the object. Since function parameters are pushed onto the stack at runtime rather than compile time, neither of those is necessary in order to declare function parameters.
-
April 20th, 2010, 06:27 PM
#7
Re: Help with friend function
Cool, 'function parameters are pushed onto the stack at runtime rather than compile time'...great, this is a wonderful forum!
 Originally Posted by Lindley
Actually, it's just a quirk of the language: function parameters are always allowed to be forward-declared, no matter how they're passed.
The reason is that you only need the actual definition when you need either (a) the size of the object or (b) some member of the object. Since function parameters are pushed onto the stack at runtime rather than compile time, neither of those is necessary in order to declare function parameters.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|