friend

Friend lookup exception from template-id?

吃可爱长大的小学妹 提交于 2019-11-30 09:00:53
问题 Consider the following clause in [namespace.memdef]/3: If the name in a friend declaration is neither qualified nor a template-id and the declaration is a function or an elaborated-type-specifier , the lookup to determine whether the entity has been previously declared shall not consider any scopes outside the innermost enclosing namespace. Is there a reason for the exception for template-id along with the qualified name? For that matter, is there a reason for the lookup of an unqualified

Using “friend”-declarations for unit testing. Bad idea?

拜拜、爱过 提交于 2019-11-30 06:36:45
[ Of course, the question is not restricted to a specific "friend" implementation, feel free though to point out implementation specifics if relevant ] Reading through the unanswered questions, I stumbled upon the InternalsVisibleTo attribute: Specifies that types that are ordinarily visible only within the current assembly are visible to another assembly. The C# Programming Guide on MSDN has a section Friend Assemblies describing how to use the attribute to allow the use of internal methods and types to another assembly. I'm wondering whether it would be a Good Idea to use this to create a

What is the fully qualified name of a friend function defined inside of a class?

巧了我就是萌 提交于 2019-11-30 06:33:40
问题 What is the fully qualified name of a friend function defined inside of a class? I recently saw an example analogous to the following. What is the fully qualified name of val() below? #include <iostream> namespace foo { class A { int x; public: A(int x = 0) : x(x) { } friend int val(const A &a) { return a.x; } }; } int main() { foo::A a(42); // val() found using ADL: std::cout << val(a) << std::endl; // foo::val(a); // error: 'val' is not a member of 'foo' // foo::A::val(a); // error: 'val'

Implementation of Friend concept in Java [duplicate]

时间秒杀一切 提交于 2019-11-30 02:26:09
This question already has an answer here: Is there a way to simulate the C++ 'friend' concept in Java? 19 answers How does one implement the friend concept in Java (like C++)? Alastor Moody Java does not have the friend keyword from C++. There is, however, a way to emulate that; a way that actually gives a lot more precise control. Suppose that you have classes A and B. B needs access to some private method or field in A. public class A { private int privateInt = 31415; public class SomePrivateMethods { public int getSomethingPrivate() { return privateInt; } private SomePrivateMethods() { } //

How do I define friends in global namespace within another C++ namespace?

回眸只為那壹抹淺笑 提交于 2019-11-30 01:30:47
问题 I'd like to define a binary operator on in the global namespace. The operator works on a class that is defined in another namespace and the operator should get access to the private members of that class. The problem I have is that I don't know how to scope that global operator when making it a friend in the class definition. I tried something like: namespace NAME { class A { public: friend A ::operator * (double lhs, const A& rhs); private: int private_var; }; } A operator * (double lhs,

Allowing a “friend” class to access only some private members

眉间皱痕 提交于 2019-11-29 22:55:43
Suppose I have three C++ classes FooA, FooB and FooC. FooA has an member function named Hello , I want to call this function in class FooB, but I don't want class FooC be able to call it. The best way I can figure out to realize this is to declare FooB as a friend class of FooA. But as long as I do this, all FooA's private and protected members will be exposed which is quite unacceptable to me. So, I wanna know if there is any mechanism in C++(03 or 11) better than friend class which can solve this dilemma. And I assume it will be nice if the following syntax is possible: class FooA { private

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

跟風遠走 提交于 2019-11-29 14:42:23
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 needs access to the internals of a class. It makes writing function templates easier, because you don't

How can I call a private destructor from a shared_ptr?

拜拜、爱过 提交于 2019-11-29 14:20:53
问题 I have a resource_manager class which maintains a std::vector<boost::shared_ptr<resource> > internally. resource_manager is a friend class of resource . I want resource s to only be created/deleted by resource_manager , so I made its constructors private (which works ok). However, if I make the destructor private, the code doesn't compile because the destructor is called by boost::shared_ptr , which is not a friend of resource . I am thinking of enforcing the "do not delete by clients" rule

Friend lookup exception from template-id?

旧巷老猫 提交于 2019-11-29 09:20:26
Consider the following clause in [namespace.memdef]/3: If the name in a friend declaration is neither qualified nor a template-id and the declaration is a function or an elaborated-type-specifier , the lookup to determine whether the entity has been previously declared shall not consider any scopes outside the innermost enclosing namespace. Is there a reason for the exception for template-id along with the qualified name? For that matter, is there a reason for the lookup of an unqualified name that isn't a template-id to be restricted to the innermost enclosing namespace? Is there a specific

Using “friend”-declarations for unit testing. Bad idea?

不打扰是莪最后的温柔 提交于 2019-11-29 06:16:47
问题 [ Of course, the question is not restricted to a specific "friend" implementation, feel free though to point out implementation specifics if relevant ] Reading through the unanswered questions, I stumbled upon the InternalsVisibleTo attribute: Specifies that types that are ordinarily visible only within the current assembly are visible to another assembly. The C# Programming Guide on MSDN has a section Friend Assemblies describing how to use the attribute to allow the use of internal methods