By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

const_cast question

P: n/a
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[i]);
double t2 = const_cast<double>(vd[i]);
cd[i] = t1;
vd[i] = t2;
cout << "cd[" << i << "] = " << cd[i] << endl;
cout << "vd[" << i << "] = " << vd[i] << 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.
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"drowned" <vo**********@hotmail.com> wrote in message
news:6d**************************@posting.google.c om...
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[i]); Change to -
double *t1 = const_cast<double*>(cd+ i);
double t2 = const_cast<double>(vd[i]); Change to -
double *t2 = const_cast<double*>(vd+ i);
cd[i] = t1;
vd[i] = t2;
cout << "cd[" << i << "] = " << cd[i] << endl;
cout << "vd[" << i << "] = " << vd[i] << endl;
}
}
Additionally, be aware of the problems that could be caused when you cast
away the constness of a const object.
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.


Jul 19 '05 #2

P: n/a

"drowned" <vo**********@hotmail.com> wrote in message
news:6d**************************@posting.google.c om...
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[i]);
double t2 = const_cast<double>(vd[i]);
cd[i] = t1;
vd[i] = t2;
cout << "cd[" << i << "] = " << cd[i] << endl;
cout << "vd[" << i << "] = " << vd[i] << 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[i] = 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))[i] = 1.0;

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

john
Jul 19 '05 #3

P: n/a

"Josephine Schafer" <js*@usa.net> wrote in message
news:bg************@ID-192448.news.uni-berlin.de...

"drowned" <vo**********@hotmail.com> wrote in message
news:6d**************************@posting.google.c om...
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[i]);

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

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


cd[i] = t1;
vd[i] = t2;

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


Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.