Use of “this” keyword in C++ [duplicate]











up vote
52
down vote

favorite
24













Possible Duplicate:
Is excessive use of this in C++ a code smell
When should you use the "this" keyword in C++?
Is there any reason to use this->






In C++, is the keyword this usually omitted? For example:



Person::Person(int age) {
_age = age;
}


As opposed to:



Person::Person(int age) {
this->_age = age;
}









share|improve this question















marked as duplicate by Kev Jul 21 '11 at 16:46


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 7




    most people use it when the variable passed to the function has the same name as the instance variable...
    – HRÓÐÓLFR
    Jul 21 '11 at 16:44















up vote
52
down vote

favorite
24













Possible Duplicate:
Is excessive use of this in C++ a code smell
When should you use the "this" keyword in C++?
Is there any reason to use this->






In C++, is the keyword this usually omitted? For example:



Person::Person(int age) {
_age = age;
}


As opposed to:



Person::Person(int age) {
this->_age = age;
}









share|improve this question















marked as duplicate by Kev Jul 21 '11 at 16:46


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 7




    most people use it when the variable passed to the function has the same name as the instance variable...
    – HRÓÐÓLFR
    Jul 21 '11 at 16:44













up vote
52
down vote

favorite
24









up vote
52
down vote

favorite
24






24






Possible Duplicate:
Is excessive use of this in C++ a code smell
When should you use the "this" keyword in C++?
Is there any reason to use this->






In C++, is the keyword this usually omitted? For example:



Person::Person(int age) {
_age = age;
}


As opposed to:



Person::Person(int age) {
this->_age = age;
}









share|improve this question
















Possible Duplicate:
Is excessive use of this in C++ a code smell
When should you use the "this" keyword in C++?
Is there any reason to use this->






In C++, is the keyword this usually omitted? For example:



Person::Person(int age) {
_age = age;
}


As opposed to:



Person::Person(int age) {
this->_age = age;
}






c++ this






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 7 at 13:46

























asked Jul 21 '11 at 16:41









moteutsch

2,29532135




2,29532135




marked as duplicate by Kev Jul 21 '11 at 16:46


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Kev Jul 21 '11 at 16:46


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 7




    most people use it when the variable passed to the function has the same name as the instance variable...
    – HRÓÐÓLFR
    Jul 21 '11 at 16:44














  • 7




    most people use it when the variable passed to the function has the same name as the instance variable...
    – HRÓÐÓLFR
    Jul 21 '11 at 16:44








7




7




most people use it when the variable passed to the function has the same name as the instance variable...
– HRÓÐÓLFR
Jul 21 '11 at 16:44




most people use it when the variable passed to the function has the same name as the instance variable...
– HRÓÐÓLFR
Jul 21 '11 at 16:44












6 Answers
6






active

oldest

votes

















up vote
73
down vote



accepted










Yes, it is not required and is usually omitted. It might be required for accessing variables after they have been overridden in the scope though:



Person::Person() {
int age;
this->age = 1;
}


Also, this:



Person::Person(int age) {
_age = age;
}


It is pretty bad style; if you need an initializer with the same name use this notation:



Person::Person(int age) : age(age) { }





share|improve this answer



















  • 4




    I have no idea what the last syntax is called, could you give me some hint / info about what to search for to understand how does that work ?
    – Stormsson
    Jan 7 '16 at 22:35






  • 13




    @Stormsson Member initializer lists.
    – orlp
    Jan 8 '16 at 1:59












  • It's not just bad style, it's also less efficient than initialization. Moreover, const and reference variables must be initialized on the line they are declared.
    – Alejandro Blasco
    Mar 22 at 9:02










  • @orlp there is actually a case where you have to use this-> pointer: if you are using derived template classes. In the first compilation phase, the member variables of parent-classes need to be accessed with this-> or ParentClass:: to make sure the compiler knows that those are not typenames.
    – Dorian
    May 11 at 17:12


















up vote
17
down vote













It's programmer preference. Personally, I love using this since it explicitly marks the object members. Of course the _ does the same thing (only when you follow the convention)






