friend

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

对着背影说爱祢 提交于 2019-11-28 08:39:58
问题 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 should not be part of the class. They should be nonmembers and nonfriends. The arguments are that: It promotes encapsulation, because there is less code that

ampersand (&) at the end of variable etc

二次信任 提交于 2019-11-28 06:16:53
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 parameter? friend int date_ok( const date& ); Thanks edit: Thanks for the answers. If I have understood

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

感情迁移 提交于 2019-11-28 06:02:36
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 complicated class to reduce compile time. One approach might be to add a class acting as an intermediary:

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

谁说胖子不能爱 提交于 2019-11-28 04:04:37
This question already has an answer here: How do I test a private function or a class that has private methods, fields or inner classes? 50 answers 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 abstraction because I am not modifying the public or protected interface of the class. If I buy a third party library, I

friend class with inheritance

丶灬走出姿态 提交于 2019-11-28 03:13:24
问题 If I have two Classes as follows with inheritance: class A { ... } class B : public A { ... } And a third class with defined as a friend class A: class C { friend class A; } Will I be able to access from class B (which is also an object of type A ) all members of class C as if I had defined class B the friend Class in the first place? 回答1: friend ship is neither inherited nor transitive. It is strictly one-one relationship between two classes. class A { friend class B; int Aries; }; class B {

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

对着背影说爱祢 提交于 2019-11-28 03:02:56
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? 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. Since the syntax friend class B doesn't declare a member of the class A , so it doesn't matter where you write it, class B is a friend of class A .

Member function a friend

久未见 提交于 2019-11-28 02:08:46
问题 I've been trying some examples in a book (C++ Primer by Stanley Lippman) and I understand that a class can make another class its friend (to access some private members). Now I'm reading about a member function being a friend and I try the example class Screen { public: friend void Window_mgr::clear(); typedef std::string::size_type pos; Screen () = default; Screen (pos ht, pos wd, char c) : height (ht), width (wd), contents (ht * wd, c) { } private: void do_display (std::ostream &os) const {

public friend swap member function

馋奶兔 提交于 2019-11-27 19:40:44
问题 In the beautiful answer to the copy-and-swap-idiom there is a piece of code I need a bit of help: class dumb_array { public: // ... friend void swap(dumb_array& first, dumb_array& second) // nothrow { using std::swap; swap(first.mSize, second.mSize); swap(first.mArray, second.mArray); } // ... }; and he adds a note There are other claims that we should specialize std::swap for our type, provide an in-class swap along-side a free-function swap, etc. But this is all unnecessary: any proper use

How to send friend request using Facebook dialogs on Android?

白昼怎懂夜的黑 提交于 2019-11-27 19:02:59
问题 I want tot send a friend request using the Facebook Android SDK. I'm currently using this code (which I got from here): Bundle parameters = new Bundle(); parameters.putString("APP_ID","USERNAME"); facebook.dialog(this, "friends", parameters, this); where APP_ID is the Facebook ID of my app and USERNAME is the username of the friend I want to add. This leads to the following error: API Error Code:100 Invalid parameter The parameter id is required I thought the id parameter meant the APP_ID. I

How do you mark a struct template as friend?

北城余情 提交于 2019-11-27 14:45:29
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 ? Rob Walker class IWantToBeFriendsWithMyStruct { template <typename T, typename U> friend struct MyStruct; }; Works in VS2008, and allows MyStruct to access the class. According to this site , the correct syntax would be class IWantToBeFriendsWithMyStruct { template <typename T, typename U> friend struct MyStruct; } 来源: https:/