friend

When should you use 'friend' in C++?

I have been reading through the C++ FAQ and was curious about the friend declaration. I personally have never used it, however I am interested in exploring the language. What is a good example of using friend? Edit: Reading the FAQ a bit longer I like the idea of the << >> operator overloading and adding as a friend of those classes....

Class design vs. IDE: Are nonmember nonfriend functions really worth it?

In the (otherwise) excellent book C++ Coding Standards, Item 44, titled "Prefer writing nonmember nonfriend functions", Sutter and Alexandrescu recommend that only functions that really need access to the members of a class be themselves members of that class. All other operations which can be written by using only member functions shoul...

Is there a way to simulate the C++ 'friend' concept in Java?

I would like to be able to write a Java class in one package which can access non-public methods of a class in another package without having to make it a subclass of the other class. Is this possible? ...

Why does C# not provide the C++ style 'friend' keyword?

The C++ friend keyword allows a class A to designate class B as it's friend. This allows Class B to access the private/protected members of class A. I've never read anything as to why this was left out of C# (and VB.NET). Most answers to this earlier StackOverflow question seem to be saying it is a useful part of C++ and there are go...

How do you mark a struct template as friend ?

I have code like this: template <typename T, typename U> struct MyStruct { T aType; U anotherType; }; class IWantToBeFriendsWithMyStruct { friend struct MyStruct; //what is the correct syntax here ? }; What is the correct syntax to give friendship to the template ? ...

c++ class friend

Hi, I'm trying to compile such code: #include <iostream> using namespace std; class CPosition { private: int itsX,itsY; public: void Show(); void Set(int,int); }; void CPosition::Set(int a, int b) { itsX=a; itsY=b; } void CPosition::Show() { cout << "x:" << itsX << " y:" << itsY << endl; } class CCube { fri...

Using "friend"-declarations for unit testing. Bad idea?

[Of course, the question is not restricted to a specific "friend" implementation, feel free though to point out implementation specifics if relevant] Reading through the unanswered questions, I stumbled upon the InternalsVisibleTo attribute: Specifies that types that are ordinarily visible only within the current assembly are vi...

PHP equivalent of friend or internal

Is there some equivalent of "friend" or "internal" in php? If not, is there any pattern to follow to achieve this behavior? Edit: Sorry, but standard Php isn't what I'm looking for. I'm looking for something along the lines of what ringmaster did. I have classes which are doing C-style system calls on the back end and the juggling has...

'Friends' equivalent for Java?

Hi guys, having a little architectural trouble here. In C++, we have the notion of 'friends,' where such friend classes can access private members. So, I'm deving a Java app and trying to adhere to the MVC architecture. I've got a controller class that manages graph connectivity between 'map_objects.' I'd like to hide the function in t...

C++ : friend AND inline method, what's the point ?

Hello, I see in a header that I didn't write myself the following : class MonitorObjectString: public MonitorObject { // some other declarations friend inline bool operator==(MonitorObjectString& lhs, MonitorObjectString& rhs) { return(lhs.fVal==rhs.fVal); } I can't understand why this method is declared as friend. I thought ...

Friend scope in C++

If I have three classes, A, B, C. A and B are friends (bidirectionally). Also, B and C are friends (bidirectionally). A has a pointer to B and B has a pointer to C. Why can't A access C's private data through the pointer? Just to clarify: This is a pure theoretical C++ language question, not a design advice question. ...

What is the equivalant of a 'friend' keyword in C Sharp?

What is the equivalant of a 'friend' keyword in C Sharp? How do I use the 'internal' keyword? I have read that 'internal' keyword is a replacement for 'friend' in C#. I am using a dll in my C# project that I have the source code for and yet I do not want to modify the existing code. I have inherited the class and I can use my inherite...

Friend template functions (in non-template classes), C++

If I have a non-template (i.e. "normal") class and wish to have a template friend function, how do I write it without causing a compiler error? Here is an example to illustrate what I am trying to do: template <class T> void bar(T* ptr); class MyClass // note that this isn't a template class { private: void foo(); template <...

friend class : inherited classes are not friend as well ?

In C++, I have a class A which is friend with a class B. I looks like inherited classes of B are not friend of class A. I this a limitation of C++ or my mistake ? Here is an example. When compiling, I get an error on line "return new Memento": Memento::Memento : impossible to access private member declared in Memento. class Originat...

When to use friend class in c++

I was just brushing up on my cpp (I'm a java developer) and I came across the Friend class keyword which I forgot about for a while. Is this one of those features that's just part of the kitchen sink, or is there a good reason for doing this rather than just a vanilla getter? I understand the difference in that it limits who can access t...

MSBuild fails to build C++/CLI friend assemblies

I have Visual Studio 2008 SP1, two C++/CLI projects, lets say proj1 and proj2. proj2 is dependent on proj1, but in a weird way (see below). In Project Dependencies I specify that proj2 depends on proj1. Also proj2 references include proj1. Then I want proj1 to be a friend to proj2, so, as MSDN page on "Friend Assemblies" says, I write so...

In what scenarios should one declare a member function a friend?

In what kind of scenarios would we declare a member function as a 'friend function' ?..What exact purpose does 'friend function' which defies one of central concept of 'Encapsulation' of OOP serve? ...

Is C++ friendship among peers healthy?

Although class friendship is one of the last resorts of C++, does this pattern make sense? class Peer { public: friend class Peer; void GetSecret(const Peer& other) { const std::string& secret = other.GiveSecret(); std::cout << secret << std::endl; } private: const std::string& GiveSecret() const { ...

How do I unit test private functions from a separate project in VB .NET?

As I develop code, I often want to unit test some of the building blocks of a class even if they are normally private. If my unit tests are inside the project, I can use "Friend" to accomplish this and still keep the functions private for normal use. But I would rather move my NUnit tests into their own separate project(s). How do I achi...

a class-key must be declared when declaring a friend

The g++ compiler complains with this error when I declare a friend thusly: friend MyClass; instead of friend class MyClass; Why should the class keyword be required? (the Borland C++ compiler, BTW, does not require it.) Couldn't the compiler simply look-up MyClass in the symbol table and tell it was declared as a class? (it is ob...