Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Forward references in class definition
Forum Updated to NodeBB v4.3 + New Features

Forward references in class definition

Scheduled Pinned Locked Moved Solved C++ Gurus
9 Posts 5 Posters 784 Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JonB
    wrote on 2 Mar 2024, 12:27 last edited by
    #1

    I always thought/assumed you could not do this:

    class Foo
    {
    public:
        int getter() const { return _thing; }
    
    private:
        int _thing;
    };
    

    I do mean in specifically that order. I thought when it met the early return _thing; compiler would complain "_thing not defined".

    I now discover you can do that, which surprised me. So this means compiler is doing two passes over this (e.g. in .h file), first just for declarations and then second for code generation.

    Now that I think about it, of course this applies too if the first method were calling another method defined later in the class, and that would not surprise me, else you'd have to be careful about the order of declarations in class/.h file. I guess I just had a mental block on variables.

    Don't know what anyone else would care to comment on this as I guess there is not much of a question here! I sort of thought code was generated as it did single pass though class declaration if that had code blocks in it, but clearly not?

    J 1 Reply Last reply 2 Mar 2024, 19:46
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 2 Mar 2024, 19:30 last edited by
      #2

      Hi,

      My memory is a bit rusty about that topic but IIRC, in the class/struct case, the compiler "waits" to have the whole declaration before it starts name resolution.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • J JonB
        2 Mar 2024, 12:27

        I always thought/assumed you could not do this:

        class Foo
        {
        public:
            int getter() const { return _thing; }
        
        private:
            int _thing;
        };
        

        I do mean in specifically that order. I thought when it met the early return _thing; compiler would complain "_thing not defined".

        I now discover you can do that, which surprised me. So this means compiler is doing two passes over this (e.g. in .h file), first just for declarations and then second for code generation.

        Now that I think about it, of course this applies too if the first method were calling another method defined later in the class, and that would not surprise me, else you'd have to be careful about the order of declarations in class/.h file. I guess I just had a mental block on variables.

        Don't know what anyone else would care to comment on this as I guess there is not much of a question here! I sort of thought code was generated as it did single pass though class declaration if that had code blocks in it, but clearly not?

        J Offline
        J Offline
        JoeCFD
        wrote on 2 Mar 2024, 19:46 last edited by
        #3

        @JonB
        Now you may know why this works

        class Foo
        {
        public:
            Foo() : _thing{5} {}/* setting here */
            int getter() const { return _thing; }
        
        private:
            const int _thing;
        };
        

        this does not work

        class Foo
        {
        public:
            Foo()
            { _thing = 6;} /* _thing can not be changed anymore */ 
            int getter() const { return _thing; }
        
        private:
            const int _thing;
        };
        

        all member variables are declared first before the constructor func is called. The order of variables do matter if the first one uses the second one.

        S 1 Reply Last reply 2 Mar 2024, 19:49
        0
        • J JoeCFD
          2 Mar 2024, 19:46

          @JonB
          Now you may know why this works

          class Foo
          {
          public:
              Foo() : _thing{5} {}/* setting here */
              int getter() const { return _thing; }
          
          private:
              const int _thing;
          };
          

          this does not work

          class Foo
          {
          public:
              Foo()
              { _thing = 6;} /* _thing can not be changed anymore */ 
              int getter() const { return _thing; }
          
          private:
              const int _thing;
          };
          

          all member variables are declared first before the constructor func is called. The order of variables do matter if the first one uses the second one.

          S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 2 Mar 2024, 19:49 last edited by
          #4

          @JoeCFD Initialization VS assignment. In the second example you are trying to assign a value to a const variable, that's why it fails.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • M Offline
            M Offline
            mpergand
            wrote on 3 Mar 2024, 00:21 last edited by
            #5

            As @Christian-Ehrlicher might say : Basic C++ stuff
            :)

            1 Reply Last reply
            0
            • J JonB has marked this topic as solved on 3 Mar 2024, 08:41
            • S Offline
              S Offline
              SimonSchroeder
              wrote on 4 Mar 2024, 07:56 last edited by
              #6

              @JonB said in Forward references in class definition:

              I always thought/assumed you could not do this:

              Bjarne Stroustrup had some discussions with colleges. His first intuition was to actually not allow this. However, in the end the following idea won: It should not matter if you are defining a member function inside a class or outside of it. The visibility of member variables for member functions written inside the class should be the same as if defined outside. This allows to freely move member functions inside the class or outside. We are quite lucky that this was the final decision!

              J 1 Reply Last reply 4 Mar 2024, 08:20
              1
              • S SimonSchroeder
                4 Mar 2024, 07:56

                @JonB said in Forward references in class definition:

                I always thought/assumed you could not do this:

                Bjarne Stroustrup had some discussions with colleges. His first intuition was to actually not allow this. However, in the end the following idea won: It should not matter if you are defining a member function inside a class or outside of it. The visibility of member variables for member functions written inside the class should be the same as if defined outside. This allows to freely move member functions inside the class or outside. We are quite lucky that this was the final decision!

                J Offline
                J Offline
                JonB
                wrote on 4 Mar 2024, 08:20 last edited by
                #7

                @SimonSchroeder said in Forward references in class definition:

                Bjarne Stroustrup

                This guy had time to design C++ at the same time as doing Abba? Amazing.

                S 1 Reply Last reply 5 Mar 2024, 14:15
                0
                • J JonB
                  4 Mar 2024, 08:20

                  @SimonSchroeder said in Forward references in class definition:

                  Bjarne Stroustrup

                  This guy had time to design C++ at the same time as doing Abba? Amazing.

                  S Offline
                  S Offline
                  SimonSchroeder
                  wrote on 5 Mar 2024, 14:15 last edited by
                  #8

                  @JonB said in Forward references in class definition:

                  Abba

                  That one is Björn Ulvaeus (he is Swedish, whereas Bjarne is Danish).

                  J 1 Reply Last reply 5 Mar 2024, 14:31
                  0
                  • S SimonSchroeder
                    5 Mar 2024, 14:15

                    @JonB said in Forward references in class definition:

                    Abba

                    That one is Björn Ulvaeus (he is Swedish, whereas Bjarne is Danish).

                    J Offline
                    J Offline
                    JonB
                    wrote on 5 Mar 2024, 14:31 last edited by
                    #9

                    @SimonSchroeder
                    Details! And as far as I know Denmark & Sweden are more or less the same country anyway ;-)

                    1 Reply Last reply
                    0

                    1/9

                    2 Mar 2024, 12:27

                    • Login

                    • Login or register to search.
                    1 out of 9
                    • First post
                      1/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved