friend

Friend function template with automatic return type deduction cannot access a private member

。_饼干妹妹 提交于 2020-01-03 07:19:15
问题 Sorry for how complicated the title of this question is; I tried to describe the minimal SSCCE I constructed for this problem. I have the following code: #include <iostream> namespace fizz { template<typename... Ts> class bar { public: template<int I, typename... Us> friend auto foo(const bar<Us...> &); private: int i = 123; }; template<int I, typename... Ts> auto foo(const bar<Ts...> & b) { return b.i; } } int main() { std::cout << fizz::foo<1>(fizz::bar<int, float>{}); } This code compiles

Are derived classes considered friends?

时光总嘲笑我的痴心妄想 提交于 2020-01-02 05:06:29
问题 If I create base class A and A is a friend of class B, can a class derived from A access B to its liking, or else what is it allowed? Thanks 回答1: struct A{}; struct Ader : A{}; struct B{ friend struct A; }; No. Friendship is not inherited in C++. It is also not transitive. Ader can not access B as a friend unless explicitly given friendship by B, just because it's base A is a friend of B . 回答2: No, that's not allowed. Check here. 来源: https://stackoverflow.com/questions/4083838/are-derived

Are derived classes considered friends?

て烟熏妆下的殇ゞ 提交于 2020-01-02 05:06:08
问题 If I create base class A and A is a friend of class B, can a class derived from A access B to its liking, or else what is it allowed? Thanks 回答1: struct A{}; struct Ader : A{}; struct B{ friend struct A; }; No. Friendship is not inherited in C++. It is also not transitive. Ader can not access B as a friend unless explicitly given friendship by B, just because it's base A is a friend of B . 回答2: No, that's not allowed. Check here. 来源: https://stackoverflow.com/questions/4083838/are-derived

Partial specialization friend declaration

非 Y 不嫁゛ 提交于 2020-01-02 04:11:48
问题 In the following code: template <typename U, typename V> class A {}; template <typename U, typename V> class B {}; template <typename T> class C { template <typename U, typename V> friend class A; // Works fine. // template <typename U> friend class B<U,T>; // Won't compile. }; I want B<U,T> to be friend to C<T> , that is, the second parameter of B must match C's parameter, though its first parameter can be anything. How do I achieve this? The friend declaration of A<U,V> is too much, though

PHP friend/package visibility

谁说我不能喝 提交于 2020-01-02 01:30:12
问题 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

Friend within private nested class

为君一笑 提交于 2020-01-01 22:47:55
问题 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

Friend within private nested class

别来无恙 提交于 2020-01-01 22:46:09
问题 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

Compiler error in declaring template friend class within a template class

时光怂恿深爱的人放手 提交于 2020-01-01 08:24:11
问题 I have been trying to implement my own linked list class for didactic purposes. I specified the "List" class as friend inside the Iterator declaration, but it doesn't seem to compile. These are the interfaces of the 3 classes I've used: Node.h: #define null (Node<T> *) 0 template <class T> class Node { public: T content; Node<T>* next; Node<T>* prev; Node (const T& _content) : content(_content), next(null), prev(null) {} }; Iterator.h: #include "Node.h" template <class T> class Iterator {

Can friend class be declared conditionally in C++03?

 ̄綄美尐妖づ 提交于 2019-12-31 01:47:07
问题 I want to declare a friend class only if some (compile-time) condition is true. For example: // pseudo-C++ class Foo { if(some_compile_time_condition) { friend class Bar; } }; I did not find any solution on the internet. I went through all the answers to the question Generating Structures dynamically at compile time. Many of them use the C++11 std::conditional , but I would like to know if it is possible to do this in C++03 without using the preprocessor . This solution https://stackoverflow

Template parameter as a friend

橙三吉。 提交于 2019-12-30 06:18:11
问题 In C++03 the following is illegal, although some compilers support it. template <class T> class X { friend T; }; Has this been legalized in C++11? (Sorry, didn't have time to read the draft myself, just hoping someone knows this) 回答1: From section §11.3, 3 in N3291: template <typename T> class R { friend T; }; R<C> rc; // class C is a friend of R<C> R<int> Ri; // OK: "friend int;" is ignored So it is legal in C++11. 回答2: Yes c++0x allows template parameter to be friends. Well, I happened to