www.digitalmars.com         C & C++   DMDScript  

c++ - Accessibility of a static constant within a class

reply Edward A. Waugh <edward_waugh hotmail.com> writes:
Consider the following code paying attention to the
comments:

class foo {

public:

    static const int TEXT_SIZE = 255; // TEXT_SIZE is
accessible within eggs.

    void bar() {
        size = TEXT_SIZE;
    }

private:

    // static const int TEXT_SIZE = 255; is not accessible
within struct eggs.

    struct eggs {
        // char buffer2[foo::TEXT_SIZE]; does not work.
        char buffer2[TEXT_SIZE];
    };

    char buffer[TEXT_SIZE]; // Public or private TEXT_SIZE
are both accessible.
    int size;

};

Why is TEXT_SIZE not accessible within the struct eggs
declaration if TEXT_SIZE is declared to be private?

The declaration of the buffer array and the bar function
have no problem with TEXT_SIZE being private but eggs can
only access TEXT_SIZE if its declared to be public.

 - Edward
Oct 24 2006
parent "Matthew" <matthew hat.stlsoft.dot.org> writes:
"Edward A. Waugh" <edward_waugh hotmail.com> wrote in message
news:ehlo0k$1vfl$1 digitaldaemon.com...
 Consider the following code paying attention to the
 comments:

 class foo {

 public:

     static const int TEXT_SIZE = 255; // TEXT_SIZE is
 accessible within eggs.

     void bar() {
         size = TEXT_SIZE;
     }

 private:

     // static const int TEXT_SIZE = 255; is not accessible
 within struct eggs.

     struct eggs {
         // char buffer2[foo::TEXT_SIZE]; does not work.
         char buffer2[TEXT_SIZE];
     };

     char buffer[TEXT_SIZE]; // Public or private TEXT_SIZE
 are both accessible.
     int size;

 };

 Why is TEXT_SIZE not accessible within the struct eggs
 declaration if TEXT_SIZE is declared to be private?
Because TEXT_SIZE is private. Just because foo::eggs is defined as a member type of foo does not mean it has access to the non-public members of foo.
 The declaration of the buffer array and the bar function
 have no problem with TEXT_SIZE being private but eggs can
 only access TEXT_SIZE if its declared to be public.
Oct 24 2006