3 Replies - 663 Views - Last Post: 08 June 2009 - 11:34 AM Rate Topic: -----

#1 ABXG  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 17-April 09

Question About Pointers And Classes

Posted 08 June 2009 - 12:19 AM

So for a while now I have been working with classes almost exclusively in C++ and I always declare a pointer to my class when using them, ie:

MyClass * ptr = new MyClass ();



My question is really about when building a class, should I bother to make excessive use of the "this" pointer? By excessive use I mean making sure every single time I access a variable or member within my class I reference it with the "this" pointer, even if it is pointing at another pointer,

ie I am building a class based wrapper for DirectShow and DarkGDK and I up getting lines of code like this line of code:

this->ppose->put_CurrentPosition (timeIndex);


I really just want to know if there is any advantage (such as better performance or safer operation) to using "this" other than the autocomplete feature in Visual Studios (IntelliSense). Can using "this" be a bad thing in some or any cases?

I have tried using Google to find my answer but search engines don't mix well with generic terms :/

This post has been edited by ABXG: 08 June 2009 - 01:12 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Question About Pointers And Classes

#2 Dantheman  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 445
  • Joined: 27-May 09

Re: Question About Pointers And Classes

Posted 08 June 2009 - 08:08 AM

Compilers make sure to prefix this for you. Using it will only make your code less readable. Use it only when you need to.

This post has been edited by Dantheman: 08 June 2009 - 08:09 AM

Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5928
  • View blog
  • Posts: 12,851
  • Joined: 16-October 07

Re: Question About Pointers And Classes

Posted 08 June 2009 - 08:20 AM

View PostDantheman, on 8 Jun, 2009 - 09:08 AM, said:

Using it will only make your code less readable. Use it only when you need to.


Or, using it will explicitly let you know that you're working with an element that belongs to your current instance. Thus clarifying the scope and making in more readable. ;)

It's more a matter of style than function. Using it has no impact on the compiled code. However, there's a difference here:
// actually sets a value in class Foo
void Foo::setMiles(int miles) { this->miles = miles; }

// does nothing
void Foo::setMiles(int miles) { miles = miles; }



Such times you must use it. Other than that, it's really up to you.

This post has been edited by baavgai: 08 June 2009 - 08:21 AM

Was This Post Helpful? 1
  • +
  • -

#4 Dantheman  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 445
  • Joined: 27-May 09

Re: Question About Pointers And Classes

Posted 08 June 2009 - 11:34 AM

View Postbaavgai, on 8 Jun, 2009 - 07:20 AM, said:

Or, using it will explicitly let you know that you're working with an element that belongs to your current instance. Thus clarifying the scope and making in more readable. ;)


True.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1