草庐IT

private-constructor

全部标签

c++ - 启动一个作为该类私有(private)方法的守护线程?

我是C++11线程的新手,我正在尝试执行以下操作:classSomething{public:voidstart(){this->task_=std::thread(&Something::someTask,this);this->isRunning_=true;this->task_.detach();//Ireaddetachwillstopitfromhanging}voidstop(){this->isRunning=false;}~Something(){this->stop();}private:std::atomicisRunning_;std::threadtask_;

c++ - 为什么在 Singleton 类中使用私有(private)析构函数?

我已经看到构造函数、复制构造函数、析构函数和赋值运算符保存在典型的单例类中的私有(private)范围内。例如classCMySingleton{public:staticCMySingleton&Instance(){staticCMySingletonsingleton;returnsingleton;}private:CMySingleton(){}//Privateconstructor~CMySingleton(){}CMySingleton(constCMySingleton&);//Preventcopy-constructionCMySingleton&operator

c++ - 复制初始化: why move or copy constructor was not called even if copy-elision is turned off?

我的问题不同,因为我可能“知道”复制省略。我正在学习复制初始化。但是,以下代码让我感到困惑,因为我已经使用-fno-elide-contructors-O0选项关闭了复制省略。#includeusingnamespacestd;classtest{public:test(inta_,intb_):a{a_},b{b_}{}test(consttest&other){cout我首先使用命令构建:g++-std=c++11-fno-elide-constructors-O0main.cpp-omain得到如下结果:**showelideconstructors**moveconstruct

c++ - 使用模板公开私有(private) Typedef

我有一个包含私有(private)typedef和几个成员的类功能:classFoo{private:typedefstd::blahblahFooPart;FooPartm_fooPart;...public:intsomeFn1();intsomeFn2();};几个成员函数需要以类似的方式使用m_fooPart,所以我想把它放在一个函数中。我将辅助函数放在匿名中命名空间,但在这种情况下,他们需要知道什么FooPart是。所以,我这样做了:namespace{templateinthelperFn(constT&foopart,intindex){...returnfoopart.

c++ - 使用私有(private)复制/移动构造函数进行聚合初始化

我在为anotherquestion测试一些东西时遇到了这个问题关于初始化聚合。我正在使用GCC4.6。当我用列表初始化聚合时,所有成员都在适当的位置构建,无需复制或移动。即:intmain(){std::array,2>a{std::array{Goo{1,2},Goo{3,4}},std::array{Goo{-1,-2},Goo{-3,-4}}};}让我们通过一些嘈杂的构造函数来确认:structGoo{Goo(int,int){}Goo(Goo&&){std::cout运行时,不会打印任何消息。但是,如果我将移动构造函数设为私有(private),编译器会提示'Goo::Goo

C++ "No appropriate default constructor available"

我想在不使用STL的情况下创建一个数组链表。但是,我在将数组传递到我的链接列表时遇到困难...编译时出现上面列出的错误。我需要如何将数组传递给链表?谢谢!(有问题的代码有**标记,如果测试请去掉)单链表.h#pragmaonce#ifndefSinglyLinkedList_h#defineSinglyLinkedList_h#includetemplatestructnode{Typevalue;node*next;};templateclassSinglyLinkedList{private:node*head;public:SinglyLinkedList();~SinglyLi

c++ - 具有私有(private)构造函数的类私有(private)继承的工作机制

案例一:classObjectCount{private:ObjectCount(){}};classEmployee:privateObjectCount{};案例二:classObjectCount{public:ObjectCount(){}};classEmployee:privateObjectCount{};案例1:ObjectCount构造函数是私有(private)的,继承是私有(private)的。它给出了编译器错误情况2:ObjectCount构造函数是公共(public)的,继承是私有(private)的。这段代码没问题。谁能解释一下这是怎么回事?

c++ - 我必须将数据设为私有(private)吗?

我知道类中的数据应该是私有(private)的,然后使用getter和setter来读取/修改它们。但是比起直接使用student.scores.push_back(100)省了一个成员函数是不是很麻烦。classStudent{public:voidaddToScores(intinScore){scores.push_back(inScore);}private:vectorscores;}简而言之,我很好奇人们实际上在做什么,总是使用getter和setter严格私有(private)数据? 最佳答案 成员函数的目的是公开接口

c++继承私有(private)复制构造函数: how doesn't this yield a compile time error?

在C++中,如果我们有这个类classUncopyable{public:Uncopyable(){}~Uncopyable(){}private:Uncopyable(constUncopyable&);Uncopyable&operator=(constUncopyable&);};然后我们有一个派生类classDervied:privateUncopyable{};我的问题是:当编译器在派生类中生成默认的复制构造函数和赋值运算符时,为什么这不会生成编译时错误?生成的代码不会尝试访问基类私有(private)成员吗? 最佳答案

c++ - 类 std::array of objects without default constructors

所以让我们假设我有以下类(class)classNoDefaultConstructor{NoDefaultConstructor()=delete;...};我还有另一个类,它有一个类型为NoDefaultConstructor和其他成员的数组classWrapper{std::arrayarr;...};如何在Wrapper的构造函数中初始化数组(可能在使用std::intializer_list的初始化列表中)?更具体地说,是我可以将参数传递给Wrapper的初始化列表中的数组构造函数以具有类似于以下的构造的唯一方法吗?我正在考虑这样做,因为将来数组的大小可能会发生变化。temp