Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: plz xplain behavior of const_cast

Reply
Thread Tools

Re: plz xplain behavior of const_cast

 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-05-2012
On 05.01.2012 08:01, Heck wrote:
> const int NUM_UNIVS = -1;


Don't use all uppercase for constants. That's a Java convention. Java
does not have a preprocessor. C++ does have a preprocessor. In C++, all
uppercase is conventionally used for and reserved for macros names,
primarily to reduce the probability of name collisions.

>
> {
> // some block of code, and at the end of the block,
> // I now have the value I need for NUM_UNIVS,
> // so I do the following to assign it the new value
>
> int * tmp;
> *(tmp = const_cast<int*> (& NUM_UNIVS)) = new_value;


To cast away constness of an object, just cast to a non-const reference;
there's no need for the pointer stuff.

However, if you cast away constness of and then attempt to modify an
/originally const/ object, then you have Undefined Behavior.

That's because you have told the compiler that it's originally const and
will never be changed, so the compiler is free to substitute the
(presumed) known value anywhere the object is used.

Short advice: don't cast.


Cheers & hth.,

- Alf
 
Reply With Quote
 
 
 
 
gwowen
Guest
Posts: n/a
 
      01-05-2012
On Jan 5, 7:57*am, "Alf P. Steinbach" <alf.p.steinbach
(E-Mail Removed)> wrote:
> On 05.01.2012 08:01, Heck wrote:
>
> > * const int * *NUM_UNIVS = *-1;

>
> Don't use all uppercase for constants. That's a Java convention.


Not really. It's a logical extension of the C rules/conventions that
say:
(i) Macros are conventially capitals.
(ii) In C, const variables of integer type cannot be used as compile
time constants, and, prior to C99's VLAs, if you want non-dynamic
array, you need a compile time constants.

So integer constants tend to #define'd, instead of just defined.

So compile-time constants tend to be capitals. Then that convention
tends to leak across to C++ due to the similarity of the languages and
the identical use cases.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-05-2012
On 05.01.2012 13:00, gwowen wrote:
> On Jan 5, 7:57 am, "Alf P. Steinbach"<alf.p.steinbach
> (E-Mail Removed)> wrote:
>> On 05.01.2012 08:01, Heck wrote:
>>
>>> const int NUM_UNIVS = -1;

>>
>> Don't use all uppercase for constants. That's a Java convention.

>
> Not really. It's a logical extension of the C rules/conventions that
> say:
> (i) Macros are conventially capitals.
> (ii) In C, const variables of integer type cannot be used as compile
> time constants, and, prior to C99's VLAs, if you want non-dynamic
> array, you need a compile time constants.


That's just bad programmers.

C has had 'enum' since C89.

'enum' works nicely for expressing whatever integer real constants.


> So integer constants tend to #define'd, instead of just defined.


It's true that the convention probably originated with C, although the
early C practitioners used all kinds of weird conventions, including
none. They were programming in-the-small. So it didn't matter so much.

However, as you write, in C and C++ all uppercase is a convention for
macros.

Arguing that it is a convention for constants just because constants are
sometimes expressed as macros, is just like arguing that it is for
functions, or whatever that can be expressed as a macro.

The convention does IMO not serve a useful purpose for constants, in
particular in light of "one man's constant is another man's variable".

The convention does serve a useful purpose for macros.


> So compile-time constants tend to be capitals. Then that convention
> tends to leak across to C++ due to the similarity of the languages and
> the identical use cases.


Hm, I don't think there are really many who start out with C and migrate
up. Rather they migrate sideways/down from Java and C#. They don't pick
up all these weird inheritance patterns and singletons and abstract
factories and managers and whatnot in C, I think.

Cheers,

- Alf
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      01-05-2012
On 01/ 6/12 11:17 AM, Alf P. Steinbach wrote:
> On 05.01.2012 13:00, gwowen wrote:
>
>> So compile-time constants tend to be capitals. Then that convention
>> tends to leak across to C++ due to the similarity of the languages and
>> the identical use cases.

>
> Hm, I don't think there are really many who start out with C and migrate
> up. Rather they migrate sideways/down from Java and C#. They don't pick
> up all these weird inheritance patterns and singletons and abstract
> factories and managers and whatnot in C, I think.


I think you would be surprised Alf. Most of the aspirational C++
programmers I work with or coach are embedded C programmers looking to
make the next step.

--
Ian Collins
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      01-06-2012
On Jan 5, 10:17*pm, "Alf P. Steinbach" <alf.p.steinbach
(E-Mail Removed)> wrote:
> On 05.01.2012 13:00, gwowen wrote:
> > On Jan 5, 7:57 am, "Alf P. Steinbach"<alf.p.steinbach
> > (E-Mail Removed)> *wrote:
> >> On 05.01.2012 08:01, Heck wrote:

>
> >>> * *const int * *NUM_UNIVS = *-1;

>
> >> Don't use all uppercase for constants. That's a Java convention.

>
> > Not really. It's a logical extension of the C rules/conventions that
> > say:
> > (i) Macros are conventially capitals.
> > (ii) In C, const variables of integer type cannot be used as compile
> > time constants, and, prior to C99's VLAs, if you want non-dynamic
> > array, you need a compile time constants.

>
> That's just bad programmers.
>
> C has had 'enum' since C89.
>
> 'enum' works nicely for expressing whatever integer real constants.
>
> > So integer constants tend to #define'd, instead of just defined.

>
> It's true that the convention probably originated with C, although the
> early C practitioners used all kinds of weird conventions, including
> none. They were programming in-the-small. So it didn't matter so much.


small. Like Unix...

> However, as you write, in C and C++ all uppercase is a convention for
> macros.
>
> Arguing that it is a convention for constants just because constants are
> sometimes expressed as macros, is just like arguing that it is for
> functions, or whatever that can be expressed as a macro.
>
> The convention does IMO not serve a useful purpose for constants, in
> particular in light of "one man's constant is another man's variable".
>
> The convention does serve a useful purpose for macros.
>
> > So compile-time constants tend to be capitals. Then that convention
> > tends to leak across to C++ due to the similarity of the languages and
> > the identical use cases.

>
> Hm, I don't think there are really many who start out with C and migrate
> up.


seriously? I'd have thought there were loads. I'm one. Weren't all the
initial C++ programmers originally C programmers?

> Rather they migrate sideways/down from Java and C#. They don't pick
> up all these weird inheritance patterns and singletons and abstract
> factories and managers and whatnot in C, I think.


don't singletons and abstract factory come from GoF?
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      01-06-2012
gwowen <(E-Mail Removed)> wrote:
> Not really. It's a logical extension of the C rules/conventions that
> say:
> (i) Macros are conventially capitals.
> (ii) In C, const variables of integer type cannot be used as compile
> time constants, and, prior to C99's VLAs, if you want non-dynamic
> array, you need a compile time constants.
>
> So integer constants tend to #define'd, instead of just defined.
>
> So compile-time constants tend to be capitals. Then that convention
> tends to leak across to C++ due to the similarity of the languages and
> the identical use cases.


There's a difference between a compile-time constant in C++ and a
#define macro. For one, you can take the address of the former (ie.
create a pointer that points to the const) but not the latter.
 
