learning fellow
learning fellow

Reputation: 97

Access static const in another class.

A static const variable declared and defined in a class. How to access it in the private access of another class in same project. Is it possible?

//in some header file
Class A{
    public:
    //some data

    private:
        static const uint8_t AVar =1;
        //other data
};


//in some another header file
Class B{
    static const Bvar; 
};
//here inside Class B it possible to give Bvar  = AVar ? If yes, How ?

Upvotes: 0

Views: 2329

Answers (1)

ComicSansMS
ComicSansMS

Reputation: 54589

A clean way to avoid duplication of the magic value without weakening encapsulation of either class is to move the magic value to a different place that is publicly accessible to both classes.

For example:

namespace detail {
    enum MAGIC_NUMBER_T {
        MAGIC_NUMBER = 1
    };
}

class A{
  private:
  static const uint8_t AVar = detail::MAGIC_NUMBER;
};

class B{
     static const uint8_t BVar = detail::MAGIC_NUMBER;
};

Upvotes: 2

Related Questions