friend

How to declare two classes such that A has members of B and B marks members of A as friends?

别说谁变了你拦得住时间么 提交于 2019-12-01 03:28:49
问题 I am attempting to do exercise 7.32 from C++ Primer 5th Edition. That exercise asks the following: Define your own versions of Screen and Window_mgr in which clear is a member of Window_mgr and a friend of Screen . Here are the definitions for Screen , Window_mgr and clear given in the text. class Screen { public: using pos = std::string::size_type; Screen(pos ht, pos wd, char c) : height(ht), width(wd), contents(ht * wd, c) { } private: pos height = 0, width = 0; std::string contents; };

Friend access to protected nested class

老子叫甜甜 提交于 2019-12-01 03:24:46
I have the following C++ code: class A { protected: struct Nested { int x; }; }; class B: public A { friend class C; }; class C { void m1() { B::Nested n; // or A::Nested } }; Compiling this snippet with g++ 4.4, it does not make a difference whether I use B::Nested or A::Nested in m1. Clang accepts B::Nested , but does not compile if I A::Nested . Is that a bug in g++ or in clang? According to the Standard, GCC is correct and Clang is wrong. It says at 11.2/4 A member m is accessible when named in class N if m as a member of N is protected, and the reference occurs in a member or friend of

C++-like friend class mechanism in Java

江枫思渺然 提交于 2019-12-01 02:37:20
问题 Do you know how can I make an object changeable only inside a special class? In this example I want the object PrivateObject to be only changable (incrementable) inside the Box class, nowhere else. Is there a way to achieve this? public class Box { private PrivateObject prv; public void setPrivateObject(PrivateObject p){ prv = p; } public void changeValue(){ prv.increment(); } } public class PrivateObject { private value; public increment(){ value++; } } PrivateObject priv = new PrivateObject

VB.NET: what does the 'friend' modifier do?

六月ゝ 毕业季﹏ 提交于 2019-12-01 02:29:13
What does the 'friend' modifier do in VB.NET? Why is it the default modifier for GUI components in Visual Studio? friend in VB.Net is the same as internal in C#, it means that it can be accessed anywhere in the same assembly, but not from other assemblies. I think it's a sensible default since I would say that normally one assembly should not be using another assembly's GUI controls (unless it's a class library or similar that is built for the purpose). DannyLane Friend is available in VB: The Friend (Visual Basic) keyword in the declaration statement specifies that the elements are accessible

C++11 Declaring factory a friend of base class

岁酱吖の 提交于 2019-12-01 00:49:40
问题 I'm trying to create a factory for derived classes. I only want the factory to be able to create instances of the derived classes so I've made the base constructor protected ; the derived classes just use the base class constructors so their constructors are protected also. I tried to declare the factory as a friend of the base class so that it could access the protected constructor. When I compile using this command clang++ -std=c++11 -stdlib=libc++ Friends.cpp -o Friends I get this error:

VB.NET: what does the 'friend' modifier do?

痞子三分冷 提交于 2019-11-30 22:06:52
问题 What does the 'friend' modifier do in VB.NET? Why is it the default modifier for GUI components in Visual Studio? 回答1: friend in VB.Net is the same as internal in C#, it means that it can be accessed anywhere in the same assembly, but not from other assemblies. I think it's a sensible default since I would say that normally one assembly should not be using another assembly's GUI controls (unless it's a class library or similar that is built for the purpose). 回答2: Friend is available in VB:

Implementation of Friend concept in Java [duplicate]

吃可爱长大的小学妹 提交于 2019-11-30 12:18:26
问题 This question already has answers here : Is there a way to simulate the C++ 'friend' concept in Java? (19 answers) Closed 5 years ago . How does one implement the friend concept in Java (like C++)? 回答1: 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

Friend classes across different namespaces. Is that possible

有些话、适合烂在心里 提交于 2019-11-30 11:04:19
I'm having problems trying to use the friend feature of C++. I have these interfaces: #pragma once #include "Mesh3D.h" #include <string> namespace tools{ namespace sysInput{ class CGeometryManager3D { public: bool loadFromFile(render::CMesh3D& mesh, std::string filename); CGeometryManager3D(void); ~CGeometryManager3D(void); }; }; }; and #pragma once #include "GeometryManager.h" class CGeometryManager3D; namespace render{ class CMesh3D { public: friend class tools::sysInput::CGeometryManager3D; CMesh3D(void); ~CMesh3D(void); }; }; I don't know what is happening, but a lot of errors are thrown

What's the difference between friendship and inheritance?

北慕城南 提交于 2019-11-30 10:25:30
Suppose there are two classes A and B: class A {}; class B {}; In what aspects differ the two examples below? Example 1: class C : public A, public B {}; Example 2: class C { //private friend class A; friend class B; } A friend can touch the private parts (pun only slightly intentional! ;) ) of whatever it is friend of, but nothing of A and B are part of C - it just means that " A and B can touch C 's private bits"). Anything "less" than private is of course also available to A and B , so if C has protected or public members, that will also be available. When you inherit, the A and B becomes

How can I call a private destructor from a shared_ptr?

醉酒当歌 提交于 2019-11-30 09:30:01
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 by only returning only const resource* from the resource_manager , but somehow I am not satisfied with