我们是否可以增加这种面向密钥的访问保护模式的可重用性:
class SomeKey {
friend class Foo;
// more friends... ?
SomeKey() {}
// possibly non-copyable too
};
class Bar {
public:
void protectedMethod(SomeKey); // only friends of SomeKey have access
};
Run Code Online (Sandbox Code Playgroud)
为了避免持续的误解,这种模式不同于律师 - 客户的习惯用语:
(在这个问题中提出了一个侧面讨论,因此我打开了这个问题.)
可能重复:
函数参数使用'const'
例如,
void Func(int para);
Run Code Online (Sandbox Code Playgroud)
如果我知道我不想在Func(...)中更改para的值,我是否应该将para声明为“ const int”以保证这一点?即
void Func(const int para);
Run Code Online (Sandbox Code Playgroud)
顺便说一句,我认为
void Func(const int ¶);
Run Code Online (Sandbox Code Playgroud)
有时不是合适的替代方法,因为const int&通常是由基础指针实现的,因此它基本上等效于
void Func(const int *p_para);
Run Code Online (Sandbox Code Playgroud)
《 C ++编码标准:101条规则》一书第31页说:“ void Func(const int para);” 不好,因为它会“混淆”头文件的阅读器。我不确定...
经过深思熟虑,我认为一个好的解决方案是将其声明为“ void Func(int para);”。并在实现它时使用“ void Func(const int para){...}”。我在“ void Func(const int para);”中发现“ const”一词。将被编译器静默删除...
所以,我有一个程序使用图形模式[ graphics.h]库...我想初始化图形,所以我会这样做,所以自然地这样做:
initgraph(graphics_driver,graphics_mode,"") ;
Run Code Online (Sandbox Code Playgroud)
当我编译上面的代码时,它会给出错误"ISO C++禁止将字符串常量转换为char*"
我知道一个解决方法:
char c_array[] = "" ;
initgraph(graphics_driver,graphics_mode,c_array) ;
Run Code Online (Sandbox Code Playgroud)
上面编译得很好......这对于像...这样的函数是可以的initgraph(),因为我只会调用一次.但是,我想使用这样的outtextxy()函数(因为我在我的程序中多次调用它):
outtextxy(0,0,"Test") ;
Run Code Online (Sandbox Code Playgroud)
因为为所有不同的outtextxy()函数声明一个数组只会浪费空间.
那么,有没有办法使用上面没有数组或任何额外的变量?
PS:我在安装graphics.h库并配置所有链接器选项后使用代码块.等等...
谢谢,再见,塞缪尔