friend

Is this key-oriented access-protection pattern a known idiom?

做~自己de王妃 提交于 2019-11-26 12:50:52
Matthieu M. brought up a pattern for access-protection in this answer that i'd seen before, but never conciously considered a pattern: class SomeKey { friend class Foo; SomeKey() {} // possibly make it non-copyable too }; class Bar { public: void protectedMethod(SomeKey); }; Here only a friend of the key class has access to protectedMethod() : class Foo { void do_stuff(Bar& b) { b.protectedMethod(SomeKey()); // fine, Foo is friend of SomeKey } }; class Baz { void do_stuff(Bar& b) { b.protectedMethod(SomeKey()); // error, SomeKey::SomeKey() is private } }; It allows more fine-granular access

&#39;friend&#39; functions and << operator overloading: What is the proper way to overload an operator for a class?

烂漫一生 提交于 2019-11-26 11:57:48
问题 In a project I\'m working on, I have a Score class, defined below in score.h . I am trying to overload it so, when a << operation is performed on it, _points + \" \" + _name is printed. Here\'s what I tried to do: ostream & Score::operator<< (ostream & os, Score right) { os << right.getPoints() << \" \" << right.scoreGetName(); return os; } Here are the errors returned: score.h(30) : error C2804: binary \'operator <<\' has too many parameters (This error appears 4 times, actually) I managed

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

谁说胖子不能爱 提交于 2019-11-26 11:52:19
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 Client instances Client instances should access ClassA solely through the ClassAAccessor helper class that

Overload operators as member function or non-member (friend) function?

一曲冷凌霜 提交于 2019-11-26 11:30:56
问题 I am currently creating a utility class that will have overloaded operators in it. What are the pros and cons of either making them member or non-member ( friend ) functions? Or does it matter at all? Maybe there is a best practice for this? 回答1: Each operator has its own considerations. For example, the << operator (when used for stream output, not bit shifting) gets an ostream as its first parameter, so it can't be a member of your class. If you're implementing the addition operator, you'll

What&#39;s the scope of inline friend functions?

爱⌒轻易说出口 提交于 2019-11-26 10:32:39
After searching aroung SO, one question taught me that the lexical scope of an inline friend function is the class it's defined in, meaning it can access e.g. the typedef s in the class without qualifying them. But then I wondered what is the actual scope of such a function? GCC at least rejects all my attempts to call it. Can a function such as in the example ever be called through means other than ADL, which is not possible here thanks to no arguments? Standard quotations are appreciated, as I currently can't access my copy of it. The following code namespace foo{ struct bar{ friend void baz

Error with multiple definitions of function

落爺英雄遲暮 提交于 2019-11-26 10:31:06
问题 I am trying to relearn C++ after taking an intro course a few years ago and I’m having some basic problems. My current problem occurs when trying to use a friend function. Here is my code in 2 files. First: // fun.cpp #include <iostream> using namespace std; class classA { friend void funct(); public: classA(int a=1,int b=2):propa(a),propb(b){cout<<\"constructor\\n\";} private: int propa; int propb; void outfun(){ cout<<\"propa=\"<<propa<<endl<<\"propb=\"<<propb<<endl; } }; void funct(){ //

Are inner classes in C++ automatically friends?

冷暖自知 提交于 2019-11-26 06:36:33
问题 If I define an inner class in C++, is it automatically a friend of the class that contains it? For example, is this legal: class Outer { public: class Inner { public: void mutateOuter(Outer& o); }; private: int value; }; void Outer::Inner::mutateOuter(Outer& o) { o.value ++; // Legal? Or not? } I ask because on some compilers I\'ve tried (VS2003) this code won\'t work, but I\'ve heard at least anecdotally that it does work on some compilers. I can\'t find a relevant section in the C++ spec

友元函数

筅森魡賤 提交于 2019-11-26 05:58:46
类的友元函数是定义在类外部,但有权访问类的所有私有成员和保护成员。尽管友元函数的原型有在类的定义中出现过,但友元函数并不是成员函数。 友元可以是一个函数,该函数被称为友元函数;友元也可以是一个类,该类被称为友元类。在这种情况下,整个类及其所有成员都是友元。如果要声明函数为一个类的友元,需要在类定义中该函数原型前使用关键字friend,如下所示: class Box { double width; public: double length; friend void printWidth(Box box); void setWidth(double wid); }; 声明类ClassTwo的所有成员函数作为类的ClassOne的友元,需要在类ClassOne的定义中放置如下声明: friend class ClassTwo; /*** friend.cpp ***/ #include<iostream> using namespace std; class Box { double width; public: friend void printWidth(Box box); void setWidth(double wid); }; void Box::setWidth(double wid) { width = wid; } void printWidth(Box box) {

Why does C++ not allow inherited friendship?

帅比萌擦擦* 提交于 2019-11-26 05:22:43
问题 Why is friendship not at least optionally inheritable in C++? I understand transitivity and reflexivity being forbidden for obvious reasons (I say this only to head off simple FAQ quote answers), but the lack of something along the lines of virtual friend class Foo; puzzles me. Does anyone know the historical background behind this decision? Was friendship really just a limited hack that has since found its way into a few obscure respectable uses? Edit for clarification: I\'m talking about

How to declare a templated struct/class as a friend?

落花浮王杯 提交于 2019-11-26 04:25:21
问题 I\'d like to do the following: template <typename T> struct foo { template <typename S> friend struct foo<S>; private: // ... }; but my compiler (VC8) chokes on it: error C3857: \'foo<T>\': multiple template parameter lists are not allowed I\'d like to have all possible instantiations of template struct foo friends of foo<T> for all T . How do I make this work ? EDIT: This template <typename T> struct foo { template <typename> friend struct foo; private: // ... }; seems to compile, but is it