Like Tree1Likes
  • 1 Post By antred

Person::name and this->name difference?

This is a discussion on Person::name and this->name difference? within the C++ Programming forums, part of the General Programming Boards category; Code: void Person::setname(string name) { Person::name = name; cout<<Person::name; } Code: void Person::setname(string name) { this->name = name; cout<<Person::name; } ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    48

    Person::name and this->name difference?

    Code:
    void Person::setname(string name)
    {
    
        Person::name = name;
        cout<<Person::name;
    }
    Code:
    void Person::setname(string name)
    {
    
        this->name = name;
        cout<<Person::name;
    }
    I know this(keyword) is a pointer that points to the current object that has called setname function.

    But I want to know what's the difference between using
    Person::name and this->name.

  2. #2
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    In this case, there is no difference. If you had a class that was derived from class Person and that adds an additional attribute also called "name" and you wanted to get access to the Person class's "name" attribute, then you could only do it via Person::name. If you just said "this->name", you'd get the derived class's "name" attribute instead.
    manasij7479 likes this.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Wow, 16 hours have passed, and nobody has corrected the above posted. Heck, there's even a "like" on it!

    No, they are not the same. Person::name refers to a static class member. Being static means that there is only one name in the entire program. Every Person object would be associated with the name "Bob" for example.

    this->name refers to a normal class member. In this case there is a separate name for each instance of the Person class.

    You cannot use the two interchangably.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,181
    Quote Originally Posted by iMalc
    Wow, 16 hours have passed, and nobody has corrected the above posted. Heck, there's even a "like" on it!
    I did not bother checking since I vaguely recalled something about this kind of disambiguation being used similiar to what antred mentioned.

    Quote Originally Posted by iMalc
    No, they are not the same.
    Because of the caveat "in this case", antred does not appear to dispute that.

    EDIT:
    Quote Originally Posted by iMalc
    Person::name refers to a static class member. Being static means that there is only one name in the entire program. Every Person object would be associated with the name "Bob" for example. (...) You cannot use the two interchangably.
    g++ 4.6.3 and the Comeau online compiler compile this test program without any warnings:
    Code:
    #include <iostream>
    
    class X
    {
    public:
        X() : n(1) {}
    
        virtual ~X() {}
    
        virtual void foo() const
        {
            std::cout << n << std::endl;
        }
    protected:
        int n;
    };
    
    class Y : public X
    {
    public:
        Y() : n(2) {}
    
        void setN(int n)
        {
            Y::n = n;
        }
    
        virtual void foo() const
        {
            std::cout << X::n << ',' << Y::n << std::endl;
        }
    private:
        int n;
    };
    
    int main()
    {
        Y y;
        y.foo();
        y.setN(3);
        y.foo();
    }
    When run after being compiled by g++ 4.6.3, the output is:
    Code:
    1,2
    1,3
    EDIT #2:
    Oh, but of course, one oft-cited case of the use of this is when a name foo in a derived class D hides members of the same name in the base class B. A using declaration like using B::foo; in the definition of D would then be appropriate, even if the members named foo in the base class are non-static.
    Last edited by laserlight; 05-28-2012 at 01:29 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    48
    Person::name refers to a static class member.
    In my class. I have not declared name as static. But it still both code compiles fine?
    Last edited by freiza; 05-28-2012 at 02:44 AM.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    Quote Originally Posted by freiza View Post
    In my class. I have not declared name as static. But it still both code compiles fine?
    The point of Person::name when name is static is so you can access the variable without an object, but if you access name through an object there is no difference.

  7. #7
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    Quote Originally Posted by iMalc View Post
    Wow, 16 hours have passed, and nobody has corrected the above posted. Heck, there's even a "like" on it!

    No, they are not the same. Person::name refers to a static class member.
    Not in this case, iMalc. Specifying a fully qualified name merely distinguishes between several attributes that happen to have the same name. It does not say anything about whether that attribute is necessarily static or not.

    Please, try to for yourself.

    Code:
    #include <iostream>
    
    class Person
    {
    public:
        Person() : age( 40 ) {}
    
    protected:
        const int age;
    };
    
    class Student : public Person
    {
    public:
        Student() : age( 16 ) {}
    
        void print() const;
    
    private:
        const int age;
    };
    
    void Student::print() const
    {
        std::cout << "Person::age = " << Person::age << std::endl;
        std::cout << "Student::age = " << Student::age << std::endl;
        std::cout << "age = " << age << std::endl;
    }
    
    
    int main()
    {
        const Student a;
        a.print();
    }
    Output:

    Person::age = 40
    Student::age = 16
    age = 16



    EDIT: Nevermind, I see laserlight has already posted a very similar test program.
    Last edited by antred; 05-28-2012 at 04:13 AM.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Yeah alright, I simply forgot for a moment that it didn't necessarily mean that it was static.
    On the plus side, we now have a lot more informative information in this thread.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Randomizing a person's name
    By Naz in forum C++ Programming
    Replies: 1
    Last Post: 11-03-2010, 11:13 PM
  2. Another new person - incrementing
    By MB1 in forum C++ Programming
    Replies: 5
    Last Post: 02-24-2005, 06:54 AM
  3. 1st Person Shooter
    By Okiesmokie in forum Game Programming
    Replies: 3
    Last Post: 01-09-2002, 12:25 AM
  4. Who is the coolest person here?
    By Betazep in forum A Brief History of Cprogramming.com
    Replies: 41
    Last Post: 11-04-2001, 11:42 AM
  5. What should Person A do?
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 11-03-2001, 01:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21