473,378 Members | 1,218 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

const_cast question

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
3 5369

"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

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kaspar Minosiants | last post by:
Hi , there is a question about work of const_cast The question is why the object is still reachable after a delete operation class A { int var; public: A():var(0){} ~A(){}
4
by: S.Senthilvel | last post by:
hi, I am a little confused about the const_cast.I've thought it this way till now. //Proper cast int i = 10; const int* pci = &i; int *pi = const_cast<int*>(pci);
2
by: Tobias Kilian | last post by:
Hi Ng ! I wrote a SmartPointer class which holds a doubled linked list to all its copies to ensure that all copies are holding the same pointer, even if one copy changes later. If the last...
18
by: johny smith | last post by:
Can someone please give me some good reference site/information for understanding when and why I would use 1.) const_cast Don't know why you would do this? 2.) reinterpret_cast. I have used...
20
by: CoolPint | last post by:
While I was reading about const_cast, I got curious and wanted to know if I could modify a constant variable through a pointer which has been "const_cast"ed. Since the pointer would be pointing to...
6
by: Simon Bailey | last post by:
In the following code at the end of the program z = 20 & y = 99. void doit(const int* x) { int* nonconst; nonconst = const_cast<int*>(x); *nonconst = 99; } int main(int argc, char* argv)
6
by: Alexander Stippler | last post by:
Hello, I wonder if and in what cases casting const away by a const_cast can be dangerous. In one place the only way I can imagine to realize a pretty nice feature is casting away const. Now I...
11
by: Squeamizh | last post by:
class my_class { public: my_class() : value(0) { } int& get_value() { return value; } const int& get_value() const { my_class& c = const_cast<my_class&>(*this); return c.get_value(); }
4
by: subramanian100in | last post by:
when are the use of static_cast and const_cast essential ? Stroustrup, in his book TC++PL(3rd edition) in page number 139 (in Section 6.5 Advice - ), has advised "Avoid explicit type...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.