草庐IT

safe-publication

全部标签

c++ - 这一堆 typedef 应该是私有(private)的还是公共(public)的?

我正在编写一个表示图形的类,所以我编写了以下标题classGraph{public:Graph();Graph(intN);voidaddVertex();voidaddEdge(VertexNumv1,VertexNumv2,Weightw);std::pairshortestPath(constVerticesGroup&V1,constVerticesGroup&V2);private:typedefintVertexNum;typedefintWeight;typedefstd::pairEdge;typedefstd::vectorPath;typedefsize_tPath

c++ - 无法从派生指针访问公共(public)基成员

为什么这段代码不能编译?(海合会4.7.0)//Classwithasimplegetter/setterpair.classBase{public:Base():m_Value(0){}virtual~Base(){}//Gettervirtualintvalue(){returnm_Value;}//Settervirtualvoidvalue(intVal){m_Value=Val;}private:intm_Value;};//Derivedclassoverridesthesetter.classDerived:publicBase{public:voidvalue(intV

c++ - 从派生类访问基类公共(public)成员

是否可以从程序中其他一些位置的派生类实例访问基类公共(public)成员。classbase{public:intx;base(intxx){x=xx;}};classderived:base{public:derived(intxx):base(xx){}};classmain{public:derived*myDerived;voidm1(){myDerived=newderived(5);m2(myDerived);}voidm2(derived*myDerived){printf("%i",myDerived->x);}};在上面的代码之后,我得到了以下错误。`error:'i

c++ - 公共(public)新私有(private)构造函数

当我尝试编译以下内容时:#includeclassTest{public:void*operatornew(size_tnum);voidoperatordelete(void*test);~Test();private:Test();};Test::Test(){std::cout(test));}intmain(){Test*test=newTest;deletetest;}我明白了:$g++-otesttest.cpptest.cpp:Infunction‘intmain()’:test.cpp:14:error:‘Test::Test()’isprivatetest.cpp:3

c++ - 为什么在使用 GCC 7、libstdc++ 和 -fgnu-tm 编译时,std::is_function 无法识别 transaction_safe 函数?

下面的编译失败是由于libstdc++缺陷造成的,还是此行为符合事务内存TS(n4514)?#includestatic_assert(std::is_function_v,"");intmain(){} 最佳答案 它应该可以工作,请提交一份GCC错误报告。 关于c++-为什么在使用GCC7、libstdc++和-fgnu-tm编译时,std::is_function无法识别transaction_safe函数?,我们在StackOverflow上找到一个类似的问题:

c++ - 在 public 中使用 "using"继承 private 构造函数并没有使其 public

“使用”私有(private)成员变量使其成为公共(public)成员,但构造函数保持私有(private)。示例:classMyClass;classBase{private:Base(floatv):m_v{v}{}floatm_v;friendMyClass;};classMyClass:publicBase{public:usingSuper=Base;usingSuper::Super;//thisisstillprivateusingSuper::m_v;//thisispublic};intmain(){MyClassx{3.4f};//error-callingapri

c++ - 防止同一个类的实例调用公共(public)函数(C++)

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Whydoobjectsofthesameclasshaveaccesstoeachother’sprivatedata?我在尝试保持封装时从未理解的东西:假设我有一个名为GameObject的类和一个名为Human的派生类。GameObject有一个私有(private)变量position。我有多个Human实例,我希望每个人都能够调用SetPos()并根据需要设置其位置。然而,我不希望一个人有能力确立另一个人的地位。这是我的问题。如果我有SetPospublic或protected,每个人都可以改变彼此

python - 如何创建一个可以接收 c++ 结构/实例或 python 对象作为参数的公共(public) cython 函数?

我的矩形.hnamespaceshapes{classRectangle{public:intx0,y0,x1,y1;Rectangle();Rectangle(intx0,inty0,intx1,inty1);~Rectangle();intgetArea();};}我的矩形.cpp#include"Rectangle.h"namespaceshapes{Rectangle::Rectangle(){}Rectangle::Rectangle(intX0,intY0,intX1,intY1){x0=X0;y0=Y0;x1=X1;y1=Y1;}Rectangle::~Rectangle

c++ - 将私有(private)函数覆盖到公共(public)函数的潜在危险是什么?

我刚刚发现在C++中允许将私有(private)函数从基对象覆盖为公共(public)函数,因为VisualStudio会产生0警告。这样做有什么潜在的危险吗?如果没有,在基础对象中声明一个私有(private)的、protected和公共(public)的虚函数有什么区别? 最佳答案 what'sthedifferencebetweendeclaringavirtualfunctioninprivate,protectedandpublicinabaseobject?不同之处在于,private虚函数只能从基类中调用。如果该函数不

c++ - 在 boost::python 的类中公开公共(public)结构

我想在带有boost::python的python代码上使用这个C++类/*creature.h*/classHuman{private:public:structemotion{/*Allemotionsarepercentages*/charjoy;chartrust;charfear;charsurprise;charsadness;chardisgust;charanger;charanticipation;charlove;};};问题是如何在boost-python中公开这个公共(public)属性namespacepy=boost::python;BOOST_PYTHON