Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use const_cast?

Tags:

c++

const-cast

I have a private variable in my Student class defined as:

const int studentNumnber; 

I am trying to write a copy constructor for the Student and I need to cast away the constness to do this. Unfortunately, I don't understand how to use std::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);     } 

That doesn't work... I am unsure of the syntax.

like image 865
Sarah Avatar asked Oct 24 '13 00:10

Sarah


People also ask

When should I use const_cast?

const_cast is one of the type casting operators. It is used to change the constant value of any object or we can say it is used to remove the constant nature of any object. const_cast can be used in programs that have any object with some constant value which need to be changed occasionally at some point.

Why is const_cast allowed?

When it comes to removing constness, the purpose of const_cast is to allow you remove constness from an access path (pointer or reference) that leads to a non-constant object.

Is const_cast safe?

const_cast is safe only if you're casting a variable that was originally non- const . For example, if you have a function that takes a parameter of a const char * , and you pass in a modifiable char * , it's safe to const_cast that parameter back to a char * and modify it.

How do you const cast in C++?

First create a constant variable of type int and give it some suitable size, let's say “a” and its value be 20. Then create a constant pointer, let us say “b” of the same data type and allocate it the address of our constant variable “a”.


1 Answers

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) {     // ... } 
like image 82
timrau Avatar answered Sep 18 '22 16:09

timrau