friend

Is a friend function defined in-class automatically inline?

旧时模样 提交于 2019-12-01 15:00:55
问题 If a member function is defined inside the class, it is an inline function. E.g. struct X { void mem_f() {} //mem_f is inline }; My question is whether a nonmember friend function defined inside the class is also automatically inline. E.g. struct Y { friend void friend_f() {} //is friend_f inline? }; A relevant quote/paragraph_no from the standard would be much welcome. Thanks. 回答1: Yes, it is. §11.4/5: A function can be defined in a friend declaration of a class if and only if the class is a

Strange behavior of templated operator<<

给你一囗甜甜゛ 提交于 2019-12-01 13:56:26
I cant understand a behavior of operator<< in my class: header: #ifndef VECTOR_H_ #define VECTOR_H_ #include <string> #include <iostream> template<class T> class Vector { static const int EXPANDER = 10; T* array; int next; int length; void expand(); void contract(); public: Vector(); Vector(const Vector& v); void add(const T e); T get(int index) const; bool removeByIndex(int index); bool remove(T e); int size() const; T operator[](int i) const; T& operator+=(const T& t); T operator+(const T& s); friend std::ostream& operator<< (std::ostream& os, const Vector<T>& obj); friend std::istream&

Strange behavior of templated operator<<

好久不见. 提交于 2019-12-01 12:51:38
问题 I cant understand a behavior of operator<< in my class: header: #ifndef VECTOR_H_ #define VECTOR_H_ #include <string> #include <iostream> template<class T> class Vector { static const int EXPANDER = 10; T* array; int next; int length; void expand(); void contract(); public: Vector(); Vector(const Vector& v); void add(const T e); T get(int index) const; bool removeByIndex(int index); bool remove(T e); int size() const; T operator[](int i) const; T& operator+=(const T& t); T operator+(const T&

incomplete type error

元气小坏坏 提交于 2019-12-01 08:59:55
Im trying to make class A a friend of class B. class B; class A{ public: void show(const B&); // ##1## but this one works fine B ob;// error incomplete type }; class B{ public: int b; B():b(1){} friend class A; }; so my question why it's incomplete type? I thought that when I did class B it's like a prototype of a function which tell the compile there is a definition somewhere in the code. also in the code above at ##1## why this is possible ? No, that's a forward declaration and does not define a full type. You'll need to have a full definition of B before A , if you want to keep the member

how do I call an inline friend function with the same name as a member function?

喜夏-厌秋 提交于 2019-12-01 07:13:47
问题 As described here C++11 style SFINAE and function visibility on template instantiation class member functions overshadow free functions. Using a fully qualified name usually works, however I am having a hard time with friend functions of other classes which are declared in-line. Consider the following example: namespace N { struct C { friend int f(const C& c) { return 1; } friend int g(const C& c) { return 2; } }; struct D { void f() { g(C{}); // ADL finds this ::N::f(C{}); // not found

Calling friend function defined in struct requires forward declaration?

一曲冷凌霜 提交于 2019-12-01 05:48:59
While reading Karlsson's Beyond the C++ Standard the author defined the friend function intrusive_ptr_add_ref in the body of class reference_counted (see pg 36). That function is called automatically using Argument Dependent Lookup at the proper time. Never having seen friend functions defined in the body of a class, I played around and discovered that gcc 4.4.3 requires a forward declaration if not using ADL lookup. In fact, there seems to be no way to reference adl_no without that forward declaration. Is this part of the C++ standard or is it an artifact of gcc? (I don't have Windows box so

C++-like friend class mechanism in Java

旧巷老猫 提交于 2019-12-01 05:23:24
Do you know how can I make an object changeable only inside a special class? In this example I want the object PrivateObject to be only changable (incrementable) inside the Box class, nowhere else. Is there a way to achieve this? public class Box { private PrivateObject prv; public void setPrivateObject(PrivateObject p){ prv = p; } public void changeValue(){ prv.increment(); } } public class PrivateObject { private value; public increment(){ value++; } } PrivateObject priv = new PrivateObject (); Box box = new Box(); box.setPPrivateObject(priv); box.changevalue(); priv.increment(); // I don't

How to declare two classes such that A has members of B and B marks members of A as friends?

浪尽此生 提交于 2019-12-01 04:57:26
I am attempting to do exercise 7.32 from C++ Primer 5th Edition. That exercise asks the following: Define your own versions of Screen and Window_mgr in which clear is a member of Window_mgr and a friend of Screen . Here are the definitions for Screen , Window_mgr and clear given in the text. class Screen { public: using pos = std::string::size_type; Screen(pos ht, pos wd, char c) : height(ht), width(wd), contents(ht * wd, c) { } private: pos height = 0, width = 0; std::string contents; }; class Window_mgr { public: using ScreenIndex = std::vector<Screen>::size_type; void clear(ScreenIndex);

Calling friend function defined in struct requires forward declaration?

故事扮演 提交于 2019-12-01 04:28:53
问题 While reading Karlsson's Beyond the C++ Standard the author defined the friend function intrusive_ptr_add_ref in the body of class reference_counted (see pg 36). That function is called automatically using Argument Dependent Lookup at the proper time. Never having seen friend functions defined in the body of a class, I played around and discovered that gcc 4.4.3 requires a forward declaration if not using ADL lookup. In fact, there seems to be no way to reference adl_no without that forward

C++11 Declaring factory a friend of base class

江枫思渺然 提交于 2019-12-01 03:55:45
I'm trying to create a factory for derived classes. I only want the factory to be able to create instances of the derived classes so I've made the base constructor protected ; the derived classes just use the base class constructors so their constructors are protected also. I tried to declare the factory as a friend of the base class so that it could access the protected constructor. When I compile using this command clang++ -std=c++11 -stdlib=libc++ Friends.cpp -o Friends I get this error: Friends.cpp:23:20: error: calling a protected constructor of class 'A' return new T(i); ^ Friends.cpp:42