在编写一个带有传递给它的args的C++函数时,如果可以保证不改变对象,则应始终使用const,如果指针不会被更改,则应始终使用const指针.
这种做法何时建议?
你什么时候使用const引用,比仅通过指针传递它有什么好处?
那么void MyObject::Somefunc(const std::string& mystring) 如果一个字符串实际上已经是一个不可变对象,那么拥有一个const字符串会有什么意义呢?
我需要从非const对象调用const函数.见例子
struct IProcess {
virtual bool doSomeWork() const = 0L;
};
class Foo : public IProcess {
virtual bool doSomeWork() const {
...
}
};
class Bar
{
public:
const IProcess& getProcess() const {return ...;}
IProcess& getProcess() {return ...;}
void doOtherWork {
getProcess().doSomeWork();
}
};
Run Code Online (Sandbox Code Playgroud)
调用
getProcess().doSomeWork();
Run Code Online (Sandbox Code Playgroud)
总是会打电话给
IProcess& getProcess()
Run Code Online (Sandbox Code Playgroud)
还有另一种方式来打电话
const IProcess& getProcess() const
Run Code Online (Sandbox Code Playgroud)
来自非常数成员函数?我到目前为止使用过
const_cast<const Bar*>(this)->getProcess().doSomeWork();
Run Code Online (Sandbox Code Playgroud)
这样做的技巧,但似乎过于复杂.
编辑:我应该提到代码正在重构,最终只剩下一个函数.
const IProcess& getProcess() const
Run Code Online (Sandbox Code Playgroud)
但是,目前存在副作用,并且const调用可能在某些时候返回不同的IProcess实例.
请继续主题.
首先,示例代码:
情况1:
typedef char* CHARS;
typedef CHARS const CPTR; // constant pointer to chars
Run Code Online (Sandbox Code Playgroud)
文本替换CHARS变为:
typedef char* const CPTR; // still a constant pointer to chars
Run Code Online (Sandbox Code Playgroud)
案例2:
typedef char* CHARS;
typedef const CHARS CPTR; // constant pointer to chars
Run Code Online (Sandbox Code Playgroud)
文本替换CHARS变为:
typedef const char* CPTR; // pointer to constant chars
Run Code Online (Sandbox Code Playgroud)
在案例2中,在文本替换CHARS之后,typedef的含义发生了变化.为什么会这样?C++如何解释这个定义?
这似乎是未定义的行为
union A {
int const x;
float y;
};
A a = { 0 };
a.y = 1;
Run Code Online (Sandbox Code Playgroud)
规范说
在存储位置创建一个具有静态,线程或自动存储持续时间的const对象占用的新对象,或者在此生命周期结束之前这样的const对象占用的存储位置处导致未定义的行为.
但是没有编译器警告我,因为它很容易诊断错误.我是否误解了措辞?
示例代码段
const const const int x = 10;
int main()
{}
Run Code Online (Sandbox Code Playgroud)
用C语言编译而不是用C++编译.为什么用C编译?我认为这也会在C中失败.没关系.
C++标准的哪一部分禁止使用复制品const,哪部分C标准允许这样做?
我有一个课我希望能够比较平等.这个类很大(它包含一个位图图像),我会多次比较它,所以为了提高效率,我要对数据进行哈希处理,只检查哈希是否匹配.此外,我将只比较我的对象的一小部分,所以我只是在第一次完成相等性检查时计算哈希值,然后使用存储的值进行后续调用.
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);
此外,操作符重载以下一般建议 …
考虑一下代码:
int const x = 50;
int const& y = x;
cout << std::is_const<decltype(x)>::value << endl; // 1
cout << std::is_const<decltype(y)>::value << endl; // 0
Run Code Online (Sandbox Code Playgroud)
这是有道理的,因为y不是const参考,它是对a的引用const.
有没有foo这样std::foo<decltype(y)>::value的1?如果没有,那么定义我自己的是什么样的呢?
请注意,我std::thread只是用来获取错误中的可读类型:
int main() {
const int * first;
using deref = decltype(*first);
std::thread s = std::remove_const<deref>::type{}; // const int ???
std::thread s2 = deref{}; // const int
std::thread s3 = std::remove_const<const int>::type{}; // int
}
Run Code Online (Sandbox Code Playgroud)
似乎remove_const<deref>::type是const int,不像int我期望的那样可变.
有没有办法定义一个指针数组,以便任何指针是const?
例如,可以char** array定义array[0]为const和array[1]const等,但是array非const并且array[j][i]是非const?
我正在使用Stroustrup(Programming Principles&Practice Using C++)一书学习C++.在练习中,我们定义了一个简单的结构:
template<typename T>
struct S {
explicit S(T v):val{v} { };
T& get();
const T& get() const;
void set(T v);
void read_val(T& v);
T& operator=(const T& t); // deep copy assignment
private:
T val;
};
Run Code Online (Sandbox Code Playgroud)
然后我们要求定义一个const和一个非const成员函数来获取val.
我想知道:有任何情况下get,返回非const 函数是否有意义val?
对我来说似乎更清洁,我们无法间接改变这种情况下的价值.在需要const和非const get函数返回成员变量的情况下可能会出现什么情况?