friend

Is friendship inherited in C++?

一世执手 提交于 2019-12-03 05:25:32
Suppose I have a Base class: class Base { friend SomeOtherClass; }; And there is another (different) class that inherits from Base : class AnotherClass : public Base {} Is friendship inherited as well? In principle, a derived class inherits every member of a base class except: * its constructor and its destructor * its operator=() members * its friends So, no. Friends are not inherited. No it isn't. Edit: To quote from the C++ Standard, section 11.4/8 Friendship is neither inherited nor transitive. No it isn't, as documented here: http://www.parashift.com/c++-faq-lite/friends.html#faq-14.4 来源:

In C# 4.0, is there any way to make an otherwise private member of one class available only to a specific other class?

好久不见. 提交于 2019-12-03 04:58:14
问题 We're creating an object hierarchy where each item has a collection of other items, and each item also has a Parent property pointing to its parent item. Pretty standard stuff. We also have an ItemsCollection class that inherits from Collection<Item> which itself has an Owner property pointing to the item the collection belongs to. Again, nothing interesting there. When an item is added to the ItemsCollection class, we want it to automatically set the parent of Item (using the collection's

When should you use friend classes? [duplicate]

冷暖自知 提交于 2019-12-03 03:45:58
This question already has answers here : When should you use 'friend' in C++? (30 answers) Possible Duplicate: When should you use 'friend' in C++? I have come to a stumbling block because of lack of documentation on friend classes. Most books just explain it briefly, e.g an excerpt from C++: the Complete Reference : Friend Classes are seldom used. They are supported to allow certain special case situations to be handled. And frankly, I have never seen a friend class in any good code made by an experienced C++ programmer. So , here is my list of problems. Do Inherited Classes have the same

Virtual friend functions for a base class?

China☆狼群 提交于 2019-12-03 02:44:04
I'm in the proccess of learning the language and this is a noob doubt. Is it possible to use a virtual friend function? I don't know if it's possible, I didn't even test it but it could be useful in some situations. For example, for the overloaded operator<<(). DerivedClass dc; BaseClass &rbc = dc; cout << rbc; My guess is it's possible, but I'm not sure since a friend function is not implemented in the class design, and theoretically is not part of it (though in this example, conceptually it makes sense that operator<<() should be a method, but due to syntax limitations it's not possible to

In C++, why isn't it possible to friend a template class member function using the template type of another class?

不打扰是莪最后的温柔 提交于 2019-12-03 01:16:37
In other words, why does this compile fine : template<typename Type> class A{ public: void f(); }; class B{ friend void A<int>::f(); }; template<> void A<int>::f(){ B* var = new B(); } While this doesn't : template<typename Type> class A{ public: void f(); }; template<typename Type> // B is now a templated class class B{ friend void A<Type>::f(); // Friending is done using B templated type }; template<> void A<int>::f(){ B<int>* var = new B<int>(); // var is now declared using int as its templated type } For the second code snippet, compiler (gcc 6.2, no special flags) says: main.cpp: In

How to make google-test classes friends with my classes?

与世无争的帅哥 提交于 2019-12-02 20:09:53
I heard there is a possibility to enable google-test TestCase classes friends to my classes, thus enabling tests to access my private/protected members. How to accomplish that? Try this (straight from Google Test docs...): FRIEND_TEST(TestCaseName, TestName); For example: // foo.h #include <gtest/gtest_prod.h> // Defines FRIEND_TEST. class Foo { ... private: FRIEND_TEST(FooTest, BarReturnsZeroOnNull); int Bar(void* x); }; // foo_test.cc ... TEST(FooTest, BarReturnsZeroOnNull) { Foo foo; EXPECT_EQ(0, foo.Bar(NULL)); // Uses Foo's private member Bar(). } Ralfizzle I know this is old but I was

Friend methods in C++ is not working

不打扰是莪最后的温柔 提交于 2019-12-02 19:51:04
问题 I wrote the following code: class Osoba{ private: string imie, nazwisko, kolorOczu; friend void Dziecko::coutall(); public: Osoba(string imie, string nazwisko, string kolorOczu):imie(imie), nazwisko(nazwisko), kolorOczu(kolorOczu){}; void coutall(){ cout << "Imie: " << imie << endl; // cout << "Nazwisko: " << nazwisko << endl; cout << "Kolor oczu: " << kolorOczu << endl; } }; class Dziecko: public Osoba{ private: string nazwaPrzedszkola, choroba; typedef Osoba super; public: Dziecko(string

friend member function in C++ - forward declaration not working

烈酒焚心 提交于 2019-12-02 11:57:44
I'm having a situation similar to the one described in Specify a class member function as a friend of another class? . However in my case, class B needs to know class A since it's using it, so the solution given in that thread is not working for me. I tried to give also a forward declaration to the function itself but it didn't work as well. It seems that each class need the full definition of the other... Is there any easy way to solve it? I prefer a solution which doesn't involve new classes which wrap one of the old classes. code example: //A.h class B; //not helping void B::fB(); //not

Friend functions and namespaces. Cannot access private member in class

只谈情不闲聊 提交于 2019-12-02 06:35:57
问题 So I have a class inside a foo namespace, which includes a friend function. Now I want the definition of the friend function to be in a different namespace bar so it can be called the way you see below. The error I get is that the private member val cannot be accessed. Question: Why? #include <iostream> namespace foo { template<typename T> class myclass { private: T val; public: myclass(T v) : val(v) {} template<class U> friend void myfun(myclass<U>); }; namespace bar { template<class U> void

how to overload operator == outside template class using friend function?

妖精的绣舞 提交于 2019-12-02 04:27:17
问题 I'm trying to write a template class which overloads operator== . I know how to get it inside the class: template <typename T> class Point { private: T x; public: Point(T X) : x(X) {} bool operator== (Point &cP) { return (cP.x == x); } }; But now I want to achieve this outside the template class. I've read this post: error when trying to overload << operator and using friend function and add template declaration in my code: template <typename> class Point; template <typename T> bool operator=