call static method when class initialized

Discussion in 'C++' started by Ge Cong, Sep 14, 2005.

  1. Ge  Cong

    Ge Cong Guest

    I have a class

    class MyClass
    {
    private:
    static double a;
    static void computeA(){
    a =....;
    }

    public:
    static double getA();
    }



    User uses my code like:

    double x = MyClass::getA();

    I want the method computeA() be called the first time MyClass is used,
    just like the "static" in java. How can I do it?

    Thanks,
     
    Ge Cong, Sep 14, 2005
    #1
    1. Advertising

  2. Ge  Cong

    mlimber Guest

    Ge Cong wrote:
    > I have a class
    >
    > class MyClass
    > {
    > private:
    > static double a;
    > static void computeA(){
    > a =....;
    > }
    >
    > public:
    > static double getA();
    > }
    >
    >
    >
    > User uses my code like:
    >
    > double x = MyClass::getA();
    >
    > I want the method computeA() be called the first time MyClass is used,
    > just like the "static" in java. How can I do it?


    I don't think there's a perfect equivalent. You can either initialize
    the static data when you declare it:

    double MyClass::a = MyClass::doComputeA();

    or you can call the compute method in getA():

    double MyClass::getA()
    {
    static bool init = false;
    if( !init )
    {
    computeA();
    init = true;
    }
    return a;
    }

    Cheers! --M
     
    mlimber, Sep 14, 2005
    #2
    1. Advertising

  3. * Ge Cong:
    > I have a class
    >
    > class MyClass
    > {
    > private:
    > static double a;
    > static void computeA(){
    > a =....;
    > }
    >
    > public:
    > static double getA();
    > }


    Semicolon.

    Missing definition of 'a' (it's declared in the class, but you also need
    a definition outside the class),

    double MyClass::a = something();


    > User uses my code like:
    >
    > double x = MyClass::getA();
    >
    > I want the method computeA() be called the first time MyClass is used,
    > just like the "static" in java.


    Generally it's not a good idea to want things to be like Java.

    What you need technically is just that 'a' is initialized before it's used.

    Btw., in my humble opinion novices should not be allowed to use 'static' in
    Java -- it is a sure way to create a hopeless spaghetti system that is next
    to impossible to test, extend, debug, ...


    > How can I do it?


    Low-level approach: initialize 'a'. ;-)

    Shown above.

    Higher-level approach: redesign as a singleton.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Sep 14, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Thomas_Gagn=E9?=
    Replies:
    9
    Views:
    11,295
    Jezuch
    Jul 3, 2003
  2. utab
    Replies:
    5
    Views:
    600
    Thomas Tutone
    May 16, 2006
  3. Pawel_Iks
    Replies:
    3
    Views:
    556
    terminator
    Jul 31, 2007
  4. , India
    Replies:
    5
    Views:
    1,807
    chisholm
    Feb 15, 2011
  5. Immortal Nephi
    Replies:
    15
    Views:
    6,587
    Bo Persson
    Jul 23, 2010
Loading...

Share This Page