friend

Declare a member-function of a forward-declared class as friend

穿精又带淫゛_ 提交于 2019-11-27 05:37:56
问题 Is it possible to declare a member function of a forward-declared class as friend? I am trying to do the following: class BigComplicatedClass; class Storage { int data_; public: int data() { return data_; } // OK, but provides too broad access: friend class BigComplicatedClass; // ERROR "invalid use of incomplete type": friend void BigComplicatedClass::ModifyStorage(); }; So the goal is to (i) restrict the friend declaration to a single method, and (ii) not to include the definition of the

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

最后都变了- 提交于 2019-11-27 05:18:08
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? 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 probably want to benefit from automatic type conversions on both sides, therefore you'll go with a non

Error with multiple definitions of function

一个人想着一个人 提交于 2019-11-27 03:38:21
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(){ // ERROR HERE cout<<"enter funct"<<endl; classA tmp(1,2); tmp.outfun(); cout<<"exit funct"<<endl; } Second: //

'Friends' equivalent for Java? [duplicate]

人走茶凉 提交于 2019-11-27 03:05:06
问题 This question already has answers here : Closed 10 years ago . 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 the DTO 'map_objects' that actuall sets up these connectivities, by using this controller class. (Ie, even

Is it possible to declare a friend function as static?

雨燕双飞 提交于 2019-11-27 01:30:27
问题 Here is some C++ example code that compiles and works fine: class A { public: A() {/* empty */} private: friend void IncrementValue(A &); int value; }; void IncrementValue(A & a) { a.value++; } int main(int, char **) { A a; IncrementValue(a); return 0; } What I would like to do, however, is declare IncrementValue() as static, so that it can't be seen or called from another compilation unit: static void IncrementValue(A & a) { a.value++; } Doing that, however, gives me a compile error: temp

ampersand (&) at the end of variable etc

蓝咒 提交于 2019-11-27 01:09:03
问题 I am a C++ noob and i've a problem of understanding c++ syntax in a code. Now I am quite confused. class date { private: int day, month, year; int correct_date( void ); public: void set_date( int d, int m, int y ); void actual( void ); void print( void ); void inc( void ); friend int date_ok( const date& ); }; Regarding to the '&' character, I understand its general usage as a reference, address and logical operator... for example int *Y = &X What is the meaning of an & operator at end of

What is wrong with making a unit test a friend of the class it is testing? [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-11-27 00:16:29
问题 This question already has answers here : How do I test a private function or a class that has private methods, fields or inner classes? (51 answers) Closed last year . In C++, I have often made a unit test class a friend of the class I am testing. I do this because I sometimes feel the need to write a unit test for a private method, or maybe I want access to some private member so I can more easily setup the state of the object so I can test it. To me this helps preserve encapsulation and

Friend declaration in C++ - difference between public and private

大兔子大兔子 提交于 2019-11-26 23:54:49
问题 Is there a difference between declaring a friend function/class as private or public? I can't seem to find anything about this online. I mean the difference between: class A { public: friend class B; }; and class A { private: //or nothing as the default is private friend class B; }; Is there a difference? 回答1: No, there's no difference - you just tell that class B is a friend of class A and now can access its private and protected members, that's all. 回答2: Since the syntax friend class B

declare template friend function of template class

筅森魡賤 提交于 2019-11-26 22:15:08
I have a class template Obj and a function template make_obj . Obj has a private single constructor defined, which takes a reference to its templated type to bind to. template <typename T> class Obj { private: T& t; Obj(T& t) : t{t} { } }; template <typename T> Obj<T> make_obj(T& t) { return {t}; } What I want is to declare the make_obj function a friend so that it can create Obj 's, but no one else can (except via the copy ctor). I have tried several friend declaration including friend Obj make_obj(T&); and template <typename T1, typename T2> friend Obj<T1> make_obj(T2&); The latter being a

Friend scope in C++

元气小坏坏 提交于 2019-11-26 21:57:17
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. Martin York Friendship in C++ is not transitive: John is a friend of mine and he can use my wireless connection any time (I trust him). John's friend Tim though is a waster and though John is my friend I do not include Tim as a friend, and thus I don't let him use my wireless