将常数T*常数转换为T* 这是不是可以在C++中进行这种转换? 我需要以这种方式声明我的属性 Class A { public: void update() { ++i_; } private: int i_; } Class B{ public: void foo() { a_->update(); /* Error */ } private: const A* const a_; }
错误是: 将“const A”作为“void A::update”的“this”参数传递将丢弃 限定符[-fppermissive]将常数T*常数转换为T* 这是不是可以在C++中进行这种转换? 我需要以这种方式声明我的属性 Class A { public: void update() { ++i_; } private: int i_; } Class B{ public: void foo() { a_->update(); /* Error */ } private: const A* const a_; },c++,pointers,casting,C++,Pointers,Casting,错误是: 将“const A”作为“void A::update”的“this”参数传递将丢弃 限定符[-fppermissive] 我尝试使用静态_cast,但还不够。。不起作用。。有什么想法吗?假设您不能将该方法声明为const,并且您知道自己在做什么,以及为什么这样做不好:尝试const_cast 假设您不能将该方法声明为const,并且您知道自己在做什么,以及为什么这样做不好:尝试const_cast 除非使用mutable,否则禁止将常量成员与非常量方法一起使用。在声明foo和upda
我尝试使用静态_cast,但还不够。。不起作用。。有什么想法吗?假设您不能将该方法声明为const,并且您知道自己在做什么,以及为什么这样做不好:尝试const_cast 假设您不能将该方法声明为const,并且您知道自己在做什么,以及为什么这样做不好:尝试const_cast 除非使用mutable,否则禁止将常量成员与非常量方法一起使用。在声明foo和update后放置常量: 或者 如果不想将更新设置为常量,可以使用常量强制转换:
禁止将常量成员与非常量方法一起使用,除非使用mutable。在声明foo和update后放置常量: 或者 如果不想将更新设置为常量,可以使用常量强制转换:
*之后的常量与此无关。必须声明使用uu作为常量函数的函数。如果它们不编译,则必须在调用之前使用const_cast或reinterpret_cast从指针中删除const。。但这是非常狡猾的,如果update函数修改一个最初声明为const的对象,这是未定义的行为。后面的const*与此无关。必须声明使用uu作为常量函数的函数。如果它们不编译,则必须在调用之前使用const_cast或reinterpret_cast从指针中删除const。。但这是非常狡猾的,如果update函数修改了一个最初声明为const的对象,这就是未定义的行为。您有几个选项: 使用const_cast丢弃该const并调用该方法 使update成为const方法,以便可以通过const指针调用它 首先不要将一个_uu存储为常量。将其更改为*const A_uu,以便可以调用非常量方法,但指针不能更改
您有几个选择: 使用const_cast丢弃该const并调用该方法 使update成为const方法,以便可以通过const指针调用它 首先不要将一个_uu存储为常量。将其更改为*const A_uu,以便可以调用非常量方法,但指针不能更改 你得到的错误 将“const A”作为“void A::update”的“this”参数传递将丢弃 限定符[-fppermissive] 是对常量指针调用非常量方法的结果。这是禁止的。由于您要求更新为非常量,并且无法存储指向非常量a的指针,因此可以使用运算符const_cast来丢弃常量,以下是如何做到这一点:
class A {
public:
void update(){}
};
class B{
public:
void foo() {
const_cast< A*>(a_)->update(); /* OK*/
}
private:
const A* const a_;
};
但是,您应该重新考虑您的设计。您所遇到的错误
将“const A”作为“void A::update”的“this”参数传递将丢弃
限定符[-fppermissive]
是对常量指针调用非常量方法的结果。这是禁止的。由于您要求更新为非常量,并且无法存储指向非常量a的指针,因此可以使用运算符const_cast来丢弃常量,以下是如何做到这一点:
class A {
public:
void update(){}
};
class B{
public:
void foo() {
const_cast< A*>(a_)->update(); /* OK*/
}
private:
const A* const a_;
};
但是,您应该重新考虑您的设计。您有两个选择。或者生成一个::更新一个常量函数-
Class A {
void update() const;
}
或者删除指针的常量
Class B{
public:
void foo() {
const_cast<A*>(a_)->update();
}
private:
const A* const a_;
}
前者将是首选方法,但这也将阻止您在类A的更新中执行任何有用的操作
根据经验,如果你必须把常量从某个东西上去掉,那么你首先要看看指针为什么是常量。这里有两个选择。或者生成一个::更新一个常量函数-
Class A {
void update() const;
}
或者删除指针的常量
Class B{
public:
void foo() {
const_cast<A*>(a_)->update();
}
private:
const A* const a_;
}
前者将是首选方法,但这也将阻止您在类A的更新中执行任何有用的操作
根据经验,如果你必须去掉某个常量,那么首先你真的想看看指针为什么是常量。如前所述,使用mutable/const,但这会稍微改变你的设计:
class A {
public:
void update() const { ++i_; } // this will make the method callable by const A*
private:
mutable int i_; // and this will make you field mutable in a const method
};
class B{
public:
void foo() {
a_->update();
}
private:
const A* const a_;
};
如前所述,使用mutable/const,但这会稍微改变您的设计:
class A {
public:
void update() const { ++i_; } // this will make the method callable by const A*
private:
mutable int i_; // and this will make you field mutable in a const method
};
class B{
public:
void foo() {
a_->update();
}
private:
const A* const a_;
};
正如你在答案中看到的,你需要改变你的设计。您有一个指向const的指针,因此对对象调用非const方法的唯一方法是const_cast。但随后您确实修改了object++i,因此这是未定义的行为。即使现在起作用,明天也不行。更改您的设计。@iavr:等一下,const_cast并不一定意味着未定义的行为;只有当指向的对象首先被创建为const时,您才有未定义的行为。正如您在答案中看到的,您需要更改您的设计。您有一个指向const的指针,因此对对象调用非const方法的唯一方法是const_cast。但是你要修改object++i,所以这是 未定义的行为。即使现在起作用,明天也不行。更改您的设计。@iavr:等一下,const_cast并不一定意味着未定义的行为;只有当指向的对象首先创建为const时,才有未定义的行为。