Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Some questions about inline function

Some questions about inline function

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 6 Posters 0 Views 1 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.
  • N Offline
    N Offline
    nachilau
    wrote on last edited by
    #1

    Hello all, I have some questions about inline function, if I have something like this class myClass { protected: int i; public: inline Set_I(int val) {i = val;}; } int main() { myClass c; c.Set_I(10); } I want to know what will actually happen. Since Set_I() is an inline function, the compiler will replcae the definition of the function in where the function has been invoked. However, in this case the inline is actually accessing the protected member of the class, so what will happen?? Thanks! Nachi

    R M C I 4 Replies Last reply
    0
    • N nachilau

      Hello all, I have some questions about inline function, if I have something like this class myClass { protected: int i; public: inline Set_I(int val) {i = val;}; } int main() { myClass c; c.Set_I(10); } I want to know what will actually happen. Since Set_I() is an inline function, the compiler will replcae the definition of the function in where the function has been invoked. However, in this case the inline is actually accessing the protected member of the class, so what will happen?? Thanks! Nachi

      R Offline
      R Offline
      Ryan Binns
      wrote on last edited by
      #2

      nachilau wrote: However, in this case the inline is actually accessing the protected member of the class, so what will happen?? Exactly the same thing. The protected keyword tells the compiler that the i variable can be accessed by members of myClass or its derivatives. This condition is still true. Whether or not the compiler inlines the member functions makes no difference at all. It is still the same function accessing the same variable. Hope this helps,

      Ryan

      "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

      1 Reply Last reply
      0
      • N nachilau

        Hello all, I have some questions about inline function, if I have something like this class myClass { protected: int i; public: inline Set_I(int val) {i = val;}; } int main() { myClass c; c.Set_I(10); } I want to know what will actually happen. Since Set_I() is an inline function, the compiler will replcae the definition of the function in where the function has been invoked. However, in this case the inline is actually accessing the protected member of the class, so what will happen?? Thanks! Nachi

        M Offline
        M Offline
        Maxwell Chen
        wrote on last edited by
        #3

        For your information. 6.3: The keyword inline[^] Maxwell Chen

        1 Reply Last reply
        0
        • N nachilau

          Hello all, I have some questions about inline function, if I have something like this class myClass { protected: int i; public: inline Set_I(int val) {i = val;}; } int main() { myClass c; c.Set_I(10); } I want to know what will actually happen. Since Set_I() is an inline function, the compiler will replcae the definition of the function in where the function has been invoked. However, in this case the inline is actually accessing the protected member of the class, so what will happen?? Thanks! Nachi

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          nachilau wrote: compiler will replcae the definition of the function in where the function has been invoked. No. inline is a suggestion to the compiler, it can choose to ignore it. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

          1 Reply Last reply
          0
          • N nachilau

            Hello all, I have some questions about inline function, if I have something like this class myClass { protected: int i; public: inline Set_I(int val) {i = val;}; } int main() { myClass c; c.Set_I(10); } I want to know what will actually happen. Since Set_I() is an inline function, the compiler will replcae the definition of the function in where the function has been invoked. However, in this case the inline is actually accessing the protected member of the class, so what will happen?? Thanks! Nachi

            I Offline
            I Offline
            includeh10
            wrote on last edited by
            #5

            inline is an important keyword, but it must be used as: 1. in h file, but not inside class scope(if so, it is ignored) 2. not in cpp file (if so, it is ignored) example is //MyClass.h class MyClass { public int Get(); }; inline int MyClass::Get(){return 1;} advantage of inline function is 1.(at least books say) it is faster than normal function. 2.in case of you need to put your code on head file (e.g. craete library) includeh10

            J 1 Reply Last reply
            0
            • I includeh10

              inline is an important keyword, but it must be used as: 1. in h file, but not inside class scope(if so, it is ignored) 2. not in cpp file (if so, it is ignored) example is //MyClass.h class MyClass { public int Get(); }; inline int MyClass::Get(){return 1;} advantage of inline function is 1.(at least books say) it is faster than normal function. 2.in case of you need to put your code on head file (e.g. craete library) includeh10

              J Offline
              J Offline
              jhwurmbach
              wrote on last edited by
              #6

              includeh10 wrote: public int Get(); }; inline int MyClass::Get(){return 1;} 2.in case of you need to put your code on head file (e.g. craete library) In fact, the compiler is trying to inline any function defined in the *.h-file. Regardless of inline or not. If you want to suggest to inline a function with a body in the *.cpp-file, you add inline before the declaration in the header file. In any case, you are only giving a suggestion, you can not force the compiler to do the inlining.


              "We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.D.

              I 1 Reply Last reply
              0
              • J jhwurmbach

                includeh10 wrote: public int Get(); }; inline int MyClass::Get(){return 1;} 2.in case of you need to put your code on head file (e.g. craete library) In fact, the compiler is trying to inline any function defined in the *.h-file. Regardless of inline or not. If you want to suggest to inline a function with a body in the *.cpp-file, you add inline before the declaration in the header file. In any case, you are only giving a suggestion, you can not force the compiler to do the inlining.


                "We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.D.

                I Offline
                I Offline
                includeh10
                wrote on last edited by
                #7

                i think u never use inline before. code above can not be compiled if no keyword inline. includeh10

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups