LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-16-2010, 07:38 PM   #1
lesca
Member
 
Registered: Sep 2010
Posts: 58

Rep: Reputation: 0
How can a friend function access a public function from a base class?


Code:
#include <iostream>
using namespace std;
class Base
{
public:
	// this function may set some output attributes
	void foo();
};

class Derived : public Base
{
public:
	friend ostream& operator <<(ostream& outs, const Derived d);
};

ostream& operator <<(ostream& outs, const Derived d)
{
	foo();			// undeclared identifier
	/* OR */
	Base::foo();		// illegal call of non-static member function

	outs << "...";
	return outs;
}

void main()
{
	Derived d;
	cout << d;
}
Any tips are appreciated!
Thanks!

Update: 16:04, 17 Nov
I did some tests, and find one possible solution: (Any other solutions are welcome, if you have one.)
Code:
ostream& operator <<(ostream& outs, const Derived d)
{
	d.foo();		// Just do this!

	outs << "...";
	return outs;
}

Last edited by lesca; 11-17-2010 at 02:11 AM.
 
Old 11-17-2010, 01:56 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Of course, your "possible solution" is THE solution. The compiler error is exclaiming "?WHAT? foo?!?". Explicitly stating "d.foo()" fixes the problem.

Or you can always refactor your clases

In general, the fact that you're even USING "friend" should probably be regarded as a "yellow flag" that your design might have problems. You're usually much better off refactoring your classes than using "friend".

Here's a great link that might help:

http://www.parashift.com/c++-faq-lite/friends.html

PS:
It's uncoincidental that Java, C# and most (all?) other contemporary OO languages do *not* have a "friend" construct

PPS:
Before somebody flames me, I learned that .Net *does* have a "friend" construct:
Code:
Friend Assemblies in C# and VB.Net

Imports System.Runtime.CompilerServices
Imports System
<Assembly: InternalsVisibleTo("AssemblyB")> 

' Friend class.
Friend Class FriendClass
    Public Sub Test()
        Console.WriteLine("Sample Class")
    End Sub
End Class

' Public class with a Friend method.
Public Class ClassWithFriendMethod
    Friend Sub Test()
        Console.WriteLine("Sample Method")
    End Sub
End Class

Last edited by paulsm4; 11-17-2010 at 02:02 PM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript - class / function in function jchambers Programming 3 06-15-2009 10:33 PM
log base 2 function in Java manolakis Programming 4 10-26-2007 10:13 AM
how pass a class to a function aditya1 Programming 2 03-08-2005 09:02 PM
function prototypes in template class' (c++) qwijibow Programming 4 12-13-2004 09:34 AM
Function Definitoin Outside Templatized Class ashwinipahuja Programming 1 06-15-2004 10:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration