Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

TICPP Task unsolvable?

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.

Name: Anonymous 2007-09-10 11:41 ID:bIYFc2vh

>>14
Okay, constants have some use in the sense of security, but that's only if you fear your program is subject to code injection, which it shouldn't.

But typecasting is completely useless. What you should have is dynamic evaluation and polymorphism. You simply call functions, e.g. f(x) → x.lol(). When f is actually called, and passed an object, the interpreter/specializer/recompiler/whatever should check if it knows how to lol on that object. If it happens to, it lols, whatever that may be for the given object. This way you avoid shooting your foot by ungeneralizing and wasting time defining types. This also makes typecasting completely useless: even if you could turn an object into another (in Python, you can change an object's class in real time, whatever you'd want to do that for), it would do nothing. Instead of typecasting, just call the fucking function or use the fucking property, and catch exceptions if the object you're passed doesn't have the property you were looking for.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List