friend

How can I remove/refactor a «friend» dependency declaration properly?

允我心安 提交于 2019-12-17 02:24:45
问题 The background of this question is based on a practical sample where I wanted to remove a «friend» dependency from a pair of classes that are used to manage read/write locked access to a shared resource. Here's an abstraction of the original structural design for that scenario: Marked in red, there's this ugly «friend» dependency I want to remove from the design. In short, why do I have this thing there: ClassAProvider shares a reference to a ClassA over a number of concurrently accessing

overloading friend operator<< for template class

南笙酒味 提交于 2019-12-16 19:31:10
问题 I have read couple of the questions regarding my problem on StackOverflow.com now, and none of it seems to solve my problem. Or I maybe have done it wrong... The overloaded << works if I make it into an inline function. But how do I make it work in my case? warning: friend declaration std::ostream& operator<<(std::ostream&, const D<classT>&)' declares a non-template function warning: (if this is not what you intended, make sure the function template has already been declared and add <> after

C++ 友元函数友元类

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-15 00:18:10
友元函数是可以直接访问类的私有成员的非成员函数。它是定义在类外的普通函数,它 不属于任何类,但需要在类的定义中加以声明,声明时只需在友元的名称前加上关键字friend,其格式如下: friend 类型 函数名(形式参数); 友元函数的声明可以放在类的私有部分,也可以放在公有部分,它们是没有区别的,都说明是该类的一个友元函数。 一个函数可以是多个类的友元函数,只需要在各个类中分别声明。 友元函数的调用与一般函数的调用方式和原理一致。 友元类 : 友元类的所有成员函数都是另一个类的友元函数,都可以访问另一个类中的隐藏信息(包括私有成员和保护成员)。 当希望一个类可以存取另一个类的私有成员时,可以将该类声明为另一类的友元类。定义友元类的语句格式如下: friend class 类名; 其中:friend和class是关键字,类名必须是程序中的一个已定义过的类。 以下语句说明类B是类A的友元类: class A { … public: friend class B; … }; 经过以上说明后,类B的所有成员函数都是类A的友元函数,能存取类A的私有成员和保护成员。 使用友元类时注意: (1) 友元关系不能被继承。 (2) 友元关系是单向的,不具有交换性。若类B是类A的友元,类A不一定是类B的友元,要看在类中是否有相应的声明。 (3) 友元关系不具有传递性。若类B是类A的友元,类C是B的友元

Difference between nested class and befriending a derived class in base class

ぐ巨炮叔叔 提交于 2019-12-14 03:24:35
问题 What is the difference between creating class P with all members as private and befriending class B which is derived from P class P { private: int i; friend class B; }; class B: public P { public: void get() { cout <<i; } }; int main() { B obj2; obj2.get(); return 0; } vs Creating a class within a class so that nobody can use the hidden class(subclass) it would be easy for maintanence without having to worry about breaking someone elses dependent code. In which situation do we choose which

Operator override - when to use friend?

六月ゝ 毕业季﹏ 提交于 2019-12-14 02:08:35
问题 I'm wondering why the () operator override can't be "friend" (and so it needs a "this" additional parameter) while the + operator needs to be friend like in the following example: class fnobj { int operator()(int i); friend int operator+(fnobj& e); }; int fnobj::operator()(int i) { } int operator+(fnobj& e) { } I understood that the + operator needs to be friend to avoid the "additional" extra this parameter, but why is that the operator() doesn't need it? 回答1: You have overloaded the unary

Error: 'Friend Member Function Name' was not declared in this scope

冷暖自知 提交于 2019-12-13 13:06:21
问题 I am in the process of moving all of my C++ Windows applications to Ubuntu Linux. This application runs fine on Visual Studio 2015 Community on Windows 7 OS. However, it gives an error when running in Code Blocks on Ubuntu Linux. I have replicated the error message I am getting using the following simple Person class. Error Message: 'comparePersonAge' was not declared in this scope Person.h #ifndef Person_h #define Person_h #include <string> class Person { private: int age; std::string name;

Friendness and derived class

好久不见. 提交于 2019-12-12 20:17:22
问题 Let's say I have the following class hierarchy: class Base { protected: virtual void foo() = 0; friend class Other; }; class Derived : public Base { protected: void foo() { /* Some implementation */ }; }; class Other { public: void bar() { Derived* a = new Derived(); a->foo(); // Compiler error: foo() is protected within this context }; }; I guess I could change it too a->Base::foo() but since foo() is pure virtual in the Base class, the call will result in calling Derived::foo() anyway.

Friend template overloaded operator <<: unresolved external symbol

这一生的挚爱 提交于 2019-12-12 17:43:55
问题 I'm having issues with the error Error LNK2019 unresolved external symbol "class std::basic_ostream > & __cdecl cop4530::operator<<(class std::basic_ostream > &,class rob::Stack const &)" (??6rob@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV12@ABV?$Stack@H@0@@Z) referenced in function _main Project7 c:\Users\Robrik\documents\visual studio 2015\Projects\Project7\Project7\post.obj 1 Right now, all that post is doing is calling the operator<< The declaration namespace rob { template <

How to make the lambda a friend of a class?

这一生的挚爱 提交于 2019-12-12 08:33:53
问题 Let's say, I have a class: class A { int a; }; And I have a lambda: auto function = [](A* a) { a->a; // <== gives an error in this line. }; function(new A); Is there any way to use a private member/method inside a lambda? - It's not necessary to pass the pointer to the lambda - it may be a capture-by or something else. All reasonable schemes are welcome. 回答1: You can do it by creating a friend function that returns the lambda function. It inherits the friend access: struct A { friend std:

Social networking v2

寵の児 提交于 2019-12-12 05:30:52
问题 I'm trying to make a network of friends like in this example previous to the final correction of JenB in here, since it is more comprehensible for me and Bryan Head said it would be the way he would do it with fewer agents. I'm planning to use around 200, maybe 300 agents. My question is... how could I do it without forming pairs? I want the agents to make friends with every agent in radius x during y ticks and with the same color (I'm still working on the tick condition). I tried with if and