LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-21-2005, 08:29 PM   #1
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
Question about 'this' in C++


When programming a class in C++, I'm wondering if there is any speed difference or other negative reasons for using the 'this' pointer to access data members. Its harder for me to explain, but easier to show with code. I'm wondering if these 2 class methods have any difference from eachother.

Code:
class Test
{
private:
    string data_;

public:
    void setData1(string& data);
    void setData2(string& data);

};

void Test::setData1(string& data)
{
    // use 'this' for extra clarity
    this->data_ = data;
}

void Test::setData2(string& data)
{
    // don't use 'this'
    data_ = data;
}
I prefer using the 'this->data_member' notation because it makes my classes easier to read. I'm wondering if there is any negative impact to using this notation.

I was also wondering the same thing about the resolution operator, '::'. I can't see any negative effects of using it in classes like this:

Code:
// same class definition as above

void Test::setData1(string& data)
{
    // Use scope resolution operator
    Test::data_ = data;
}

void Test::setData2(string& data)
{
    // Dont use scope resolution operator
    data_ = data;
}
 
Old 08-21-2005, 10:42 PM   #2
FreeThinkerJim
Member
 
Registered: Apr 2005
Location: Las Vegas, Nevada, USA
Distribution: Ubntu Intrepid Ibex (8.10)
Posts: 129

Rep: Reputation: 15
Re: Question about 'this' in C++

Quote:
Originally posted by lowpro2k3
When programming a class in C++, I'm wondering if there is any speed difference or other negative reasons for using the 'this' pointer to access data members. Its harder for me to explain, but easier to show with code. I'm wondering if these 2 class methods have any difference from eachother.

Code:
class Test
{
private:
    string data_;

public:
    void setData1(string& data);
    void setData2(string& data);

};

void Test::setData1(string& data)
{
    // use 'this' for extra clarity
    this->data_ = data;
}

void Test::setData2(string& data)
{
    // don't use 'this'
    data_ = data;
}
I prefer using the 'this->data_member' notation because it makes my classes easier to read. I'm wondering if there is any negative impact to using this notation.


Using the 'this' pointer to reference a class's data within its methods is totally superfluous. When the compiler writes out the machine code for a method, it does this anyway, and so putting it in your code doesn't really have an effect.


Quote:
I was also wondering the same thing about the resolution operator, '::'. I can't see any negative effects of using it in classes like this:

Code:
// same class definition as above

void Test::setData1(string& data)
{
    // Use scope resolution operator
    Test::data_ = data;
}

void Test::setData2(string& data)
{
    // Dont use scope resolution operator
    data_ = data;
}
Like the this pointer thing, doing this is also unnecessary. Doing these things might make your code seem more readable, but it's really nonstandard and can throw a lot of people off
 
Old 08-22-2005, 12:13 AM   #3
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
Re: Question about 'this' in C++

Quote:
Originally posted by lowpro2k3

I was also wondering the same thing about the resolution operator, '::'. I can't see any negative effects of using it in classes like this:

Code:
// same class definition as above

void Test::setData1(string& data)
{
    // Use scope resolution operator
    Test::data_ = data;
}

void Test::setData2(string& data)
{
    // Dont use scope resolution operator
    data_ = data;
}
Using the resolution operator is not a problem in this case. However, it could lead to problems if you want to use it in a polymorphic class. If you explicitly qualify a member, it is going to use the member of the class you specify no matter what. Sometimes this is the behavior you want. Sometimes you would rather have a derived class's member be referenced.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question, Apples Contribution to Open Source + MacOs file structure question Higgy3k Other *NIX 5 07-25-2005 04:23 AM
Not your regular GRUB question - just a short question for a fried MBR!! ziphem Linux - General 3 01-31-2005 01:51 PM
2 part question: Speeding up MDK9.1/GNOME question wardialer Linux - Newbie 6 10-14-2004 03:16 PM
login prompt question & kde scheme question JustinCoyan Slackware 2 06-09-2004 02:02 PM
RE: Suse 8.0 hardware question {newbie question, pls help} Radiouk Linux - Distributions 2 06-04-2002 12:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration