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


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

Re: Declaring a static const member of a class.

Date 2012-03-02 18:49 -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>
Message-ID <WM2dnWIIfKoewczSnZ2dnUVZ_o-dnZ2d@giganews.com> (permalink)

Show all headers | View raw


On 3/2/12 11:26 AM, Luca Cerone wrote:
> Dear all,
>
> I'm having a few difficulties in declaring a class with a static constant member.
> This member is a vector of double that has to be created once during
> the execution and shared among all the objects.
> After being created it can't be modified during the whole execution of my program.
>
> I write a toy example to show you what I'd like to do (it doesn't work,
> but I can't figure out a way to code it well, that's why I'm posting here).
>
> #include<iostream>
> #include<vector>
>
> class QuadFun{
>    public:
>      QuadFun(double a, double b) ; // a and b will be used to initialize
>      // the private vector y to evaluate the values (x-a)*(x-b)
>      // x has to be a vector that is shared among all the object of the class.
>    private:
>      static const std::vector<double>  x; // this will be a vector of uniformly
>             //spaced numbers from 0 to 1. The number of points is determined
>             //by reading a configuration file. in this example I set the
>             //number manually in the main function.
>      std::vector<double>  y;
> }
>
> int main() {
>    int numPoints=50; // this value is actually read from a configuration file
>    // here is the problem. I don't want the number of points to be a member
>    // of the class.
>    QuadFun f1=QuadFun(0,1);
>    QuadFun f2=QuadFun(0.5,0.75);
>
>    // how can I make the vector x mad of numpoints created when I first create
>    // an object of class QuadFun?
>    // how can I make it "dynamic" so that if I change the value of numPoints
>    // the x vector will be created accordingly? (in an other execution).
>    return 0 ;
> }
>
> Hope I could explain well what I'd like to achieve.
> I know this code is WRONG, but I can't understand how to write it
> in a good way.
>
> Can you help me?
> Thanks a lot in advance,
> Cheers, Luca


Luca,

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.

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

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

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