假设class X我想要返回内部成员的访问权限:
class Z
{
// details
};
class X
{
std::vector<Z> vecZ;
public:
Z& Z(size_t index)
{
// massive amounts of code for validating index
Z& ret = vecZ[index];
// even more code for determining that the Z instance
// at index is *exactly* the right sort of Z (a process
// which involves calculating leap years in which
// religious holidays fall on Tuesdays for
// the next thousand years or so)
return ret;
}
const …Run Code Online (Sandbox Code Playgroud) 我有一个声明常量和非常量成员函数的抽象类。为了讨论起见,让我们说它看起来像这样:
class record_interface
{
public:
virtual ~record_interface() = default;
virtual void set_foo(BoundedFloat) = 0;
virtual BoundedFloat get_foo() const = 0;
};
Run Code Online (Sandbox Code Playgroud)
这用作记录的高级表示,当保存到光盘并通过线路传输时,该记录具有不同的表示。所以大多数实现只需要将它们的成员转换为所需的高级表示。
作为有效实现的示例,让我们定义stored_record. 这用于以有损格式存储高级记录:
struct stored_record
{
int16_t foo;
};
Run Code Online (Sandbox Code Playgroud)
stored_record可以实现是有道理的,record_interface但由于各种原因它不能(例如,它需要是trivially_copyable)。我们可以制作一个为其实现接口的包装器:
class record_wrapper : public record_interface
{
public:
record_wrapper(stored_record & wrapped)
: wrapped_(wrapped) {}
void set_foo(BoundedFloat value) final { wrapped_.foo = convert_to_int16(value); }
BoundedFloat get_foo() const final { return convert_from_int16(wrapped_.foo); }
private:
stored_record & wrapped_;
};
Run Code Online (Sandbox Code Playgroud)
现在的问题是当给定 a 时我们不能使用包装器,const stored_record &因为包装器存储了一个可变引用。我们也不能让它存储非常量引用,因为它无法实现非常量 …
代码说出了上千个单词,所以...
这是使a发生突变的未定义行为const int:
struct foo {
int x;
void modify() { x = 3; }
};
void test_foo(const foo& f) {
const_cast<foo&>(f).modify();
}
int main(){
const foo f{2};
test_foo(f);
}
Run Code Online (Sandbox Code Playgroud)
那这个呢:
struct bar {
void no_modify() { }
};
void test_bar(const bar& b) {
const_cast<bar&>(b).no_modify();
}
int main(){
const bar b;
test_bar(b);
}
Run Code Online (Sandbox Code Playgroud)
const_cast当方法不改变对象时,是否可以通过const对象调用非const方法?
PS:我知道no_modify应该将其声明为const,然后这个问题毫无意义,但假设bars的定义不能更改。
PPS:只需确保:不要在家中(或其他任何地方)这样做。我绝不会让这样的代码通过审查。可以轻松避免强制转换。这是一个语言律师问题。
我有这样的头文件foo.h(省略了无关的东西):
#pragma once
#include <memory>
class Bar;
struct Foo
{
std::shared_ptr<Bar> getBar();
std::shared_ptr<const Bar> getBar() const
{
return const_cast<Foo*>(this)->getBar();
}
};
Run Code Online (Sandbox Code Playgroud)
非const重载getBar()在.cpp文件中实现,该文件也可以看到完整定义Bar.
当foo.h包含在另一个文件(没有看到定义Bar)时,VS 2010会给我一个这样的警告:
warning C4150: deletion of pointer to incomplete type 'Bar'; no destructor called
Run Code Online (Sandbox Code Playgroud)
在const重载getBar()(或实际上在从该重载实例化的标准库中的深处).
我的问题是这个警告是否可以安全地被忽略.
我看待它的方式,有两个std::shared_ptr<Bar>被调用的成员函数getBar() const:转换构造函数和析构函数.
// converting constructor
template <class Y>
std::shared_ptr<const Bar>::shared_ptr(std::shared_ptr<Y> &&r)
Run Code Online (Sandbox Code Playgroud)
这用于初始化返回值getBar() const的返回值getBar().这并未列出任何先决条件(C++ 1127.2.2.1§20-22),这些先决条件需要Y(Bar在我的情况下)完成.
// destructor
std::shared_ptr<const Bar>::~shared_ptr() …Run Code Online (Sandbox Code Playgroud) c++ ×4
const ×2
c++-faq ×1
c++11 ×1
class ×1
const-cast ×1
constants ×1
interface ×1
shared-ptr ×1