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


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

Declaring a static const member of a class.

From Luca Cerone <luca.cerone@gmail.com>
Newsgroups comp.lang.c++
Subject Declaring a static const member of a class.
Date 2012-03-02 08:26 -0800
Organization http://groups.google.com
Message-ID <2939750.182.1330705614328.JavaMail.geo-discussion-forums@vbva11> (permalink)

Show all headers | View raw


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




Back to comp.lang.c++ | Previous | Next — 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