friend

Can a friend class object access base class private members on a derived class object?

无人久伴 提交于 2019-12-05 14:21:10
问题 I'm surprised that the code below compiles. It seems that a class befriended to the (publicly inherited) base class can access a member of the base class provided an instance of the derived class. If the inheritance is changed to private then compilation fails. In short, how is d.b_var valid within F::func(D& d) ? #include <iostream> #include <string> using namespace std; class B{ int b_var; friend class F; }; class D: public B{ int d_var; }; class F{ public: void func(D &d){ d.b_var = 5; } }

Friend template functions (in non-template classes), C++

早过忘川 提交于 2019-12-05 13:28:41
If I have a non-template (i.e. "normal") class and wish to have a template friend function, how do I write it without causing a compiler error? Here is an example to illustrate what I am trying to do: template <class T> void bar(T* ptr); class MyClass // note that this isn't a template class { private: void foo(); template <class T> friend void bar(T*); // ERROR: compiler gives me all kinds of grief }; template <class T> void bar(T* ptr) { if (ptr) { MyClass obj; obj.foo(); } } I'm using Visual Studio 2005, and the specific error I'm given is error C2063 , stating that "bar" isn't a function.

What is the equivalent of a 'friend' keyword in C Sharp?

£可爱£侵袭症+ 提交于 2019-12-05 09:51:40
问题 What is the equivalent of a 'friend' keyword in C Sharp? How do I use the 'internal' keyword? I have read that 'internal' keyword is a replacement for 'friend' in C#. I am using a DLL in my C# project that I have the source code for and yet I do not want to modify the existing code. I have inherited the class and I can use my inherited class any way I want. The problem is that most of the code in the parent class has protected methods. Will using a friend somehow make it possible to access or

PHP friend/package visibility

强颜欢笑 提交于 2019-12-05 02:29:16
Is there any way to limit the visibility in PHP in the same way as "package" visibility works in Java or at least "friend" visibility in C++? What's the best practice to maintain large OOP project and not to let anyone use any part of code? I use private and protected visibility as much as I can but sometimes it's not enough. I know about this request: https://bugs.php.net/bug.php?id=55331 . Is there any progress in implementing such thing to PHP? Is there any workaround to protect your code (methods, class variables) from being accessed from anywhere? CodeCaster As stated here : No. You can

g++ and clang++ different behaviour with friend template function defined inside a template class

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 02:09:25
Another question of type "who's right between g++ and clang++?" for C++ standard gurus. The following code template <int> struct foo { template <typename> friend void bar () { } }; int main () { foo<0> f0; foo<1> f1; } compile without problem with clang++ (only two "unused variable" warnings) but give a the following error tmp_002-11,14,gcc,clang.cpp: In instantiation of ‘struct foo<1>’: tmp_002-11,14,gcc,clang.cpp:27:12: required from here tmp_002-11,14,gcc,clang.cpp:20:16: error: redefinition of ‘template<class> void bar()’ friend void bar () ^~~ tmp_002-11,14,gcc,clang.cpp:20:16: note:

C++ friend function hidden by class function?

喜欢而已 提交于 2019-12-05 01:36:05
Minimal example: class A { friend void swap(A& first, A& second) {} void swap(A& other) {} void call_swap(A& other) { swap(*this, other); } }; int main() { return 0; } g++ 4.7 says: friend.cpp: In member function ‘void A::call_swap(A&)’: friend.cpp:7:20: error: no matching function for call to ‘A::swap(A&, A&)’ friend.cpp:7:20: note: candidate is: friend.cpp:4:7: note: void A::swap(A&) friend.cpp:4:7: note: candidate expects 1 argument, 2 provided Outcomment line 4: // void swap(A& other) {} ...and it works fine. Why, and how to fix this, if I want to keep both variants of my swap function? I

Using variadic templates to specify friend classes

半腔热情 提交于 2019-12-04 23:42:08
问题 I'm trying to use variadic templates to specify friend classes. I try with the following syntax, but it doesn't work. template <class... Args> struct A { friend Args...; }; I try to code some workarounds, but it seems to be not so simple since the friendship is not transitive and inherited. So the question is if there is a correct syntax or any workaround to make each individual class in Args be a friend of A? 回答1: Maybe the following CRTP variant would be sufficient for your use: template

Declaring main as friend considered harmful?

倖福魔咒の 提交于 2019-12-04 23:34:27
Discussion I know that main can be a friend of a class : #include <iostream> class foo { friend int main(); int i = 4; }; int main() { foo obj; std::cout << obj.i << std::endl; } LIVE DEMO However, I feel that although this is perfectably allowable it conceals many dangers. Questions Are there any valuable uses in making main a friend of a class? Are there any reasons that declaring main as friend of a class should be considered harmful? Ben Voigt The choice whether to use or avoid a legal feature becomes moot if the feature is not, in fact, legal. I believe there's serious doubt surrounding

Friend within private nested class

早过忘川 提交于 2019-12-04 21:15:02
I have two private nested classes that would need to access a private member in another class. I thought about putting the class that needs to access the private member as friend in the accessed class, however I'm getting an error that A::m_nData is private so I can't access it. Anyway of telling the compiler that I need to access the A::m_nData private member within D::DoSomething()? Here is a sample of what I'm trying to do: File A.h class A { class D; public: A(); ~A() {} private: friend class D; int m_nData; }; File A.cpp: #include "A.h" #include "B.h" A::A() : m_nData(0) { } File B.h:

Template friend function and return type deduction

偶尔善良 提交于 2019-12-04 19:05:56
问题 Note: This question is really close to Return type deduction for in-class friend functions, but I did not find the answer to my problem there. Tested with clang 3.4 with std=c++1y and clang 3.5 with std=c++14 and std=c++1z This code compiles: #include <iostream> template<class T> class MyClass { public: MyClass(T const& a) : impl(a) {} template<class T0, class T1> friend auto // requires operator+(T0,T1) exists operator+(MyClass<T0> const& a, MyClass<T1> const& b) { return MyClass<decltype(a