friend-class

“Friend Classes” in javascript

佐手、 提交于 2019-12-07 09:15:00
问题 I have a Factory class that creates a Widget object. The Factory object needs to callback a "private method" of the Widget object at a later time to pass it some ajax info. So far, the only implementation I've come up with is to create a public method in the Widget that returns the private method to the factory, and then deletes itself, the Factory then returns the new Widget while retaining a pointer to the private method. Here is a simplified example: function Factory() { var widgetCallback

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

C++ Templates: Partial Template Specifications and Friend Classes

别来无恙 提交于 2019-12-05 03:34:51
is it possible to somehow make a partial template specification a friend class? I.e. consider you have the following template class template <class T> class X{ T t; }; Now you have partial specializations, for example, for pointers template <class T> class X<T*>{ T* t; }; What I want to accomplish is that every possible X<T*> is a friend class of X<S> for ANY S . I.e. X<A*> should be a friend of X<B> . Of course, I thought about a usual template friend declaration in X: template <class T> class X{ template <class S> friend class X<S*>; } However, this does not compile, g++ tells me this: test4

Friend class in TypeScript

谁说胖子不能爱 提交于 2019-12-04 00:41:49
In C++ there is something called a friend class. As far as I know, there's no such thing in TypeScript/JavaScript. Is there a way to simulate such behavior of friend class in TypeScript/JavaScript? To give better context (if needed) of why and what I try to do, I make some small game for fun (and to learn stuff) and try to do this . At the moment I just use public methods and everything works, but I would like to limit accessibility of those methods to just one other class. I use TypeScript, if that helps. TypeScript only provides protected and private access modifiers. It does not currently

Using a friend class vs. adding accessors for unit testing in C++?

二次信任 提交于 2019-12-03 05:59:13
Is it better to add functions that return the internal state of an object for unit testing, as opposed to making the testing class a friend? - especially, when there is no use for the functions except for the case of unit testing. Unit tests should 95% of the time only test the publicly exposed surface of a class. If you're testing something under the covers, that's testing implementation details, which is inherently fragile, because you should be able to easily change implementation and still have the tests work. Not only is it fragile, but you could also be tempted into testing things that

recursive friend classes

◇◆丶佛笑我妖孽 提交于 2019-12-01 17:07:44
Is there any way around this: class B; class C { public: C() { } private: int i; friend B::B(); }; class B { public: B() { } private: int i; friend C::C(); }; Gives error: prog.cpp:8: error: invalid use of incomplete type ‘struct B’ prog.cpp:1: error: forward declaration of ‘struct B’ You just can't do this. Remove the circular dependency. According to IBM's documentation (which I realize is not normative): A class Y must be defined before any member of Y can be declared a friend of another class. So I think the answer is "no". Of course, you can use friend class B; ...instead of friend B::B()

How come forward declaration is not needed for friend class concept?

本秂侑毒 提交于 2019-11-28 11:05:38
I've just recently learned about friend class concept in C++ (I've googled around for a bit, but this answer made me laugh until I remembered the most important parts), and I'm trying to incorporate it in to the project I'm working on right now. The concise question is singled out in the end, but in general, I'm confused by complete lack of forward declarations in my working code. All of my classes are separated through (sub-)folders and each one into a separate .h and .cpp file, but this should be enough to get a feeling about dependencies: // FE.h - no implementations - no .cpp file class FE

Why friend directive is missing in Java?

妖精的绣舞 提交于 2019-11-27 14:40:47
I was wondering why Java has been designed without the friend directive that is available in C++ to allow finer control over which methods and instance variables are available from outside the package in which a class has been defined. I don't see any practical reason nor any specific drawback, it seems just a design issue but something that wouldn't create any problem if added to the language. Here are a few reasons off the top of my head: friend is not required. It is convenient, but not required friend supports bad design. If one class requires friend access to another, you're doing it

'friend' functions and << operator overloading: What is the proper way to overload an operator for a class?

那年仲夏 提交于 2019-11-27 06:23:44
In a project I'm working on, I have a Score class, defined below in score.h . I am trying to overload it so, when a << operation is performed on it, _points + " " + _name is printed. Here's what I tried to do: ostream & Score::operator<< (ostream & os, Score right) { os << right.getPoints() << " " << right.scoreGetName(); return os; } Here are the errors returned: score.h(30) : error C2804: binary 'operator <<' has too many parameters (This error appears 4 times, actually) I managed to get it working by declaring the overload as a friend function: friend ostream & operator<< (ostream & os,

How come forward declaration is not needed for friend class concept?

孤街醉人 提交于 2019-11-27 05:55:48
问题 I've just recently learned about friend class concept in C++ (I've googled around for a bit, but this answer made me laugh until I remembered the most important parts), and I'm trying to incorporate it in to the project I'm working on right now. The concise question is singled out in the end, but in general, I'm confused by complete lack of forward declarations in my working code. All of my classes are separated through (sub-)folders and each one into a separate .h and .cpp file, but this