Results 1 to 16 of 16

Thread: C++ : What does a commented arrgument mean?

  1. #1
    Member
    Join Date
    May 2004
    Location
    Bristol
    Posts
    130
    Thanks
    0
    Thanked
    0 times in 0 posts

    Question C++ : What does a commented arrgument mean?

    I am quite new to c++ and since starting have noticed on a few occasions functions with arguments identifiers apparently "commented out". I haven't had any real problems, but I was wondering if it meant anything in particular.

    For example, in a function to repaint an object from an example in the QT toolkit (this function is an overridden protected from its parent class).

    Code:
     void paintEvent(QPaintEvent * /* event */)
    {
    ....
    }
    Why is it not?:

    Code:
     void paintEvent(QPaintEvent *  event)
    {
    ....
    }
    So, is there any relevance of "/*event */" rather than just "event" here?

  2. #2
    Hexus.net Troll Dougal's Avatar
    Join Date
    Jun 2005
    Location
    In your eyeball.
    Posts
    2,750
    Thanks
    0
    Thanked
    0 times in 0 posts
    The event section has been commented out the compiler ignores it

    Code:
    void paintEvent(QPaintEven *)
    {
    ...
    }
    Is the actual function, the "event" is just to tell you its an event.

    All the function is doing is obtaining a pointer to a QPaintEvent.

    But it should have a name according to what I have been taught.
    Quote Originally Posted by Errr...me
    I MSN offline people
    6014 3DMk 05

  3. #3
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,168
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts
    i think it might well be just a typo, i've not really done that much ++ but i've never seen something like that, nor can i understand what it would mean, the first * is for indirection, but it should then have an identifier. By encapsulating the identifyer in comments, it wouldn't get parsard, and it would find the end bracked, and should throw a build error.
    throw new ArgumentException (String, String, Exception)

  4. #4
    Member
    Join Date
    May 2004
    Location
    Bristol
    Posts
    130
    Thanks
    0
    Thanked
    0 times in 0 posts
    Ah, I see. I think it strange, like Dougal, that the argument has no identifier, but I suppose it doesn't need one if it's not referenced (it only needs an argument to fit the function prototype of the inherited function).
    I would have included an identifier, if I was writing this, so as not to confuse people like this has confused me.
    Thanks for the speediness in responding

  5. #5
    Registered+
    Join Date
    Aug 2005
    Location
    London
    Posts
    44
    Thanks
    0
    Thanked
    0 times in 0 posts
    That's a rather unusual coding convention! Still, each to their own eh?

    I personally find commenting arguments to be useful when dealing with default values (i.e. the default value is commented in the implementation). Can save some headscratching.

  6. #6
    Hexus.net Troll Dougal's Avatar
    Join Date
    Jun 2005
    Location
    In your eyeball.
    Posts
    2,750
    Thanks
    0
    Thanked
    0 times in 0 posts
    I still think its wrong.

    That pointer has no name to be used within the function therefore there is no way it can be referenced.

    So in effect it should bring up a warning at the very least.
    Quote Originally Posted by Errr...me
    I MSN offline people
    6014 3DMk 05

  7. #7
    Ex-MSFT Paul Adams's Avatar
    Join Date
    Jul 2003
    Location
    %systemroot%
    Posts
    1,926
    Thanks
    29
    Thanked
    77 times in 59 posts
    • Paul Adams's system
      • Motherboard:
      • Asus Maximus VIII
      • CPU:
      • Intel Core i7-6700K
      • Memory:
      • 16GB
      • Storage:
      • 2x250GB SSD / 500GB SSD / 2TB HDD
      • Graphics card(s):
      • nVidia GeForce GTX1080
      • Operating System:
      • Windows 10 x64 Pro
      • Monitor(s):
      • Philips 40" 4K
      • Internet:
      • 500Mbps fiber
    I don't think it throws out a compiler or linker error, it's not good practice but the labels are only for the programmer's benefit.
    Arguments passed to functions can be referenced if you use a pointer to the stack, so it's not necessary to use labels.
    ~ I have CDO. It's like OCD except the letters are in alphabetical order, as they should be. ~
    PC: Win10 x64 | Asus Maximus VIII | Core i7-6700K | 16GB DDR3 | 2x250GB SSD | 500GB SSD | 2TB SATA-300 | GeForce GTX1080
    Camera: Canon 60D | Sigma 10-20/4.0-5.6 | Canon 100/2.8 | Tamron 18-270/3.5-6.3

  8. #8
    Hexus.net Troll Dougal's Avatar
    Join Date
    Jun 2005
    Location
    In your eyeball.
    Posts
    2,750
    Thanks
    0
    Thanked
    0 times in 0 posts
    That;ll be why then, we just covered stack theory and how it worked.
    Quote Originally Posted by Errr...me
    I MSN offline people
    6014 3DMk 05

  9. #9
    Ex-MSFT Paul Adams's Avatar
    Join Date
    Jul 2003
    Location
    %systemroot%
    Posts
    1,926
    Thanks
    29
    Thanked
    77 times in 59 posts
    • Paul Adams's system
      • Motherboard:
      • Asus Maximus VIII
      • CPU:
      • Intel Core i7-6700K
      • Memory:
      • 16GB
      • Storage:
      • 2x250GB SSD / 500GB SSD / 2TB HDD
      • Graphics card(s):
      • nVidia GeForce GTX1080
      • Operating System:
      • Windows 10 x64 Pro
      • Monitor(s):
      • Philips 40" 4K
      • Internet:
      • 500Mbps fiber
    I remember seeing some very odd-looking code in assembler years ago, when every CPU cycle counted for writing graphical "demos" for MCGA displays... a call to a subroutine would push arguments onto the stack, but the subroutine would actually point directly to the stack to use them, not pop them off, then it would manually move the stack pointer in 1 CPU operation instead of 1 per stack pop.

    Cunning, and ultimately unreadable code
    ~ I have CDO. It's like OCD except the letters are in alphabetical order, as they should be. ~
    PC: Win10 x64 | Asus Maximus VIII | Core i7-6700K | 16GB DDR3 | 2x250GB SSD | 500GB SSD | 2TB SATA-300 | GeForce GTX1080
    Camera: Canon 60D | Sigma 10-20/4.0-5.6 | Canon 100/2.8 | Tamron 18-270/3.5-6.3

  10. #10
    Member
    Join Date
    May 2004
    Location
    Bristol
    Posts
    130
    Thanks
    0
    Thanked
    0 times in 0 posts
    Quote Originally Posted by Paul Adams
    Cunning, and ultimately unreadable code
    Ah a classic professional practice unless I am very much mistaken

  11. #11
    Ex-MSFT Paul Adams's Avatar
    Join Date
    Jul 2003
    Location
    %systemroot%
    Posts
    1,926
    Thanks
    29
    Thanked
    77 times in 59 posts
    • Paul Adams's system
      • Motherboard:
      • Asus Maximus VIII
      • CPU:
      • Intel Core i7-6700K
      • Memory:
      • 16GB
      • Storage:
      • 2x250GB SSD / 500GB SSD / 2TB HDD
      • Graphics card(s):
      • nVidia GeForce GTX1080
      • Operating System:
      • Windows 10 x64 Pro
      • Monitor(s):
      • Philips 40" 4K
      • Internet:
      • 500Mbps fiber
    Quote Originally Posted by Dihi Doctor
    Ah a classic professional practice unless I am very much mistaken
    Most likely
    A friend of mine inherited some source code along with a big project he was handed, and the original programmer had used variable names such as "a", "aa", "aaa", "ooooh", "hmmm", "b1", "b2" and so on.... most of which were never actually used for anything.

    I could never program for a career, other people's (lack of) coding standards would drive me insane!
    ~ I have CDO. It's like OCD except the letters are in alphabetical order, as they should be. ~
    PC: Win10 x64 | Asus Maximus VIII | Core i7-6700K | 16GB DDR3 | 2x250GB SSD | 500GB SSD | 2TB SATA-300 | GeForce GTX1080
    Camera: Canon 60D | Sigma 10-20/4.0-5.6 | Canon 100/2.8 | Tamron 18-270/3.5-6.3

  12. #12
    Registered+
    Join Date
    Aug 2005
    Location
    London
    Posts
    44
    Thanks
    0
    Thanked
    0 times in 0 posts
    Some people have no concept of teamwork! I'm glad to work in an environment where good coding standards exist. New people on the team usually have trouble adopting them, but I find that our coding standards certainly makes the code more readable when it needs to be (i.e. when it's 7pm on a Friday and a client needs something doing "now").

  13. #13
    Hexus.net Troll Dougal's Avatar
    Join Date
    Jun 2005
    Location
    In your eyeball.
    Posts
    2,750
    Thanks
    0
    Thanked
    0 times in 0 posts
    Opensource goddamnit!
    Quote Originally Posted by Errr...me
    I MSN offline people
    6014 3DMk 05

  14. #14
    Senior Member
    Join Date
    Jan 2005
    Location
    Manchester
    Posts
    2,900
    Thanks
    67
    Thanked
    182 times in 136 posts
    • Butcher's system
      • Motherboard:
      • MSI Z97 Gaming 3
      • CPU:
      • i7-4790K
      • Memory:
      • 8 GB Corsair 1866 MHz
      • Storage:
      • 120GB SSD, 240GB SSD, 2TB HDD
      • Graphics card(s):
      • MSI GTX 970
      • PSU:
      • Antec 650W
      • Case:
      • Big Black Cube!
      • Operating System:
      • Windows 7
    It's not a syntax error to have an unnamed argument to a function in C++

    void foo (int)
    {
    // do stuff
    }

    is valid, though you can't actually use the int passed.

    NB, directly fiddling the stack is very bad practice in C/C++. Not naming your arguments so you can hack the stack is not a good idea. The compiler will read them straight off the stack rather than popping them in any event.

  15. #15
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,168
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts
    i would of hope it at least gives a warning?

    if its stdcall, the calling code will push and pop that one param, as its upto the calling program to balance. So i would of thought hacking the stack like that (thou i can't see why you'd want to) would work.
    throw new ArgumentException (String, String, Exception)

  16. #16
    Senior Member
    Join Date
    Jan 2005
    Location
    Manchester
    Posts
    2,900
    Thanks
    67
    Thanked
    182 times in 136 posts
    • Butcher's system
      • Motherboard:
      • MSI Z97 Gaming 3
      • CPU:
      • i7-4790K
      • Memory:
      • 8 GB Corsair 1866 MHz
      • Storage:
      • 120GB SSD, 240GB SSD, 2TB HDD
      • Graphics card(s):
      • MSI GTX 970
      • PSU:
      • Antec 650W
      • Case:
      • Big Black Cube!
      • Operating System:
      • Windows 7
    It doesn't give a warning; it's perfectly legal code.
    You can't use the param, but it still gets passed. It's normally used for virtual functions where the specific implementation doesn't need the param but it needs to be there for overload resolution. Or in callback functions where the function has to comform to a specific prototype, but the code doesn't use the param. It's also sometimes used for backwards or forwards compatibility.

    For reference the standard explcitily states that parameters need not be named:
    Quote Originally Posted by C++ standard 2003 8.3.5.8
    An identifier can optionally be provided as a parameter name; if present in a function definition (8.4), it names a parameter (sometimes called “formal argument”). [Note: in particular, parameter names are also optional in function definitions and names used for a parameter in different declarations and the definition of a function need not be the same. ... ]

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •