const-cast

C++ const_cast usage instead of C-style casts

你离开我真会死。 提交于 2019-12-19 17:41:25
问题 Why is the following?: const int i0 = 5; //int i1 = const_cast<int>(i0); // compilation error int i2 = (int)i0; // okay int i3 = 5; //const int i4 = const_cast<const int>(i3); // compilation error const int i5 = (const int)i3; // okay 回答1: const int i0 = 5; //int i1 = const_cast<int>(i0); // compilation error int i2 = (int)i0; // okay int i3 = 5; //const int i4 = const_cast<const int>(i3); // compilation error const int i5 = (const int)i3; // okay The compilation errors are caused because you

Am I right in saying that const_cast followed by modification on a ref-to-const bound to a temporary is okay?

╄→尐↘猪︶ㄣ 提交于 2019-12-18 12:55:40
问题 I would like to check my understanding and conclusions on this matter. On IRC, it was asked: Is it acceptable to const_cast a const reference that's bound to a temporary object? Translating: he has a ref-to-const bound to a temporary, and he wants to cast away its const -ness to modify it. My response was that I'd asked a similar question previously, where the consensus seemed to be that temporaries themselves are not inherently const , and thus that you can cast off the const -ness of a

How to convert “pointer to pointer type” to const?

一个人想着一个人 提交于 2019-12-17 09:51:04
问题 With the following code void TestF(const double ** testv){;} void callTest(){ double** test; TestF(test); } I get this: 'TestF' : cannot convert parameter 1 from 'double **' to 'const double **' I cannot understand why. Why test cannot be silently casted to const double** ? Why should I do it explicitly? I know that TestF(const_cast<const double**>(test)) makes my code correct, but I feel this should be unnecessary. Are there some key concepts about const that I'm missing? 回答1: The language

How to use const_cast?

南笙酒味 提交于 2019-12-17 07:05:42
问题 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

If class A modifies its construction parameters, can I initialize const A's with const parameters?

时光毁灭记忆、已成空白 提交于 2019-12-12 03:44:20
问题 Suppose I have class A final { int& ir; public: A(int& x) : ir(x) { } void set(int y) { ir = y; } // non-const method! int get() const { return ir; } }; and const int i; Obviously I can't have A a(i); since that would breaks constness. But I also cannot have const A a(i); despite the fact that this will not break constness de-facto. C++ doesn't support "const-only" ctors, e.g. in this case one which would take a const int& . Is there a way to get const A a wrapping a reference to i - other

how can const_cast<const string &>(s), while s is a string type?

情到浓时终转凉″ 提交于 2019-12-11 18:28:07
问题 I asked a related, tedious question before and now I discover something new. #include <iostream> #include <string> using namespace std; void hello (const string &s1) { cout << "rocky" << endl; } void hello(string &s1) { cout << "banana" << endl; } int main() { string s = "abc"; hello(const_cast<const string&>(s)); //how to explain this const_cast? } hello(const_cast<const string&>(s)); this works and match the const reference parameter function. So how's the conversion this time? Isn't it

How to get alternative value from function that gives wanted data via non-const output parameter for assigning reference-to-const variable to it?

ε祈祈猫儿з 提交于 2019-12-11 12:52:30
问题 The commented code works, but it is not a reference, so it has more computational cost. void CClass::Function(const CArray<CItem*>* ItemsInput) const { /* CArray<CItem*> Items; if (ItemsInput != nullptr) Items.Copy(*ItemsInput); else GetContainer().GetInnerItems(Items, NULL, true); */ const CArray<CItem*>& Items= (ItemsInput!= nullptr)? *ItemsInput : [this] () -> const CArray<CItem*> { CArray<CItem*> InnerItems; GetContainer().GetInnerItems(InnerItems, NULL, true); return const_cast <const

C++. Why I can't compile this code? What is wrong with removing constness using const_cast?

我们两清 提交于 2019-12-11 05:56:10
问题 I have some problem removing constness using const_cast. Error msg says "Conversion is a valid standard conversion....." What is the nature of this problem? Why should I use C-style cast instead? "error C2440: 'const_cast' : cannot convert from 'const size_t' to 'size_t'" "Conversion is a valid standard conversion, which can be performed implicitly or by use of static_cast, C-style cast or function-style cast" template<typename T> const IFixedMemory* FixedMemoryPkt<T>::operator=(const

Writing to class member through const &

白昼怎懂夜的黑 提交于 2019-12-11 03:39:17
问题 In this example, is the c-style cast to int& followed by an assignment to kind of hack the interface of class A undefined behavior? class A { public: A() : x(0) { } ~A() { std::cout << x << std::endl; } const int& getX() { return x; } private: int x; }; int main() { A a; int& x = (int&)a.getX(); x = 17; std::cout << x << std::endl; } Output: 17 17 If so, what part of the standard can i refer to? Also, is there any reason why this compiles without warnings? (i tested with c++14 on cpp.sh with

const_cast std::vector

依然范特西╮ 提交于 2019-12-11 03:35:44
问题 I have a vector with type const std::vector<const Array*> &objects which is passed as an argument to the method. I want to const_cast but I can't do it for some reason. I have tried: vector<const Array*> obj = const_cast<const std::vector<const Array*> >(objects); and some other ways but it keeps complaining. Any ideas ? 回答1: Firstly, don't . Change your function's signature to take a non-const reference, you risk UB by doing this: const std::vector<const Array*> myData = /* fill */;