Thread: Can I get rid of these?

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    Can I get rid of these?

    I'm refactoring someone else's code and I keep coming across instances of these:
    Code:
    someClass** sc;
    *sc = pointer to something somewhere else;
    ... (*((*sc))).someMember; //note the double parentheses
    The code as it is compiles and works, but the double parentheses are an eyesore, and I'd like to clean them up as:
    Code:
    (*sc)->someMember;
    Would I be breaking anything here by doing this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Would I be breaking anything here by doing this?
    No, they are equivalent.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I don't think you need the double ones.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Would there be any reason why this person would have coded like this in the first place? Personal quirk?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Style. Fun. Looks cool. Looks better. In someone's point of view, at least. Pick one. There's no advantage in the language in doing so.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cunnus88 View Post
    The code as it is compiles and works, but the double parentheses are an eyesore, and I'd like to clean them up as:
    Code:
    (*sc)->someMember;
    That's fine, and looks way better. If you're doing a bunch of accesses through (*sc), you can make a reference to alias it which cleans things up even further. Instead of the above, you could:

    Code:
    someClass &scref = **sc;
    
    scref.someMember;

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I tend to do
    Code:
    (**sc).someMember;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 19
    Last Post: 09-17-2005, 09:49 AM
  2. getting rid of the unsightly "^M" character
    By EvBladeRunnervE in forum Tech Board
    Replies: 3
    Last Post: 03-17-2004, 08:54 PM
  3. getting rid of grub
    By axon in forum Tech Board
    Replies: 3
    Last Post: 03-17-2004, 07:15 PM
  4. How do you get rid of the cursor
    By zz3ta in forum C++ Programming
    Replies: 1
    Last Post: 12-09-2003, 03:15 PM
  5. get rid of rest
    By pode in forum Networking/Device Communication
    Replies: 1
    Last Post: 10-20-2003, 06:55 PM