Thread: Order of access labels in class definition

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    Order of access labels in class definition

    I realized that when trying to define public access typedefs and enumerations inside a class definition, I need to place them first, if I want my private members to access them. So invariably all classes that define them need to start their definition with a public access label.

    Is this implementation specific, or do classes definitions follow the same rules for variable lifetime? Until now I thought that the placement of access labels was purely a matter of taste and any class definition was read by the compiler as a block, not sequentially.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Because you have to finish the declarations before you can use them. It's like trying to use a typedef in the struct defined in it.
    Code:
    typedef struct foo {
       FOO* next;
       int data;
    } FOO;
    or even more simply
    Code:
    int main() {
       foo();
    }
    
    void foo() {
       cout << "Foo";
    }
    If I understood the question correctly that is. You're asking why this doesn't work, correct?
    Code:
    class person {
        name firstName;
        name lastName;
        eyeColor iris;
      public:
        typedef std::string name;
        enum eyeColor { BROWN, GREEN, BLUE, HAZEL };
    };
    If so, then yes it reads it sequentially.
    Last edited by SlyMaelstrom; 06-14-2006 at 07:15 PM.
    Sent from my iPadŽ

  3. #3
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    The access lables can be in any order and appear any number of times. You just need to make sure that you declare something before you use it

    Code:
    class TheCoolestClassEver
    {
      public:  
         //put the typedefs here, so everything below can use them
      
      private: 
        //all your private members
    
      public:  
        //all the other public crap that you didn't want to have declared with the typedefs
    
    };
    edit: I think this is what Sly is saying as well.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Ok. thanks both

    Now... let's imagine I have my header file nicely created...

    Code:
    class myClass {
        public:
            typedef int number;
        private:
            number value;
        /*...*/
        public:
            number function(number, number);
        /*...*/
    };
    When working on the companion cpp file where my member functions will be defined, it seems intuitive to add the typedef again at the top to make things easier to type, correct?

    Code:
    #include "myClass.h"
    
    typedef int number;
    
    number myClass::function(number x, number y) {
        /*...*/
    }
    Without the typedef above, I need to use the scope operator for the type number. However something confused me. I don't need to use it for the parameter types. Why?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    If you're going to define the typedef in the global scope, then you might as well not define it in the class at all. Consider that everything using that class will require linkage with the implementation's object and the typedef will be global in the entire build.

    ...and I think the answer to:
    I don't need to use it for the parameter types. Why?
    is due to the fact that when the function is qualified with a scope, then all argument parameters in that function automatically are qualified with that scope. I may be wrong on that, though.
    Last edited by SlyMaelstrom; 06-15-2006 at 06:21 AM.
    Sent from my iPadŽ

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Hmm... the typedef above is included on myClass.cpp. It is local to that file only.

    Code:
    /* myClass.h */
    class myClass {
        public:
            typedef int number;
        private:
            number value;
        /*...*/
        public:
            number function(number, number);
        /*...*/
    };
    Code:
    /*myClass.cpp */
    #include "myClass.h"
    
    typedef int number;
    
    number myClass::function(number x, number y) {
        /*...*/
    }
    Code:
    /* main.cpp */
    #include "myClass.h"
    
    int main() {
        number test = 0;  //error, undeclared identifier
        myClass::number test = 0; // Ok.
    }
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Hmm... well I suppose it's alright, then. I didn't test it and assumed a little. I thought I was sure.
    Sent from my iPadŽ

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Declare the typedef, the enum, and the class in a namespace and the problem should be solved.

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Yes... I was trying to understand both options, though. With and without a namespace. Thanks all
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You could also just put using myClass::number at the top of your source file if you don't want to type myClass::number in the few places where it isn't already automatic.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Go with Daved's suggestion. The separate typedef is a bad idea simply because you could forget to change it if you ever change the one in the class.
    Sly's answer about the parameters is also correct.

    A thing about classes: their definitions are read sequentially, with one exception: all inline function implementations are treated as if they came directly after the closing brace of the class, i.e. this works:
    Code:
    class A
    {
    public:
      int getMember() { return member; }
    private:
      int member;
    };
    because it is treated like
    Code:
    class A
    {
    public:
      int getMember();
    private:
      int member;
    };
    inline int A::getMember() { return member; }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-26-2008, 08:44 AM
  2. Replies: 25
    Last Post: 10-29-2007, 04:08 PM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  5. Class Definition?
    By Jamina in forum C++ Programming
    Replies: 4
    Last Post: 08-07-2003, 11:12 PM