share|improve this answer



















  • 8




    The underscore only does the same thing if you follow the convention.
    – R. Martinho Fernandes
    Jul 21 '11 at 16:44






  • 11




    Using this to explicitly mark object members also only works when you follow the convention.
    – Robert Martin
    Nov 13 '13 at 13:12






  • 2




    Note that "the convention" @Rich has mentioned is to use an underscore prefix to indicate object members. When you do not follow the convention, the method parameter has exactly the the same name as the object member, and this is required to avoid ambiguity. But I believe an explicit this "works" whether or not you follow the convention.
    – Tom Anderson
    Jan 5 '16 at 3:11




















up vote
6
down vote













Either way works, but many places have coding standards in place that will guide the developer one way or the other. If such a policy is not in place, just follow your heart. One thing, though, it REALLY helps the readability of the code if you do use it. especially if you are not following a naming convention on class-level variable names.






share|improve this answer




























    up vote
    5
    down vote













    this points to the object in whose member function it is reffered, so it is optional.






    share|improve this answer




























      up vote
      3
      down vote













      For the example case above, it is usually omitted, yes. However, either way is syntactically correct.






      share|improve this answer




























        up vote
        3
        down vote













        Yes. unless, there is an ambiguity.



        It is also used sometimes to avoid some compilers optimizing virtual function call.






        share|improve this answer

















        • 12




          o avoid some compilers optimizing virtual function call, How so?
          – Alok Save
          Jul 21 '11 at 16:47










        • Yes, explain please?
          – Gerard
          Apr 29 '14 at 16:36


















        6 Answers
        6






        active

        oldest

        votes








        6 Answers
        6






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        73
        down vote



        accepted










        Yes, it is not required and is usually omitted. It might be required for accessing variables after they have been overridden in the scope though:



        Person::Person() {
        int age;
        this->age = 1;
        }


        Also, this:



        Person::Person(int age) {
        _age = age;
        }


        It is pretty bad style; if you need an initializer with the same name use this notation:



        Person::Person(int age) : age(age) { }





        share|improve this answer



















        • 4




          I have no idea what the last syntax is called, could you give me some hint / info about what to search for to understand how does that work ?
          – Stormsson
          Jan 7 '16 at 22:35






        • 13




          @Stormsson Member initializer lists.
          – orlp
          Jan 8 '16 at 1:59












        • It's not just bad style, it's also less efficient than initialization. Moreover, const and reference variables must be initialized on the line they are declared.
          – Alejandro Blasco
          Mar 22 at 9:02










        • @orlp there is actually a case where you have to use this-> pointer: if you are using derived template classes. In the first compilation phase, the member variables of parent-classes need to be accessed with this-> or ParentClass:: to make sure the compiler knows that those are not typenames.
          – Dorian
          May 11 at 17:12















        up vote
        73
        down vote



        accepted










        Yes, it is not required and is usually omitted. It might be required for accessing variables after they have been overridden in the scope though:



        Person::Person() {
        int age;
        this->age = 1;
        }


        Also, this:



        Person::Person(int age) {
        _age = age;
        }


        It is pretty bad style; if you need an initializer with the same name use this notation:



        Person::Person(int age) : age(age) { }





        share|improve this answer



















        • 4




          I have no idea what the last syntax is called, could you give me some hint / info about what to search for to understand how does that work ?
          – Stormsson
          Jan 7 '16 at 22:35






        • 13




          @Stormsson Member initializer lists.
          – orlp
          Jan 8 '16 at 1:59












        • It's not just bad style, it's also less efficient than initialization. Moreover, const and reference variables must be initialized on the line they are declared.
          – Alejandro Blasco
          Mar 22 at 9:02










        • @orlp there is actually a case where you have to use this-> pointer: if you are using derived template classes. In the first compilation phase, the member variables of parent-classes need to be accessed with this-> or ParentClass:: to make sure the compiler knows that those are not typenames.
          – Dorian
          May 11 at 17:12













        up vote
        73
        down vote



        accepted







        up vote
        73
        down vote



        accepted






        Yes, it is not required and is usually omitted. It might be required for accessing variables after they have been overridden in the scope though:



        Person::Person() {
        int age;
        this->age = 1;
        }


        Also, this:



        Person::Person(int age) {
        _age = age;
        }


        It is pretty bad style; if you need an initializer with the same name use this notation:



        Person::Person(int age) : age(age) { }





        share|improve this answer














        Yes, it is not required and is usually omitted. It might be required for accessing variables after they have been overridden in the scope though:



        Person::Person() {
        int age;
        this->age = 1;
        }


        Also, this:



        Person::Person(int age) {
        _age = age;
        }


        It is pretty bad style; if you need an initializer with the same name use this notation:



        Person::Person(int age) : age(age) { }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '14 at 2:31









        Jonathan Leffler

        553k866571009




        553k866571009










        answered Jul 21 '11 at 16:44









        orlp

        66k26156244




        66k26156244








        • 4




          I have no idea what the last syntax is called, could you give me some hint / info about what to search for to understand how does that work ?
          – Stormsson
          Jan 7 '16 at 22:35






        • 13




          @Stormsson Member initializer lists.
          – orlp
          Jan 8 '16 at 1:59












        • It's not just bad style, it's also less efficient than initialization. Moreover, const and reference variables must be initialized on the line they are declared.
          – Alejandro Blasco
          Mar 22 at 9:02










        • @orlp there is actually a case where you have to use this-> pointer: if you are using derived template classes. In the first compilation phase, the member variables of parent-classes need to be accessed with this-> or ParentClass:: to make sure the compiler knows that those are not typenames.
          – Dorian
          May 11 at 17:12














        • 4




          I have no idea what the last syntax is called, could you give me some hint / info about what to search for to understand how does that work ?
          – Stormsson
          Jan 7 '16 at 22:35






        • 13




          @Stormsson Member initializer lists.
          – orlp
          Jan 8 '16 at 1:59












        • It's not just bad style, it's also less efficient than initialization. Moreover, const and reference variables must be initialized on the line they are declared.
          – Alejandro Blasco
          Mar 22 at 9:02










        • @orlp there is actually a case where you have to use this-> pointer: if you are using derived template classes. In the first compilation phase, the member variables of parent-classes need to be accessed with this-> or ParentClass:: to make sure the compiler knows that those are not typenames.
          – Dorian
          May 11 at 17:12








        4




        4




        I have no idea what the last syntax is called, could you give me some hint / info about what to search for to understand how does that work ?
        – Stormsson
        Jan 7 '16 at 22:35




        I have no idea what the last syntax is called, could you give me some hint / info about what to search for to understand how does that work ?
        – Stormsson
        Jan 7 '16 at 22:35




        13




        13




        @Stormsson Member initializer lists.
        – orlp
        Jan 8 '16 at 1:59






        @Stormsson Member initializer lists.
        – orlp
        Jan 8 '16 at 1:59














        It's not just bad style, it's also less efficient than initialization. Moreover, const and reference variables must be initialized on the line they are declared.
        – Alejandro Blasco
        Mar 22 at 9:02




        It's not just bad style, it's also less efficient than initialization. Moreover, const and reference variables must be initialized on the line they are declared.
        – Alejandro Blasco
        Mar 22 at 9:02












        @orlp there is actually a case where you have to use this-> pointer: if you are using derived template classes. In the first compilation phase, the member variables of parent-classes need to be accessed with this-> or ParentClass:: to make sure the compiler knows that those are not typenames.
        – Dorian
        May 11 at 17:12




        @orlp there is actually a case where you have to use this-> pointer: if you are using derived template classes. In the first compilation phase, the member variables of parent-classes need to be accessed with this-> or ParentClass:: to make sure the compiler knows that those are not typenames.
        – Dorian
        May 11 at 17:12












        up vote
        17
        down vote













        It's programmer preference. Personally, I love using this since it explicitly marks the object members. Of course the _ does the same thing (only when you follow the convention)






        share|improve this answer



















        • 8




          The underscore only does the same thing if you follow the convention.
          – R. Martinho Fernandes
          Jul 21 '11 at 16:44






        • 11




          Using this to explicitly mark object members also only works when you follow the convention.
          – Robert Martin
          Nov 13 '13 at 13:12






        • 2




          Note that "the convention" @Rich has mentioned is to use an underscore prefix to indicate object members. When you do not follow the convention, the method parameter has exactly the the same name as the object member, and this is required to avoid ambiguity. But I believe an explicit this "works" whether or not you follow the convention.
          – Tom Anderson
          Jan 5 '16 at 3:11

















        up vote
        17
        down vote













        It's programmer preference. Personally, I love using this since it explicitly marks the object members. Of course the _ does the same thing (only when you follow the convention)






        share|improve this answer



















        • 8




          The underscore only does the same thing if you follow the convention.
          – R. Martinho Fernandes
          Jul 21 '11 at 16:44






        • 11




          Using this to explicitly mark object members also only works when you follow the convention.
          – Robert Martin
          Nov 13 '13 at 13:12






        • 2




          Note that "the convention" @Rich has mentioned is to use an underscore prefix to indicate object members. When you do not follow the convention, the method parameter has exactly the the same name as the object member, and this is required to avoid ambiguity. But I believe an explicit this "works" whether or not you follow the convention.
          – Tom Anderson
          Jan 5 '16 at 3:11















        up vote
        17
        down vote










        up vote
        17
        down vote









        It's programmer preference. Personally, I love using this since it explicitly marks the object members. Of course the _ does the same thing (only when you follow the convention)






        share|improve this answer














        It's programmer preference. Personally, I love using this since it explicitly marks the object members. Of course the _ does the same thing (only when you follow the convention)







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 11 '13 at 19:21









        pranavk

        6092918




        6092918










        answered Jul 21 '11 at 16:43









        Rich

        5,09484777




        5,09484777








        • 8




          The underscore only does the same thing if you follow the convention.
          – R. Martinho Fernandes
          Jul 21 '11 at 16:44






        • 11




          Using this to explicitly mark object members also only works when you follow the convention.
          – Robert Martin
          Nov 13 '13 at 13:12






        • 2




          Note that "the convention" @Rich has mentioned is to use an underscore prefix to indicate object members. When you do not follow the convention, the method parameter has exactly the the same name as the object member, and this is required to avoid ambiguity. But I believe an explicit this "works" whether or not you follow the convention.
          – Tom Anderson
          Jan 5 '16 at 3:11
















        • 8




          The underscore only does the same thing if you follow the convention.
          – R. Martinho Fernandes
          Jul 21 '11 at 16:44






        • 11




          Using this to explicitly mark object members also only works when you follow the convention.
          – Robert Martin
          Nov 13 '13 at 13:12






        • 2




          Note that "the convention" @Rich has mentioned is to use an underscore prefix to indicate object members. When you do not follow the convention, the method parameter has exactly the the same name as the object member, and this is required to avoid ambiguity. But I believe an explicit this "works" whether or not you follow the convention.
          – Tom Anderson
          Jan 5 '16 at 3:11










        8




        8




        The underscore only does the same thing if you follow the convention.
        – R. Martinho Fernandes
        Jul 21 '11 at 16:44




        The underscore only does the same thing if you follow the convention.
        – R. Martinho Fernandes
        Jul 21 '11 at 16:44




        11




        11




        Using this to explicitly mark object members also only works when you follow the convention.
        – Robert Martin
        Nov 13 '13 at 13:12




        Using this to explicitly mark object members also only works when you follow the convention.
        – Robert Martin
        Nov 13 '13 at 13:12




        2




        2




        Note that "the convention" @Rich has mentioned is to use an underscore prefix to indicate object members. When you do not follow the convention, the method parameter has exactly the the same name as the object member, and this is required to avoid ambiguity. But I believe an explicit this "works" whether or not you follow the convention.
        – Tom Anderson
        Jan 5 '16 at 3:11






        Note that "the convention" @Rich has mentioned is to use an underscore prefix to indicate object members. When you do not follow the convention, the method parameter has exactly the the same name as the object member, and this is required to avoid ambiguity. But I believe an explicit this "works" whether or not you follow the convention.
        – Tom Anderson
        Jan 5 '16 at 3:11












        up vote
        6
        down vote













        Either way works, but many places have coding standards in place that will guide the developer one way or the other. If such a policy is not in place, just follow your heart. One thing, though, it REALLY helps the readability of the code if you do use it. especially if you are not following a naming convention on class-level variable names.






        share|improve this answer

























          up vote
          6
          down vote













          Either way works, but many places have coding standards in place that will guide the developer one way or the other. If such a policy is not in place, just follow your heart. One thing, though, it REALLY helps the readability of the code if you do use it. especially if you are not following a naming convention on class-level variable names.






          share|improve this answer























            up vote
            6
            down vote










            up vote
            6
            down vote









            Either way works, but many places have coding standards in place that will guide the developer one way or the other. If such a policy is not in place, just follow your heart. One thing, though, it REALLY helps the readability of the code if you do use it. especially if you are not following a naming convention on class-level variable names.






            share|improve this answer












            Either way works, but many places have coding standards in place that will guide the developer one way or the other. If such a policy is not in place, just follow your heart. One thing, though, it REALLY helps the readability of the code if you do use it. especially if you are not following a naming convention on class-level variable names.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jul 21 '11 at 16:46









            Muad'Dib

            21.9k54460




            21.9k54460






















                up vote
                5
                down vote













                this points to the object in whose member function it is reffered, so it is optional.






                share|improve this answer

























                  up vote
                  5
                  down vote













                  this points to the object in whose member function it is reffered, so it is optional.






                  share|improve this answer























                    up vote
                    5
                    down vote










                    up vote
                    5
                    down vote









                    this points to the object in whose member function it is reffered, so it is optional.






                    share|improve this answer












                    this points to the object in whose member function it is reffered, so it is optional.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jul 21 '11 at 16:43









                    Alok Save

                    163k35338478




                    163k35338478






















                        up vote
                        3
                        down vote













                        For the example case above, it is usually omitted, yes. However, either way is syntactically correct.






                        share|improve this answer

























                          up vote
                          3
                          down vote













                          For the example case above, it is usually omitted, yes. However, either way is syntactically correct.






                          share|improve this answer























                            up vote
                            3
                            down vote










                            up vote
                            3
                            down vote









                            For the example case above, it is usually omitted, yes. However, either way is syntactically correct.






                            share|improve this answer












                            For the example case above, it is usually omitted, yes. However, either way is syntactically correct.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jul 21 '11 at 16:43









                            Chad

                            15k23252




                            15k23252






















                                up vote
                                3
                                down vote













                                Yes. unless, there is an ambiguity.



                                It is also used sometimes to avoid some compilers optimizing virtual function call.






                                share|improve this answer

















                                • 12




                                  o avoid some compilers optimizing virtual function call, How so?
                                  – Alok Save
                                  Jul 21 '11 at 16:47










                                • Yes, explain please?
                                  – Gerard
                                  Apr 29 '14 at 16:36















                                up vote
                                3
                                down vote













                                Yes. unless, there is an ambiguity.



                                It is also used sometimes to avoid some compilers optimizing virtual function call.






                                share|improve this answer

















                                • 12




                                  o avoid some compilers optimizing virtual function call, How so?
                                  – Alok Save
                                  Jul 21 '11 at 16:47










                                • Yes, explain please?
                                  – Gerard
                                  Apr 29 '14 at 16:36













                                up vote
                                3
                                down vote










                                up vote
                                3
                                down vote









                                Yes. unless, there is an ambiguity.



                                It is also used sometimes to avoid some compilers optimizing virtual function call.






                                share|improve this answer












                                Yes. unless, there is an ambiguity.



                                It is also used sometimes to avoid some compilers optimizing virtual function call.







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Jul 21 '11 at 16:45









                                balki

                                10.5k1759106




                                10.5k1759106








                                • 12




                                  o avoid some compilers optimizing virtual function call, How so?
                                  – Alok Save
                                  Jul 21 '11 at 16:47










                                • Yes, explain please?
                                  – Gerard
                                  Apr 29 '14 at 16:36














                                • 12




                                  o avoid some compilers optimizing virtual function call, How so?
                                  – Alok Save
                                  Jul 21 '11 at 16:47










                                • Yes, explain please?
                                  – Gerard
                                  Apr 29 '14 at 16:36








                                12




                                12




                                o avoid some compilers optimizing virtual function call, How so?
                                – Alok Save
                                Jul 21 '11 at 16:47




                                o avoid some compilers optimizing virtual function call, How so?
                                – Alok Save
                                Jul 21 '11 at 16:47












                                Yes, explain please?
                                – Gerard
                                Apr 29 '14 at 16:36




                                Yes, explain please?
                                – Gerard
                                Apr 29 '14 at 16:36



                                Popular posts from this blog

                                Florida Star v. B. J. F.

                                Error while running script in elastic search , gateway timeout

                                Adding quotations to stringified JSON object values