草庐IT

lldb_private

全部标签

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++ - 具有私有(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++ - 是否可以在 C++ 中声明 operator= private 并同时由编译器合成

我对运算符=很满意,它由编译器自动合成。但我希望它是私有(private)的,并且不想用类型的页面长定义来膨胀我的代码Foo&Foo::operator=(constFoo&foo){if(this==&foo)return*this;member1_=foo.member1_;member2_=foo.member2_;member3_=foo.member2_;...member1000_=foo.member1000_;return*this;}请问有什么办法吗? 最佳答案 在C++11中是:classFoo{Foo&oper

c++ - "member of type foo has private copy constructor"错误 : why's it an error?

我试图定义这样一个类:#includeclassmy_class{private:someone_elsesfoo;public:myclass();~myclass();//...};但是编译器失败了:“someone_elses类型的字段foo有一个私有(private)的复制构造函数”。现在我知道我可以通过以下方式解决这个问题:classmy_class{private:someone_elses*foo;//...};my_class::my_class(){foo=newsomeone_elses();}my_class::~my_class(){deletefoo;}我的问

c++ - 私有(private)内部类的友元函数

我有以下问题。我有一个带有私有(private)内部类的类。我现在想为内部类实现一个friend交换功能。但是我不知道如何制作非内联交换功能。如果我在内部类中定义它,一切正常。如果有人可以告诉我如何使其成为非内联的,我将不胜感激:)一些代码确实说明了问题:classOuter{classInner{intdata;friendswap(Inner&lhs,Inner&rhs)//whatisthesyntaxto{//makethisfunctionnoninline?usingstd::swap;swap(lhs.data,rhs.data);}}} 最佳

c++ - 返回私有(private) vector

我有一个具有私有(private)属性的类,它是一个vector。执行getter函数的最佳方法是什么?返回整个vector:vectorgetNames(){returnnames;}这会返回一个拷贝还是一个指针?因为它是私有(private)的,所以可以访问吗?返回迭代器:vector::iteratorgetNames(){returnnames.begin();}公开载体我知道这对OOP来说是不好的做法,只是列出选项。 最佳答案 返回constvector&.它确保它不会在外部被修改并且不会制作拷贝。

c++ - C++继承中的protected和private成员变量

我是C++的新手,我有一个关于继承中的c++protected和private成员的问题。如果一个类是public继承了一个基类,protected和private成员变量是否会成为派生类的一部分?例如:classBase{protected:inta;intb;private:intc;intd;public;intq;};classDerived:publicBase{};类Derived是否也有a,b,c,d,q的所有成员?我们可以在Derived类中将inta定义为public、protected和private吗? 最佳答案