如何使用const_cast? [英] how to use const_cast?

查看:239
本文介绍了如何使用const_cast?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个私有变量定义为:

  const int studentNumnber; 

我想写一个复制构造函数,我需要丢弃constness这样做,不幸的是我不明白如何使用const_cast。



这是我想在我的副本构造函数中执行:

  Student(const Student& s)
:Person(p.getName(),p.getEmailAddress(),p.getBirthDate ,studentNumber(0){
school = new char [strlen(s.school)+1];
strcpy_s(school,strlen(s.school)+1,s.school);
const_cast< int *>(this) - > studentNumber = s.studentNumber;
// studentNumber = s.studentNumber);
}

这不工作...我不确定的语法

解决方案

您不能允许 const_cast 实际上 const 。这导致未定义的行为。 const_cast 用于从最终引用不是 const 的引用和指针中删除常量。 / p>

所以,这是允许的:

  int i = 
const int& ref = i;
const int * ptr =& i;

const_cast< int&>(ref)= 3;
* const_cast< int *>(ptr)= 3;

这是允许的,因为 i 分配给,不是 const 。以下是不允许的:

  const int i = 0; 
const int& ref = i;
const int * ptr =& i;

const_cast< int&>(ref)= 3;
* const_cast< int *>(ptr)= 3;

因为这里 i const ,您正在通过为其分配一个新值来修改它。代码将编译,但它的行为是未定义的(这可能意味着从它的工作正常到程序将崩溃。)



您应该初始化常量在构造函数的初始化器中的数据成员,而不是在构造函数体中分配它们:

  Student(const Student& s)
:Person(p.getName(),p.getEmailAddress(),p.getBirthDate()),
school(0),
studentNumber(s.studentNumber)
{
// ...
}


I have a private variable defined as:

const int studentNumnber;

I am trying to write a copy constructor and I need to cast away the constness to do this, unfortunately I don't understand how to use const_cast.

This is what I am trying to do in my copy constructor:

    Student(const Student & s) 
        : Person(p.getName(), p.getEmailAddress(), p.getBirthDate()), school(0),     studentNumber(0){
        school = new char[strlen(s.school) +1];
        strcpy_s(school, strlen(s.school) +1, s.school);
        const_cast<int*>(this)->studentNumber = s.studentNumber;
            //studentNumber= s.studentNumber);
    }

This doesn't work... I am unsure of what the syntax is to do this

解决方案

You are not allowed to const_cast variables that are actually const. This results in undefined behavior. const_cast is used to remove the const-ness from references and pointers that ultimately refer to something that is not const.

So, this is allowed:

int i = 0;
const int& ref = i;
const int* ptr = &i;

const_cast<int&>(ref) = 3;
*const_cast<int*>(ptr) = 3;

It's allowed because i, the object being assigned to, is not const. The below is not allowed:

const int i = 0;
const int& ref = i;
const int* ptr = &i;

const_cast<int&>(ref) = 3;
*const_cast<int*>(ptr) = 3;

because here i is const and you are modifying it by assigning it a new value. The code will compile, but its behavior is undefined (which can mean anything from "it works just fine" to "the program will crash".)

You should initialize constant data members in the constructor's initializers instead of assigning them in the body of constructors:

Student(const Student & s) 
    : Person(p.getName(), p.getEmailAddress(), p.getBirthDate()),
      school(0),
      studentNumber(s.studentNumber)
{
    // ...
}

这篇关于如何使用const_cast?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