草庐IT

publication

全部标签

c++ - 结构绑定(bind) : binding to public data members (inherited base class )

即使基类和派生类都有公共(public)数据成员#includeclassM{public:intx=2;volatiledoubley=3;};classS:publicM{public:intx1=4;volatiledoubley1=5;};intmain(){Sf();Sa;std::cout获取错误auto[b,c,d,e]=f();main.cpp:Infunction'intmain()':main.cpp:21:10:error:cannotdecomposeclasstype'S':bothitanditsbaseclass'M'havenon-staticdatam

C++ 保护/公共(public)重载

我有这样一个类:classFoo{public:Foo(){for(inti=0;i&V()const{returnv;};protected:vector&V(){returnv;};private:vectorv;};然后是一段这样的代码:Foofoo;for(inti=0;i但是,后者引发编译错误,指出V()调用是protected方法,而我只是试图从中读取,而不是修改它。我已经尝试了以下(但没有成功)。Foofoo;constvector&test=foo.V();for(inti=0;i非常感谢您的帮助。=====谢谢大家的解释和解决方案!非常感谢!

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++ - 在 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虚函数只能从基类中调用。如果该函数不