friend-class

Friend Class In C++

这一生的挚爱 提交于 2019-12-31 01:45:12
问题 here im not understanding the concept very well or i am right.... So lets take this "friend" class example here: class MyClass{ friend class AnotherClass; private: int secret; } class AnotherClass{ public: void getSecret(MyClass mc){ return mc.secret; } } So Yes... in the above code it will actually work if you do it... but in general, why cant you use getters and setters all the time instead of friend class? Is the reason of friend class usage because of "tediousness"? 回答1: friend is for

Friend class in TypeScript

霸气de小男生 提交于 2019-12-21 07:27:25
问题 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,

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

瘦欲@ 提交于 2019-12-20 19:26:04
问题 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. 回答1: 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

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

删除回忆录丶 提交于 2019-12-20 19:24:06
问题 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. 回答1: 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

recursive friend classes

邮差的信 提交于 2019-12-19 16:57:27
问题 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’ 回答1: You just can't do this. Remove the circular dependency. 回答2: 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

recursive friend classes

夙愿已清 提交于 2019-12-19 16:57:07
问题 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’ 回答1: You just can't do this. Remove the circular dependency. 回答2: 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

Why friend directive is missing in Java?

前提是你 提交于 2019-12-17 12:15:51
问题 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. 回答1: Here are a few reasons off the top of my head: friend is not required. It is convenient, but

Using friend function in C++

半城伤御伤魂 提交于 2019-12-11 03:46:42
问题 Just read about friend functions and I'm trying to access private variable "number" in class A with friend function "Print" from class B. I'm working with Visual Studio. Compilation of my code gives me plenty of various errors like: C2011: 'A' : 'class' type redefinition C2653: 'B' : is not a class or namespace name Please be patient with me me and show a proper way of achieving my goal. Here are my files A.h: class A { public: A(int a); friend void B::Print(A &obj); private: int number; }; A

friend class with forward class declaration does not compile

一世执手 提交于 2019-12-11 03:09:27
问题 This a basic program to understand how to use friend class in C++. Class xxx has a class yyy object using friend . Since class yyy is defined after class xxx I have declared class yyy using forward declaration. #include<iostream> using std::cout; using std::endl; class yyy; //Forward Declaration of class yyy class xxx{ private: int a; public: xxx(){a=20;yyy y2;y2.show();} //Error// void show(){cout<<"a="<<a<<endl;} friend class yyy; //Making class yyy as freind of class xxx }; class yyy{

C++ Templates: Partial Template Specifications and Friend Classes

不想你离开。 提交于 2019-12-10 03:00:42
问题 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>