草庐IT

Non-Public

全部标签

c++ - 纯虚类中的构造函数应该是 "protected"还是 "public"?

以下示例来自《InsideC++objectmodel》一书classAbstract_base{public:virtual~Abstract_base()=0;virtualvoidinterface()const=0;virtualconstchar*mumble()const{return_mumble;}protected:char*_mumble;};作者说如果我想初始化纯虚基类的数据成员_mumble,应该实现一个“protected构造函数”。但为什么要保护?为什么“publicconstructor”不适合这个类?感谢您的回答,如果有例子就完美了。

c++ - 我可以在类定义中放置 "non-static blocks"代码吗?

C++中有非静态block吗?如果不是,如何优雅地模拟?我想替换像这样的东西:-classC{public:voidini(){/*somecode*/}};classD{std::vectorregis;//willini();laterpublic:Cfield1;public:Cfield2;public:Cfield3;//wheneverIaddanewfield,Ihaveto...#1public:D(){regis.push_back(&field1);regis.push_back(&field2);regis.push_back(&field3);//#1...al

c++ - constexpr 类的设计 : merging constexpr and non-constexpr versions?

考虑一个在运行时只包装一个值的类:templateclassNonConstValue{public:NonConstValue(constType&val):_value(val){;}Typeget()const{return_value;}voidset(constType&val)const{_value=val;}protected:Type_value;};以及它的constexpr版本:templateclassConstValue{public:constexprConstValue(constType&val):_value(val){;}constexprTypeg

c++ - 堆内存是每个进程的吗? (或)不同进程共享的公共(public)内存位置?

每个进程都可以使用堆内存来存储和共享进程内的数据。我们在编程中有一个规则,每当我们在堆内存中占用一些空间时,我们需要在作业完成后释放它,否则会导致内存泄漏。int*pIntPtr=newint;...deletepIntPtr;我的问题:堆内存是每个进程的吗?如果是,thenmemoryleakispossibleonlywhenaprocessisinrunningstate.如果不是,thenitmeansOSisabletoretaindatainamemorysomewhere.Ifso,isthereawaytoaccessthismemorybyanotherprocess

c++ - 当我从公共(public)成员函数返回引用时,为什么我可以暴露私有(private)成员?

在代码片段中,我能够访问类范围之外的私有(private)成员变量。虽然永远不应该这样做,但为什么在这种情况下允许这样做呢?通过引用接收返回的私有(private)变量是一种不好的做法吗?#include#includeclassfoo{intx;public:foo(inta):x(a){}intmethodOne(){returnx;}int&methodTwo(){returnx;}};intmain(){fooobj(10);int&x=obj.methodTwo();x=20;//Withthisstatement,modifyingthestateofobj::xstd::

c++ - xvalues : differences between non class types and class types

考虑下面的最小示例:#includestructS{};intmain(){Ss;std::move(s)=S{};}它编译没有错误。如果我改为使用非类类型,则会收到错误。例如,以下代码无法编译:#includeintmain(){inti;std::move(i)=42;}枚举、作用域枚举等也是如此。错误(来自GCC)是:usingxvalue(rvaluereference)aslvalue这背后的原理是什么?我想这是对的,但我想了解我可以对除非类之外的所有类型执行此操作的原因是什么。 最佳答案 C++允许对类对象右值进行赋值,

c++ - G++ 编译器错误或错误代码? : "template definition of non-template"

作为大型程序的特征类的一部分,我尝试创建一个静态类变量,该变量可能具有不同的值,具体取决于实例化封闭类模板的类型。我已经简化了相关代码以生成我正在谈论的内容的简单示例:#include#include#includetemplatestructFoo;templatestructFoo::value>::type>{staticstd::stringmessage;};templatestructFoo::value>::type>{staticstd::stringmessage;};templatestd::stringFoo::message;对于GCC4.6,这会产生一个编译器

c++ - 我相信这是 clang++ 中与访问类的公共(public)成员函数相关的错误

以下doesn'tcompile在clang中:#includevoidf(){std::cout产量:main.cpp:13:16:error:unknowntypename'p';didyoumean'S::p'?s.operatorp()();^S::pmain.cpp:6:19:note:'S::p'declaredheretypedefvoid(*p)();^但它应该,因为表达式s.operatorp()()访问对象S::s的公共(public)成员函数。我错过了什么吗?如果我错了,我将不胜感激标准中的引述来支持答案。 最佳答案

c++ - 静态 C++ 映射初始化错误 C2552 : non-aggregates cannot be initialized with initializer list

我正在尝试使用以下代码在header中初始化map,但它一直在标题中显示错误。我正在使用C++11,所以这应该是可能的,对吧?typedefstd::map>AnimationSpeedMap;AnimationSpeedMapAnimationSpeeds={{NPCAnimation::WALK,{{Direction::LEFT,sf::milliseconds(100)},{Direction::RIGHT,sf::milliseconds(100)},{Direction::UP,sf::milliseconds(200)},{Direction::DOWN,sf::mill

c++ - 公共(public)变量和私有(private)变量的命名约定?

把m_varname当public用同一个类用variable当private是不是错了 最佳答案 一些问题:为什么要有公共(public)变量?以_和__开头的标识符是为系统库保留的。在实践中,这通常并不重要,但很高兴知道。话虽如此,创建命名约定并没有错,无论它看起来如何。只要保持一致即可。 关于c++-公共(public)变量和私有(private)变量的命名约定?,我们在StackOverflow上找到一个类似的问题: https://stackover