Questions tagged [const-cast]

Anything related to the `const_cast` operation in C++, i.e. a form of compile-time conversion where the `const` or `volatile` qualifiers are cast away (removed) from a variable.

Anything related to the const_cast operation in C++, i.e. a form of compile-time conversion where the const or volatile qualifiers are cast away (removed) from a variable.

See CPPreference page on const_cast.

231 questions
0
votes
2 answers

Const reference to extend object's life followed by const_cast, is it a good idea?

Consider the following function that returns a big object: std::list DoSomething() { std::list TheList; //do stuff return TheList; } I want to get the list, not a copy of it. Because if I have to copy such…
0
votes
1 answer

Converting UINT to WS_STRING

I'm writing a response to a WCF call in unmanaged code. The problem is that I have an integer which I want to convert into WS_STRING and respond back. The std::towstring converts only to a wstring and doesn't convert into WS_STRING. How can I make…
confusednerd
  • 138
  • 3
  • 17
0
votes
5 answers

Segmentation fault while using const_cast to overload a constant function

I'm having some problems with the const_cast function. I created a class Calorimeter that consists of a CaloGrid and some other stuff. I have to overload the grid() function to return the CaloGrid belonging to the class Calorimeter, however calling…
Rooler
  • 29
  • 5
0
votes
1 answer

Why does a const_cast (or static_cast) not add const?

I was looking at this answer and wanted to use. However, I get a segmentation fault, when using the static_cast and const_cast, but if I use a temp variable everything is fine. It is obviously because the non-const version of bar() calls it self…
Jonas
  • 6,915
  • 8
  • 35
  • 53
0
votes
5 answers

Is const_cast acceptable when defining an array?

I have a static const array class member (const pointers to SDL_Surfaces, but that's irrelevant), and have to loop through it in order to populate it. Aside from a const_cast when I'm done looping, which I hear is bad practice, how would I go about…
Lewis
  • 1,310
  • 1
  • 15
  • 28
0
votes
5 answers

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

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…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
0
votes
1 answer

const_cast const STL container, is it undefined behavior?

I know that use of const_cast won't introduce undefined behaviour (i.e., it's safe to use), as long as the variable you're const_casting wasn't originally defined as const or if it was originally defined as const you're not modifying it via its…
101010
  • 41,839
  • 11
  • 94
  • 168
0
votes
2 answers

Design pattern for accessing non-const through a const intermediate

Can someone suggest a better design for the situation where we want two objects to "talk" to each other through a const intermediate. Here is a contrived example, where two players trade lemons. A player finds another player in the "world", but the…
c z
  • 7,726
  • 3
  • 46
  • 59
0
votes
3 answers

Implications of a const_cast in a copy constructor?

So I've got an output stream class that owns a pointer to a class that actually does the writing, and I need a copy constructor so that I can return initialized instances from a function so that I can bind certain values transparently to the user. …
gct
  • 14,100
  • 15
  • 68
  • 107
0
votes
1 answer

Testing template based class with const template parameter which has to be varied

I am developing a template based library to support fixed point integers and I came up with this class and now I have to test it for various values of INT_BITS and FRAC_BITS. but since they are const (and they have to be so for a reason), I am…
stochastic_zeitgeist
  • 1,037
  • 1
  • 14
  • 21
0
votes
5 answers

Is this a valid use of const_cast?

In the sample code, calling message() will never affect the contents of the class, so I'd like the method to be const. But I don't want the return value also to be const, so is it safe to use const_cast as below? or is there a better solution? EDIT:…
user3810155
0
votes
1 answer

const_cast with two levels pointers

I want to do this conversion using C++ format, it works on the C way. but it fails when I try on C++ format. It works! void req_password(const void *data, size_t datalen) { char *password_old = ((char **) data)[0]; char *password_new =…
Alex
  • 3,301
  • 4
  • 29
  • 43
0
votes
3 answers

Converting a std string to char* using const cast

After days of attempting to create a shell I ask for a bit of help. I have started over 4 or so times with different data structures and plea for a solution to the below problem. I have a string that I need to break into individual arguments and…
0
votes
1 answer

2 value in one variable (const and const_cast) c++

void main() { const int a = 10; const int *b = &a; int *c = const_cast (b); *c = 5; cout<
0
votes
2 answers

C++ const-cast a reference

Is this correct? It compiles with my compiler but I've been told it doesn't with an AIX one. typedef std::vector::iterator Iterator; typedef std::vector::const_iterator ConstIterator; bool funct(ConstIterator& iter) const; inline…
jimifiki
  • 5,377
  • 2
  • 34
  • 60