草庐IT

private_extern

全部标签

c++ - 公共(public)常量和私有(private)可写属性的名称?

在C++编程中,我经常希望给类的用户对属性的只读访问权限,以及类本身的读写访问权限。我讨厌XxxGet()方法,所以我经常使用一个publicconst&到一个私有(private)属性,像这样:classcounter{private:int_count;public:constint&count;counter:_count(0),count(_count){}voidinc(void){_counter++;}};这个技巧有通用名称吗? 最佳答案 我对那个把戏的个人名字是坏主意。我会避免您采用的方法,因为它会产生额外的不必要的

c++ - 公共(public)常量和私有(private)可写属性的名称?

在C++编程中,我经常希望给类的用户对属性的只读访问权限,以及类本身的读写访问权限。我讨厌XxxGet()方法,所以我经常使用一个publicconst&到一个私有(private)属性,像这样:classcounter{private:int_count;public:constint&count;counter:_count(0),count(_count){}voidinc(void){_counter++;}};这个技巧有通用名称吗? 最佳答案 我对那个把戏的个人名字是坏主意。我会避免您采用的方法,因为它会产生额外的不必要的

c++ - 公共(public) "using"= decltype(<private>)

在以下(最小化)代码中,我有一个公共(public)using引用decltype(something_private)的声明:usingFoo=decltype(something_private).在Clang而不是GCC上,由于它是私有(private)的,因此无法编译。问题:如果我不想制作func(),有什么优雅的解决方案?上市。在C++标准(C++11)中,备份Clang在这里是正确的吗?以下代码在Clang(3.9-7.0)上失败并出现以下错误代码,但在GCC(4.8.4-8.2)上构建:classA{private:templatestaticautofunc()->T;

c++ - 公共(public) "using"= decltype(<private>)

在以下(最小化)代码中,我有一个公共(public)using引用decltype(something_private)的声明:usingFoo=decltype(something_private).在Clang而不是GCC上,由于它是私有(private)的,因此无法编译。问题:如果我不想制作func(),有什么优雅的解决方案?上市。在C++标准(C++11)中,备份Clang在这里是正确的吗?以下代码在Clang(3.9-7.0)上失败并出现以下错误代码,但在GCC(4.8.4-8.2)上构建:classA{private:templatestaticautofunc()->T;

c++ - 委派到私有(private)领域

有时,C++的隐私概念让我感到困惑:-)classFoo{structBar;Bar*p;public:Bar*operator->()const{returnp;}};structFoo::Bar{voidbaz(){std::coutbaz();//fine}由于Foo::Bar是private,我不能在main中声明b。但是我可以从Foo::Bar调用方法就好了。为什么这是允许的?这是意外还是有意为之?哦,等等,它变得更好了:Foof;autox=f.operator->();//:-)x->baz();即使我不能命名类型Foo::Bar,它也可以与auto一起使用...诺亚写道

c++ - 委派到私有(private)领域

有时,C++的隐私概念让我感到困惑:-)classFoo{structBar;Bar*p;public:Bar*operator->()const{returnp;}};structFoo::Bar{voidbaz(){std::coutbaz();//fine}由于Foo::Bar是private,我不能在main中声明b。但是我可以从Foo::Bar调用方法就好了。为什么这是允许的?这是意外还是有意为之?哦,等等,它变得更好了:Foof;autox=f.operator->();//:-)x->baz();即使我不能命名类型Foo::Bar,它也可以与auto一起使用...诺亚写道

c++ - friend 类对象可以访问派生类对象上的基类私有(private)成员吗?

我很惊讶下面的代码可以编译。似乎与(公共(public)继承的)基类友好的类可以访问基类的成员,前提是派生类的实例。如果继承改为private则编译失败。简而言之,d.b_var在F::func(D&d)中如何有效?#include#includeusingnamespacestd;classB{intb_var;friendclassF;};classD:publicB{intd_var;};classF{public:voidfunc(D&d){d.b_var=5;}};intmain(){cout 最佳答案 classD的对象

c++ - friend 类对象可以访问派生类对象上的基类私有(private)成员吗?

我很惊讶下面的代码可以编译。似乎与(公共(public)继承的)基类友好的类可以访问基类的成员,前提是派生类的实例。如果继承改为private则编译失败。简而言之,d.b_var在F::func(D&d)中如何有效?#include#includeusingnamespacestd;classB{intb_var;friendclassF;};classD:publicB{intd_var;};classF{public:voidfunc(D&d){d.b_var=5;}};intmain(){cout 最佳答案 classD的对象

C++友元函数不能访问私有(private)成员

这应该是一个带有一堆运算符和函数的字符串类,包括两个友元函数。这两个给我带来了一些麻烦,因为编译器说他们不能访问私有(private)成员。这是我的string.h:#include#ifndefSTR_H#defineSTR_HnamespaceMyStr{classStr{private:unsignedintlength;char*data;public:Str();Str(constStr&);Str(constchar*);Str(charc,unsignedintdb);~Str();char*cStr()const;unsignedintgetLength()const;

C++友元函数不能访问私有(private)成员

这应该是一个带有一堆运算符和函数的字符串类,包括两个友元函数。这两个给我带来了一些麻烦,因为编译器说他们不能访问私有(private)成员。这是我的string.h:#include#ifndefSTR_H#defineSTR_HnamespaceMyStr{classStr{private:unsignedintlength;char*data;public:Str();Str(constStr&);Str(constchar*);Str(charc,unsignedintdb);~Str();char*cStr()const;unsignedintgetLength()const;