有人可以告诉我类private和protected成员之间的区别吗?
我从最佳实践约定中了解到应该在类外部调用的变量和函数private- 但是看看我的MFC项目,MFC似乎更喜欢protected.
有什么区别,我应该使用哪个?
我试图找出Java中常量的原因我已经了解到Java允许我们使用final关键字来声明常量.
我的问题是为什么Java没有引入Constant(const)功能.由于很多人说它来自C++,所以在C++中我们有const关键字.
请分享你的想法.
在C++中,你可以像这样声明lambdas:
int x = 5;
auto a = [=]() mutable { ++x; std::cout << x << '\n'; };
auto b = [&]() { ++x; std::cout << x << '\n'; };
Run Code Online (Sandbox Code Playgroud)
两个都让我修改x,那有什么区别?
这样的事情之间有什么区别
friend Circle copy(const Circle &);
Run Code Online (Sandbox Code Playgroud)
和这样的事情
friend Circle copy(Circle&) const;
Run Code Online (Sandbox Code Playgroud)
我知道const后,函数被用来告诉编译器这个函数不会尝试更改它被调用的对象,那么另一个呢?
在C++中,你会看到void func(const T& t)无处不在.但是,我在.NET中没有看到类似的东西.为什么?
我注意到使用struct的参数很多.但是我看不到readonly/const的函数.事实上,现在我尝试了它,我不能使用这些关键字来制作一个承诺不修改传入的列表的函数.有没有办法让调用者承诺这个函数永远不会修改列表的内容?有没有办法说调用代码并说这个列表永远不应该被修改?(我知道我可以克隆列表或查看文档,但我喜欢编译错误)
__attribute__((const))和__attribute__((pure))GNU C有什么区别?
__attribute__((const)) int f() {
/* ... */
return 4;
}
Run Code Online (Sandbox Code Playgroud)
VS
__attribute__((pure)) int f() {
/* ... */
return 4;
}
Run Code Online (Sandbox Code Playgroud) 我有一个课我希望能够比较平等.这个类很大(它包含一个位图图像),我会多次比较它,所以为了提高效率,我要对数据进行哈希处理,只检查哈希是否匹配.此外,我将只比较我的对象的一小部分,所以我只是在第一次完成相等性检查时计算哈希值,然后使用存储的值进行后续调用.
class Foo
{
public:
Foo(int data) : fooData(data), notHashed(true) {}
private:
void calculateHash()
{
hash = 0; // Replace with hashing algorithm
notHashed = false;
}
int getHash()
{
if (notHashed) calculateHash();
return hash;
}
inline friend bool operator==(Foo& lhs, Foo& rhs)
{
if (lhs.getHash() == rhs.getHash())
{
return (lhs.fooData == rhs.fooData);
}
else return false;
}
int fooData;
int hash;
bool notHashed;
};
Run Code Online (Sandbox Code Playgroud)
根据这个答案的指导,平等运算符的规范形式是:
inline bool operator==(const X& lhs, const X& rhs);
此外,操作符重载以下一般建议 …
我正在努力解决各种各样的地方之间的差异,你可以把'const'放在c ++的函数声明中.
const开头有什么区别:
const int MyClass::showName(string id){
...
}
Run Code Online (Sandbox Code Playgroud)
最后的const就像:
int MyClass::showName(string id) const{
...
}
Run Code Online (Sandbox Code Playgroud)
另外,在开头和结尾都有这样的结果是什么:
const int MyClass::showName(string id) const{
...
}
Run Code Online (Sandbox Code Playgroud) 我想存储一堆键值对象,但值对象本身(及其对它的引用)知道它的键.我还想在只给出密钥的情况下有效地查找这些对象.
class SomeObject
{
private:
//String or integer. int seem cheap enough to duplicate with std::map, but
//strings seem pretty expensive when there may be thousands of objects in existence.
//Reference/Pointer to key is fine
const SomeOtherObject key;
...other stuff...
public:
...methods, some of which use the key in some way...
};
Run Code Online (Sandbox Code Playgroud)
在一个子类中,我有一个私有std::mutex m字段,我在基类纯虚方法的实现中使用它以线程安全的方式返回一个值(该值可以由另一个线程更新):
int SubClass::get() const // implements 'virtual int get() = 0 const' of the base class
{
std::lock_guard<std::mutex> lck(m);
return value;
}
Run Code Online (Sandbox Code Playgroud)
编译器通过产生错误告诉我这违反了const正确性:
错误:将'const std :: mutex'绑定到类型'std :: lock_guard :: mutex_type&{aka std :: mutex&}'的引用将丢弃限定符
有没有办法使这种兼容并std::lock_guard以const正确的方式使用?只需改变它就不会const std::lock_guard改变任何东西 我真的不明白哪个部分有问题,然后我再次对C++很新...
c++ ×7
const ×3
constants ×3
.net ×1
attributes ×1
c ×1
c++11 ×1
class ×1
comparison ×1
containers ×1
dictionary ×1
equality ×1
final ×1
function ×1
gcc ×1
gnu ×1
java ×1
lambda ×1
mfc ×1
stl ×1