Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > asignment to the const member (g++ vs VC)

Reply
Thread Tools

asignment to the const member (g++ vs VC)

 
 
viki
Guest
Posts: n/a
 
      04-17-2008
How exactly do I assign to the member declared as const, portably ?
Casting the lhs compiles in VC but not in g++.

Thanks
V.M.
(This is c++ technical question without "design" dimension ).
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-17-2008
viki wrote:
> How exactly do I assign to the member declared as const, portably ?


In an initialiser list.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      04-17-2008
viki wrote:
> How exactly do I assign to the member declared as const, portably ?
> Casting the lhs compiles in VC but not in g++.
>
> Thanks
> V.M.
> (This is c++ technical question without "design" dimension ).


You can't portably. If it is a constant variable, it can only be
initialized. Changing it after initialization by any means is undefined
behavior. Just because VC may allow it doesn't mean that it's right or that
it'll always work, even in VC.

If you want to change a const member, then don't make it const.

--
Jim Langston



 
Reply With Quote
 
gpderetta
Guest
Posts: n/a
 
      04-17-2008
On Apr 17, 12:41 pm, viki <viki...@gmail.com> wrote:
> How exactly do I assign to the member declared as const, portably ?


You don't. AFAIK, casting away constness is UB (except for references
to const that actually point to non const objects).

> Casting the lhs compiles in VC but not in g++.


struct a {
const int i;
};

int main() {
a x = { 0 };
const_cast<int&>(x.i) = 10;
}

This compiles with g++-4.1.2. But I think it is still UB.

--
gpd


>


 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      04-17-2008
viki wrote:
> How exactly do I assign to the member declared as const


You don't. consts cannot be assigned to. That's exactly what you are
saying when you are writing the "const" keyword.

If you want to assign to it, don't make it const.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
asignment in class Raptor C++ 3 03-20-2005 06:20 PM
asignment in class Raptor C++ 1 03-03-2005 05:25 PM
error: incompatible types in asignment Dennis Schulz C Programming 2 05-26-2004 08:27 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57