Reply With Quote
 
gwowen
Guest
Posts: n/a
 
      01-06-2012
On Jan 6, 8:46*am, Nick Keighley <(E-Mail Removed)>
wrote:

> > Hm, I don't think there are really many who start out with C and migrate
> > up.

>
> seriously? I'd have thought there were loads. I'm one.


Me too

> Weren't all the initial C++ programmers originally C programmers?


Even if its true of the latest crop of programmers, its probably not
true of their teachers, or their teachers's teachers, and the people
who wrote their textbooks. Its easy to pick habits up from that.
Besides, K&R2 uses #define where Alf says "only bad programmers" do
that.
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      01-07-2012
On Thu, 2012-01-05, Alf P. Steinbach wrote:
....
> Hm, I don't think there are really many who start out with C and migrate
> up. Rather they migrate sideways/down from Java and C#. They don't pick
> up all these weird inheritance patterns and singletons and abstract
> factories and managers and whatnot in C, I think.


I happen to dislike much of that stuff too, but I think it's unfair to
describe it as a virus from the Java world. When people come to their
first language which supports object-oriented programming, they will
want to learn about that OOP stuff, and they will find those things
there.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      01-07-2012
Jorgen Grahn於 2012年1月7日星期*UTC+8下午2時38分32秒 道:
> On Thu, 2012-01-05, Alf P. Steinbach wrote:
> ...
> > Hm, I don't think there are really many who start out with C and migrate
> > up. Rather they migrate sideways/down from Java and C#. They don't pick
> > up all these weird inheritance patterns and singletons and abstract
> > factories and managers and whatnot in C, I think.

>
> I happen to dislike much of that stuff too, but I think it's unfair to
> describe it as a virus from the Java world. When people come to their
> first language which supports object-oriented programming, they will
> want to learn about that OOP stuff, and they will find those things
> there.
>
> /Jorgen


The object programming in C++ can be summarized as follows:

1. class inheritance and object properties set up
by the compiler for all static parts and a bunch of static functors with
*this as the hidden parameter

2. one vtable in a class and one vpointer in an object to be used to
invoke the right function pointer as a virtual function at the run-time

3. the operator reloading part similar to smalltalk only in syntax but not very helpful in developing cad tools or code generators





> --
> // Jorgen Grahn <grahn@ Oo o. . .
> \X/ snipabacken.se> O o .


 
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_cast as undefined behavior? Nephi Immortal C++ 35 01-19-2013 04:29 PM
can any 1 xplain this rincyrose C++ 0 10-26-2008 04:58 PM
Size of my Struct? PLZ PLZ reply brian C Programming 7 11-25-2004 01:30 AM
Re: plz help!!! plz plz plz plzplzplz help the noob alkzy Microsoft Certification 0 10-31-2004 10:04 PM
Computer Problems... Plz Plz Plz HELP ME..... Nick Computer Support 0 06-04-2004 08:50 PM



Advertisments