Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #14705

Re: Declaring a static const member of a class.

From Luca Cerone <luca.cerone@gmail.com>
Newsgroups comp.lang.c++
Subject Re: Declaring a static const member of a class.
Date 2012-03-03 04:35 -0800
Organization http://groups.google.com
Message-ID <22573415.56.1330778113342.JavaMail.geo-discussion-forums@vbas10> (permalink)
References <2939750.182.1330705614328.JavaMail.geo-discussion-forums@vbva11> <WM2dnWIIfKoewczSnZ2dnUVZ_o-dnZ2d@giganews.com>

Show all headers | View raw


Hi Ed, thanks a lot for your reply!
>On Friday, March 2, 2012 11:49:26 PM UTC, Ed Anson wrote:
> You have three problems here:
> 1. The C++ concept of const does not strictly allow for run-time 
> initialization such as you desire; and
> 2. Initialization in the constructor (which might be different for each 
> instance) is inconsistent with the static definition (which is shared by 
> all instances).
> 3. A static const requires a definition with an initial value.
> 

Ok probably I got everything wrong.
I thought that you used const to avoid overwriting a value that has already been defined; and I thought that static (for class members) is used to share 
the same location of memory among all the objects.

So the idea of declaring something as static const correspond to "initialize
the member once, share it among all the objects and forbid modifications".

As this is wrong, what is "static const" actually used for?

> The third problem is essentially a syntax error. The usual solution for 
> the second problem would be to add a static member function to 
> initialize the vector. And here is a reasonably good solution to the 
> first problem:
> 
> In the function you use to initialize the vector, use const_cast to 
> obtain a non-const pointer to the vector.

I'm sorry I'm not aware of the const_cast type... what is it?

> You can then initialize it as 
> you require. If it is inconvenient to guarantee that the initialization 
> function is called only once, you might instead declare the static 
> member as a pointer to vector, declared with value NULL. If the 
> initialization function sees NULL, it instantiates the vector, 
> initializes it as desired, and then sets the pointer (using const_cast).
> 

ok I think I got it..
so in my class I need a static pointer to a vector x, initially set to null that is assigned by the constructor (if it is not null).
But how can I avoid writing on x again?


> This is a fairly standard work-around when it is necessary to bend the 
> concept of const to something similar to (but different from) the C++ 
> concept.
> 
> /Ed

I actually don't understand something though.
It seems pretty reasonable to me that a class object my depend on certain
parameters input by the user somehow (keyboard, reading a file etc. etc.).

Isn't there a way to "parameterize" a class, so that its members will depend
on certain parameters?

Thanks again for your answer.
I'm sorry if I ask silly question, I'm a 3weeks "experienced" c++ programmer,
and after 10 years of Matlab programming I feel like I've never programmed before :)

Cheers, 
Luca

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Declaring a static const member of a class. Luca Cerone <luca.cerone@gmail.com> - 2012-03-02 08:26 -0800
  Re: Declaring a static const member of a class. Ed Anson <EdAnson@comcast.net> - 2012-03-02 18:49 -0500
    Re: Declaring a static const member of a class. Leigh Johnston <leigh@i42.co.uk> - 2012-03-03 11:02 +0000
    Re: Declaring a static const member of a class. Luca Cerone <luca.cerone@gmail.com> - 2012-03-03 04:35 -0800
      Re: Declaring a static const member of a class. Paavo Helde <myfirstname@osa.pri.ee> - 2012-03-03 07:21 -0600
      Re: Declaring a static const member of a class. Ed Anson <EdAnson@comcast.net> - 2012-03-03 13:04 -0500
  Re: Declaring a static const member of a class. Saeed Amrollahi <amrollahi.saeed@gmail.com> - 2012-03-03 03:29 -0800
  Re: Declaring a static const member of a class. Luca Cerone <luca.cerone@gmail.com> - 2012-03-03 10:33 -0800
    Re: Declaring a static const member of a class. Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2012-03-04 01:40 -0500

csiph-web