Name: Anonymous 2007-09-09 14:14 ID:cr2lSJc3
#include <iostream>
using namespace std;
int main(int argc, char *argv[]){
const double c_arr[5] = { 1.0 , 10.0 , 1.0, 10.0 , 1.0 };
volatile double v_arr[5] = { 1.0 , 10.0 , 1.0, 10.0 , 1.0 };
double *var;
for(int i=0; i<5;i++){
var = const_cast<double*>(&c_arr[i]);
*var=i*10;
cout << "casted from const: [" << i << "] " << c_arr[i] << endl;
}
for(int i=0; i<5 ; i++ ){
var = const_cast<double*>(&v_arr[i]);
*var=i*10;
cout << "casted from volatile: [" << i << "] " << v_arr[i] << endl;
}
return 0;
}
/*
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.
*/
The task in comments is from the book "Thinking in C++" 3rd chapter 27th task.
I mean I know that I shouldn't be able to change a const, but I can't let it be uninitialized or that will give me an error (at least the const). So please 4chan explain to me how I am supposed to solve this task?
btw. I am on Linux if that makes a difference which it shouldn't for such a basic Program.
using namespace std;
int main(int argc, char *argv[]){
const double c_arr[5] = { 1.0 , 10.0 , 1.0, 10.0 , 1.0 };
volatile double v_arr[5] = { 1.0 , 10.0 , 1.0, 10.0 , 1.0 };
double *var;
for(int i=0; i<5;i++){
var = const_cast<double*>(&c_arr[i]);
*var=i*10;
cout << "casted from const: [" << i << "] " << c_arr[i] << endl;
}
for(int i=0; i<5 ; i++ ){
var = const_cast<double*>(&v_arr[i]);
*var=i*10;
cout << "casted from volatile: [" << i << "] " << v_arr[i] << endl;
}
return 0;
}
/*
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.
*/
The task in comments is from the book "Thinking in C++" 3rd chapter 27th task.
I mean I know that I shouldn't be able to change a const, but I can't let it be uninitialized or that will give me an error (at least the const). So please 4chan explain to me how I am supposed to solve this task?
btw. I am on Linux if that makes a difference which it shouldn't for such a basic Program.