friend

友元

让人想犯罪 __ 提交于 2019-12-20 02:42:02
友元 c++控制对类私有部分的访问。 友元有3种。 友元函数 例子 A = 2.34 * B; 非成员函数不是由对象调用,它使用所有值都是显式参数。 Time operator*(double m, const Time & t); 但有时候非成员函数不可以调用成员数据。然而有一种特殊的非成员函数可以访问类的私有成员,他们被称为友元函数。 友元类 友元成员函数 通过让函数成为类的友元,可以赋予该函数与类的成员函数相同的访问权限。 创建友元函数 创建友元函数的第一步是将其原型放在类声明中,并在原型声明前加上关键字friend。 friend Time operator*(double m, const Time& t); 上述原型意味着一下两点。 operator*()函数是在类声明中声明的,但他不是成员函数,因此不能使用成员运算符来调用。 operator*()不是成员函数,但它与成员函数访问权限相同。 第二步是编写函数定义。 因为它不是成员函数,所以不要使用Time::限定符。另外不要再定义函数头使用friend。 当函数定义也是原型时,需要加friend关键字。 常用的友元 重载<<运算符 第一个重载版本 因为第一个操作数是ostream类对象cout,所以需要使用友元函数。 而且没有使用到ostream中的私有部分,所以只需要作为Time类的友元函数。 在类中声明friend

In-class friend operator doesn't seem to participate in overload resolution

ⅰ亾dé卋堺 提交于 2019-12-19 19:57:23
问题 While writing a CRTP template that enables classes to provide overloads for operator+ based on template arguments, I found that an in-class friend operator doesn't seem to participate in overload resolution if none of it's arguments is of the type of the class it was defined in. Boiled down: enum class FooValueT{ zero, one, two }; class Foo{ FooValueT val_; public: Foo(FooValueT x) : val_(x){}; Foo& operator+=(Foo other){ val_ = (FooValueT)((int)val_ + (int)other.val_); return *this; } /

C++ concept with friend-like access

廉价感情. 提交于 2019-12-19 17:13:10
问题 Is it possible to make this code work as I'd like? I.e. to allow the concept to have access to a private member funcion? template <typename T> concept bool Writeable() { return requires (T x,std::ostream os) { { x.Write(os) } -> void }; } template <Writeable T> void Write(std::ostream &os,const T &x) { x.Write(os); } class TT { private: void Write(std::ostream &os) const { os << "foo"; } //friend concept bool Writeable<TT>(); friend void ::Write<TT>(std::ostream &,const TT &); }; Thanks 回答1:

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

Making an undefined class as friend, and defining it later

久未见 提交于 2019-12-19 06:42:39
问题 Making an unknown friend template<typename T> class List { protected: class a { int x; int y; private: friend class b; // <------------ Why this is not an error? }; template <typename U > class b { //If that is not a error this should be an error int z; U y; }; public: List() { a* ptr = (a *)new unsigned char[sizeof(a)]; } }; int main() { List<int> mylist; } Please go through this link, I have my questions as comments in the code. I am trying to make another class a friend of my class. But

How do I make main a friend of my class from within a library?

﹥>﹥吖頭↗ 提交于 2019-12-19 05:58:15
问题 Please see my first attempt at answering this . I neglected to tell the whole story before in an attempt to simplify things. Turns out my example works! Sorry. The whole story is that this is a library the contains a class in one file and the main in another file, all linked into my library. The library is providing the basis for a Process Framework, which is why the main is in the library and not the process. Below is a stripped down version of what I have. pf.hpp using namespace std;

cannot convert '*void(MyClass::*)(void*) to void*(*)(void*) in pthread_create function

时光毁灭记忆、已成空白 提交于 2019-12-19 03:24:07
问题 i'm trying to create a new thread with a class "CameraManager" but i have the following error: cannot convert '*void(CameraManager:: * )(void*) to void*( * )(void*) in pthread_create function i defined in the cameramanager.h file: public: void *dequeueLoop(void *ptr); and in the cameramanager.cpp void CameraManager::startDequeuing(){ dequeuing = true; dequeueThreadId = pthread_create(&dequeueThread, NULL, &CameraManager::dequeueLoop, NULL); } void *CameraManager::dequeueLoop(void *ptr){ while

“Friend”ing classes in python

元气小坏坏 提交于 2019-12-18 19:09:24
问题 Is there any way to make certain variables in classes "private" (or whatever self.__var really is) but be accessible to another class, like friends in c++, except in python? I do not want the variables in either class being messed with. Nor do I want to copy the entire code over and convert it for the second class. 回答1: No, there is not such an option. Use names that start with single underscores and tell the other people working on your project to not be silly about what they access. 回答2:

Friend classes across different namespaces. Is that possible

旧巷老猫 提交于 2019-12-18 14:08:13
问题 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