const_cast question

Discussion in 'C++' started by drowned, Aug 4, 2003.

  1. drowned

    drowned Guest

    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.
    drowned, Aug 4, 2003
    #1
    1. Advertising

  2. "drowned" <> wrote in message
    news:...
    > 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<double*>(cd+ i);

    > double t2 = const_cast<double>(vd);

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

    > 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.

    > 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.
    Josephine Schafer, Aug 4, 2003
    #2
    1. Advertising

  3. "drowned" <> wrote in message
    news:...
    > 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
    John Harrison, Aug 4, 2003
    #3
  4. "Josephine Schafer" <> wrote in message
    news:bgl6cj$oqggo$-berlin.de...
    >
    > "drowned" <> wrote in message
    > news:...
    > > 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<double*>(cd+ i);
    >
    > > 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.
    Josephine Schafer, Aug 4, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kaspar Minosiants

    [help] const_cast

    Kaspar Minosiants, Jul 21, 2003, in forum: C++
    Replies:
    2
    Views:
    411
    John Harrison
    Jul 21, 2003
  2. R. Anbeeswaran

    const_cast<>

    R. Anbeeswaran, Nov 13, 2003, in forum: C++
    Replies:
    7
    Views:
    585
    Ekkehard Morgenstern
    Nov 14, 2003
  3. Gajanan Bhat

    const_cast question

    Gajanan Bhat, May 4, 2004, in forum: C++
    Replies:
    9
    Views:
    530
    Bill Seurer
    May 5, 2004
  4. , India
    Replies:
    4
    Views:
    512
    red floyd
    Nov 14, 2007
  5. Replies:
    4
    Views:
    422
    Alf P. Steinbach
    Feb 1, 2008
Loading...

Share This Page