|
 |
|
 |
03-01-2006, 01:55 AM
|
#1 (permalink)
|
|
Guest
|
static_cast<int>(b)=a+1 problem
hello all group members
sir i have pick the tutorial for some previous months from this bullet in board site
but their is some problem
<code>
#include<iostream.h>
int main(int argc,char *argv[])
{
int a=15;
const int b=a;
static_cast<int>(b)=a+1; //here is showing the problem like lvalue
// something like this
cout<<b<<endl;
return 0;
}
</code>
please solve this problem
from
deepak
|
|
|
|
03-01-2006, 08:42 AM
|
#2 (permalink)
|
|
Anti-Zealot
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 72
|
static_cast<TYPE>(VAR), const_cast<>(), etc all return rvalues. That code shouldn't work.
Oddly, though, Visual Studio 2005 doesn't seem to care but g++ 3.4.2 sure does. Visual Studio (seems to) just removes the "const" qualifier from b.
In C++, you aren't supposed to be able to edit a const anyways. That's a C-ism (and it's always great to muck with your const variables).
Now, there are other errors in that code:
- it should be #include <iostream>
- you need to use "std::cout" and "std::endl" or add "using namespace std;". Personally, I prefer the former.
__________________
If you always think like an expert, you'll always be a beginner. | "A handful of knowledgeable people is more effective than an army of fools" -Writing Secure Code, 2nd Ed.
|
|
|
03-02-2006, 12:08 AM
|
#3 (permalink)
|
|
Guest
|
Quote:
|
Originally Posted by AssKoala
static_cast<TYPE>(VAR), const_cast<>(), etc all return rvalues. That code shouldn't work.
Oddly, though, Visual Studio 2005 doesn't seem to care but g++ 3.4.2 sure does. Visual Studio (seems to) just removes the "const" qualifier from b.
In C++, you aren't supposed to be able to edit a const anyways. That's a C-ism (and it's always great to muck with your const variables).
Now, there are other errors in that code:
- it should be #include <iostream>
- you need to use "std::cout" and "std::endl" or add "using namespace std;". Personally, I prefer the former.
|
================================================== ======
u said i have to use using namespace std
but i have used it already
the main problem of static cast
plz send me the code which use static cast
|
|
|
|
03-02-2006, 12:32 AM
|
#4 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
|
He's sayng don't try to assign to a variable declared const.
__________________
Stop intellectual property from infringing on me
|
|
|
03-02-2006, 08:30 AM
|
#5 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
A static_cast is not to cast away constants. The code you presented does not meet the ISO C++ standards.
You'll need the const_cast keyword. This keyword is the only keyword allowed to cast away constness, explicitly stated by the ISO commitee.
Here is the code you're looking for:
Code:
#include <iostream>
using namespace std;
int main()
{
int a = 15;
const int b = a;
const_cast<int&> (b)=a+1; //OK
cout<<b<<endl; //Output is 16.
cout<<a<<endl; //Output is 15.
return 0;
}
Quote:
|
In C++, you aren't supposed to be able to edit a const anyways.
|
The ISO requirements lets you cast away constness. One has to be carefull though, but that's a different topic.
In general, this is a (any) cast:
"I know what I am doing so Mr. Compiler, please be quiet."
__________________
|
|
|
03-02-2006, 11:43 AM
|
#6 (permalink)
|
|
Registered User
Join Date: Apr 2003
Posts: 34
|
Quote:
|
Originally Posted by Valmont
The ISO requirements lets you cast away constness. One has to be carefull though, but that's a different topic.
|
That's an understatement. If you're using const_cast, you should know VERY well what you're doing, and why you're doing it. Const variables are usually const for a reason, and you can break all sorts of neat things by working around const-ness.
And I'd say it's on topic. Sometimes the proper answer to "how do I do X?" is "unless you *really* know what you're doing, don't." And this is one of those cases.
Quote:
|
Originally Posted by Valmont
In general, this is a (any) cast:
"I know what I am doing so Mr. Compiler, please be quiet."
|
Well, except for a dynamic_cast, which is more like "Hey, is it okay if I do this?" And that *is* off-topic 
|
|
|
03-02-2006, 02:57 PM
|
#7 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Nevertheless, the OP asked how to cast away a const. So post the code if you'd like to help within the ISO specifications.
__________________
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 03:30 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|