Thread: Is it weird C++ to dereference 'this' and call an operator on it?

  1. #1
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665

    Is it weird C++ to dereference 'this' and call an operator on it?

    Let's say that I've got a class that wraps a raw array. I've implemented the operator[] method which is great. In implementing the at() method, I decided that I'd just use:
    Code:
    (*this)[index]
    .

    Is that weird C++?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Nah, but I think you know the alternative.

    this->operator[](index);

  3. #3
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by whiteflags View Post
    Nah, but I think you know the alternative.

    this->operator[](index);
    I kind of thought I could do that but I wasn't sure. Thank you for demonstrating the syntax!

    Edit: I forgot that the Like button was gone!!!

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,732
    It isn't weird at all. In fact, it's the only way it should be done(in my opinion)...
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    What is wrong with this:

    operator[](index)

    This is just a style preference, but I prefer not to write this everywhere in C++.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not wrong. It's a matter of style. But again, the reason we have operators is so that we can use more natural style and (*this)[idx] is more natural.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    I think the weird thing here is that "this" is a pointer. But as you know, there's history there.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,398
    Well, you can

    Code:
    MyClass& self = *this;
    self[123] = 345;
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird compilation error in call of private function!
    By MutantJohn in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2016, 03:23 PM
  2. weird function call
    By ale88 in forum C Programming
    Replies: 3
    Last Post: 08-27-2011, 07:59 PM
  3. Bitshift operator overloading help (weird)
    By Syndacate in forum C++ Programming
    Replies: 6
    Last Post: 01-13-2011, 04:39 PM
  4. Location of the dereference operator
    By merlin2011 in forum C Programming
    Replies: 11
    Last Post: 11-13-2008, 09:01 AM
  5. Weird Problem with return with operator +
    By KonArtis in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2004, 03:46 PM

Tags for this Thread