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


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

Re: Declaring a static const member of a class.

Date 2012-03-03 13:04 -0500
From Ed Anson <EdAnson@comcast.net>
Newsgroups comp.lang.c++
Subject Re: Declaring a static const member of a class.
References <2939750.182.1330705614328.JavaMail.geo-discussion-forums@vbva11> <WM2dnWIIfKoewczSnZ2dnUVZ_o-dnZ2d@giganews.com> <22573415.56.1330778113342.JavaMail.geo-discussion-forums@vbas10>
Message-ID <fYudnbPyBozbwM_SnZ2dnUVZ_sadnZ2d@giganews.com> (permalink)

Show all headers | View raw


On 3/3/12 7:35 AM, Luca Cerone wrote:
> 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.

That is correct. A const value cannot be changed after it is 
initialized. However, it is initialized at its point of definition. 
Consequently, a static const is usually initialized before the program 
starts running.

I should have elaborated on problem 3: A static const requires a 
definition with an initial value, which must necessarily be evaluated 
before the program starts running. Using a file (as you want) to 
initialize it is not an option. Such behavior is decidedly not const.

>
> 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?

Static const is used when the value is shared by all instances (objects) 
of a class, and has a value defined before the program starts running.

>
>> 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?

The const_cast operator is specifically provided for casting away the 
const of a value. As another person has correctly mentioned, it is not 
always safe to use it on a const object; it is generally used in other 
contexts where the object has acquired const status. But it still works 
in most implementations. Its syntax and use are described in any good 
book on C++.

But (after a bit more reflection) I think you don't really need const in 
your application. Making it private should be enough, and more 
accurately reflects your intent to set its value after execution begins. 
Because it's private, your class can determine when (and how many times) 
its value gets set. A static method designed for the purpose is a common 
way to handle that.

>
>> 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?

If x is private to your class, it controls that. Any use outside of the 
class would require an accessor, such as:

private: static vector<int> x;
public:  static const vector<int>& GetVector() { return x; }

> 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?

You can use a constrtuctor to parameterize instances of a class 
(objects). But you want all objects of the class to share the same data. 
That calls for an initialization function to set the shared data.

>
> 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 :)

Don't feel bad. I've been programming with C++ for 23 years and I'm 
still learning.

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