Can I get rid of these?

This is a discussion on Can I get rid of these? within the C++ Programming forums, part of the General Programming Boards category; I'm refactoring someone else's code and I keep coming across instances of these: Code: someClass** sc; *sc = pointer to ...

  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
    21,311
    Would I be breaking anything here by doing this?
    No, they are equivalent.
    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

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    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, 07:54 PM
  3. getting rid of grub
    By axon in forum Tech Board
    Replies: 3
    Last Post: 03-17-2004, 06:15 PM
  4. How do you get rid of the cursor
    By zz3ta in forum C++ Programming
    Replies: 1
    Last Post: 12-09-2003, 02:15 PM
  5. get rid of rest
    By pode in forum Networking/Device Communication
    Replies: 1
    Last Post: 10-20-2003, 06:55 PM

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