friend-function

Friend function access the private members of class defined in static library

假装没事ソ 提交于 2019-12-11 08:19:34
问题 I have a static library written in C++. I have also got the header files for the classes defined in the static library. Can I access the private members of the classes defined in the static library introducing a friend function in class declaration ? 回答1: You mean you want to change the headers that are shipped with the library? It's in no way guaranteed that adding friend declarations there will work. You might mess up the linking part, even if your compiler says it's ok. Also, if those

How can friend function be declared for only one particular function and class?

≡放荡痞女 提交于 2019-12-11 06:47:11
问题 What's wrong with my code? I tried to compile the code below in the GNU G++ environment and I get these errors: friend2.cpp:30: error: invalid use of incomplete type ‘struct two’ friend2.cpp:5: error: forward declaration of ‘struct two’ friend2.cpp: In member function ‘int two::accessboth(one)’: friend2.cpp:24: error: ‘int one::data1’ is private friend2.cpp:55: error: within this context #include <iostream> using namespace std; class two; class one { private: int data1; public: one() { data1

How can I use ADL in template deduction?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 06:06:10
问题 I have a class with an in-class defined friend function that I would ideally not modify (it comes from a header that's already deployed) #include <numeric> #include <vector> namespace our_namespace { template <typename T> struct our_container { friend our_container set_union(our_container const &, our_container const &) { return our_container{}; } }; } // namespace our_namespace set_union is not declared outside the struct or namespace, but can normally be found through argument-dependent

Bug of gcc? Access control issue about friend function in template class

邮差的信 提交于 2019-12-11 03:41:53
问题 I have a template class, and I define a friend function inside the class. #include <iostream> using namespace std; template <typename T> class template_class { T v; friend void foo(template_class t) { t.v = 1; // (1)can access the private member because it's a friend cout << t.v << endl; template_class<int> t1; t1.v = 2; // (2)accessible if instantiated with [T=int] cout << t1.v << endl; template_class<char> t2; t2.v = 'c'; // (3)should not be accessible too if instantiated with [T=int] cout

Why LNK1120 & LNK2019 appears in case of template and friend function

旧时模样 提交于 2019-12-11 01:44:00
问题 I have compiled the first version of code in Turbo-C and it compiles without any error. But when I compile this in Visual Studio or plain g++ from CommandLine, I get errors mentioned down in the post. I did searched over internet and read some of the StackOverflow questions & MSDN LNK1120 problem docs. I found a way to fix the below code. However, didn't got the reason clearly. How can just a definition gets rid of that error. Syntactically the error prone code also look fine. 1) Error 2

How to declare template member function of template class X as friend of nested class X::Y

余生颓废 提交于 2019-12-10 18:39:41
问题 I have a template class. It has a template function. Both take different template parameters. There is an internal class that needs to make a friend of the enclosing class's template function. Compiler errors abound. The following toy example shows my issue. First, the following of course compiles (VS 2017): template <typename T> class Class1 { public: Class1() = default; ~Class1() = default; template <typename U> void Func(U& x) {}; }; class Class2 { public: Class2() = default; ~Class2() =

Define friend function template of class template

好久不见. 提交于 2019-12-10 16:32:00
问题 I want to define a function template of a class template. The code looks like this. template<int M> struct test{ private: int value; template<int N = 2 * M> friend auto foo(test const t){ test<N> r; r.value = t.value; return r; } }; int main(){ test<1> t; foo(t);// expected to return test<2> foo<1>(t);// expected to return test<1> foo<3>(t);// expected to return test<3> } But it won't compile. Compared with previous problems, the following lists the differences. The result of the function

Restrict the scope of class instances accessible by multiple template parameter friend function

╄→尐↘猪︶ㄣ 提交于 2019-12-10 13:15:55
问题 I would like to know if what I am aiming for is possible. I have a class Class such that #include<iostream> template<class T> class Class; template<class T, class W> Class<W> f(Class<T>& C, const Class<T>& D); template<class T> class Class { protected: // this could be private T m_t; public: Class(): m_t(T()) {} Class(T t): m_t(t) {} T& getT() { return m_t; } template<class U, class W> friend Class<W> f(Class<U>& C, const Class<U>& D); }; template<class T, class W> Class<W> f(Class<T>& C,

Can 2 classes share a friend function?

会有一股神秘感。 提交于 2019-12-09 03:31:14
问题 Today i have a doubt regarding friend function. Can two classes have same friend function? Say example friend void f1(); declared in class A and class B. Is this possible? If so, can a function f1() can access the members of two classes? 回答1: An example will explain this best: class B; //defined later void add(A,B); class A{ private: int a; public: A(){ a = 100; } friend void add(A,B); }; class B{ private: int b; public: B(){ b = 100; } friend void add(A,B); }; void add (A Aobj, B Bobj){ cout

Undefined reference to friend function template defined inside class in a namespace

我的梦境 提交于 2019-12-07 18:24:35
问题 This is a follow-up on my answer to this question. The original answer I posted does not have any namespaces, and it solves the problem. However, the OP subsequently made an edit claiming that the solution does not work if the class is inside a namespace. My initial reaction was that you can make it work by simply having using N::f; either at global scope or inside main() (or any other function). While that is certainly the case, the OP (justifiably) commented that this is not ideal, and I