const_cast question

D

drowned

all right, check it out... I've got a practice exercise from "thinking
in c++" whose answer isn't covered in the annotated solutions guide,
so I'm trying to handle it, but I don't understand what I'm doing
wrong. Here is the exercise:

27. Create a const array of double and a volatile array of double.
Index through each array and use const_cast to cast each element to
non-const and non-volatile, respectively, and assign a value to each
element. //end exercise


now, here's what I came up with:

#include <iostream>
using namespace std;

int main() {
const double cd[3] = {3,3,3};
volatile double vd[3];
for(int i=0;i<3;i++) {
double t1 = const_cast<double>(cd);
double t2 = const_cast<double>(vd);
cd = t1;
vd = t2;
cout << "cd[" << i << "] = " << cd << endl;
cout << "vd[" << i << "] = " << vd << endl;
}
}

when I try to compile, I get a bunch of errors about invalid use of
const_char with type 'double'. I've tried a bunch of variations on
the two lines that include const_char, but none of them work... I
obviously don't understand this concept. Please help.
 
J

Josephine Schafer

drowned said:
all right, check it out... I've got a practice exercise from "thinking
in c++" whose answer isn't covered in the annotated solutions guide,
so I'm trying to handle it, but I don't understand what I'm doing
wrong. Here is the exercise:

27. Create a const array of double and a volatile array of double.
Index through each array and use const_cast to cast each element to
non-const and non-volatile, respectively, and assign a value to each
element. //end exercise


now, here's what I came up with:

#include <iostream>
using namespace std;

int main() {
const double cd[3] = {3,3,3};
volatile double vd[3];
for(int i=0;i<3;i++) {
double t1 = const_cast<double>(cd);

Change to -
double *t1 = const_cast said:
double t2 = const_cast<double>(vd);

Change to -
double *t2 = const_cast said:
cd = t1;
vd = t2;
cout << "cd[" << i << "] = " << cd << endl;
cout << "vd[" << i << "] = " << vd << endl;
}
}


Additionally, be aware of the problems that could be caused when you cast
away the constness of a const object.
 
J

John Harrison

drowned said:
all right, check it out... I've got a practice exercise from "thinking
in c++" whose answer isn't covered in the annotated solutions guide,
so I'm trying to handle it, but I don't understand what I'm doing
wrong. Here is the exercise:

27. Create a const array of double and a volatile array of double.
Index through each array and use const_cast to cast each element to
non-const and non-volatile, respectively, and assign a value to each
element. //end exercise


now, here's what I came up with:

#include <iostream>
using namespace std;

int main() {
const double cd[3] = {3,3,3};
volatile double vd[3];
for(int i=0;i<3;i++) {
double t1 = const_cast<double>(cd);
double t2 = const_cast<double>(vd);
cd = t1;
vd = t2;
cout << "cd[" << i << "] = " << cd << endl;
cout << "vd[" << i << "] = " << vd << endl;
}
}

when I try to compile, I get a bunch of errors about invalid use of
const_char with type 'double'. I've tried a bunch of variations on
the two lines that include const_char, but none of them work... I
obviously don't understand this concept. Please help.


You need to understand array to pointer conversion to do this exercise.

When you write

a = 1.0;

the array a is converted to a pointer (e.g. double*), and then the []
operator is applied to this pointer. Because cv is a const array it is
actually converted to a const double* pointer. Because of this the
assignment fails.

The const_cast you need to apply is from const double* to double* so you can
perform the assignment.

(const_cast<double*>(cv)) = 1.0;

Exercise is very badly worded, not surprising you didn't get it, or maybe I
didn't!

john
 
J

Josephine Schafer

Josephine Schafer said:
drowned said:
all right, check it out... I've got a practice exercise from "thinking
in c++" whose answer isn't covered in the annotated solutions guide,
so I'm trying to handle it, but I don't understand what I'm doing
wrong. Here is the exercise:

27. Create a const array of double and a volatile array of double.
Index through each array and use const_cast to cast each element to
non-const and non-volatile, respectively, and assign a value to each
element. //end exercise


now, here's what I came up with:

#include <iostream>
using namespace std;

int main() {
const double cd[3] = {3,3,3};
volatile double vd[3];
for(int i=0;i<3;i++) {
double t1 = const_cast<double>(cd);

Change to -
double *t1 = const_cast said:
double t2 = const_cast<double>(vd);

Change to -
double *t2 = const_cast<double*>(vd+ i);

cd = t1;
vd = t2;

Ofcourse you need to change the above two lines also now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top