Kingkong Jnr
Kingkong Jnr

Reputation: 1158

Where should I declare my private class level static constant in the header file?

Where should I specify my class level static constant in the header file?

class ABC
{
public:
    enum A1
    {
    };

    //Manager functions
    ABC(int x )
    {
        m_x = x;
    };
    viirtual ~ABC();

protected:
    int data;

private:
    //Typedef
    typedef std::pair(int, int) INT_PAIR;
    typedef std::pair(int, int) INT_PAIR1;
    ...
    //functions
    void increment_x();
    //Member data
    int m_x;
    ... whole lot of other data
}

Where should I declare a private static const variable like version number inside this class declaration (ABC.h)?

static const std::string version;

Where exactly would it fit in? It is not really member data, since it is static. (not per object)

Edit - 1 :

Is there a specific precedence for these variables? Do they go at the start (right after the first opening curly brace after class ABC? Or right after the private keyword in my snippet? (OR) is it after the typedefs?

Of course I'll mention in my abc.cpp file that const std::string version = "10";

Edit 2: I was expecting answers like what Lucas mentions.(please provide valid reasoning)

Where, inside a class declaration like the one I mentioned below, should the static variables be placed?

Please do not provide answers that mention that the decl needs to be in .h file and definition in .cpp file. – I know that already.

Upvotes: 0

Views: 1988

Answers (1)

Grzegorz
Grzegorz

Reputation: 3335

A.h file (header:)

class A {
...
private:
    static const std::string version ;
} ;

A.cpp file (body, remember about #include "A.h" :)

const std::string A::version = "10" ;

Upvotes: 5

Related Questions