unary operator overloading and classes

This is a discussion on unary operator overloading and classes within the C++ Programming forums, part of the General Programming Boards category; Which of the following statements accurately describe unary operator overloading in C++? A. A unary operator can be overloaded with ...

  1. #1
    Spam is Good
    Join Date
    Jan 2009
    Location
    In a cave on Mars
    Posts
    37

    unary operator overloading and classes

    Which of the following statements accurately describe unary operator overloading in C++?

    A. A unary operator can be overloaded with one parameter when the operator function is a class member.
    B. A unary operator can only be overloaded if the operator function is a class member.
    C. A unary operator can be overloaded with one parameter when the operator function is free standing function (not a class member).
    D. A unary operator can be overloaded with no parameters when the operator function is a class member.
    E. A unary operator can be overloaded with no parameters when the operator function is a free standing function (not a class member)

    I would say A and B because unary means one parameter and to do overloading it must be a class member (I rather sure). D and E are wrong because its needs one parameter. C is wrong because you need a class member.

    Can someone please clarify my answer. I'm a little unsure if you can do overloading as a free standing function (not a class member). Thx.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,658
    Quote Originally Posted by coletek
    I would say A and B because unary means one parameter and to do overloading it must be a class member (I rather sure).
    Both A and B are wrong. For a non-static member function, the object itself (or perhaps its this pointer) is a hidden argument, so a non-static member function with n parameters actually has n+1 parameters.
    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
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Both A and B are wrong. For a non-static member function, the object itself (or perhaps its this pointer) is a hidden argument, so a non-static member function with n parameters actually has n+1 parameters.
    ++() and ++(int) come to mind. Once we chew up what laserlight said, D and E are the only ones I'll accept. Unless, of course, you consider the dummy int in operator++(int) a parameter.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Spam is Good
    Join Date
    Jan 2009
    Location
    In a cave on Mars
    Posts
    37
    For a non-static member function, the object itself (or perhaps its this pointer) is a hidden argument, so a non-static member function with n parameters actually has n+1 parameters.
    Ok, after reading http://publib.boulder.ibm.com/infoce...c11cplr323.htm. I see there is a differences in the number of parameters for a non-static member function and a nonmember function. But why does making it static or not matter?

    Also after reading http://publib.boulder.ibm.com/infoce...c11cplr318.htm I see "An operator function can be either a nonstatic member function, or a nonmember function with at least one parameter that has class, reference to class, enumeration, or reference to enumeration type." So this tells me overloading can be done as a standing free function (ie. a nonmember function), but again when its a member function why does it matter if its static or not?

    From this:
    * A is incorrect because its an unary operator, so for a member function it has no parameters.
    * B is incorrect because you can have a operator function as a nonmember function.
    * C is CORRECT because its an unary operator, and for a nonmember function it has one parameter.
    * D is CORRECT because its an unary operator, and for a member function it has no parameters
    * E is incorrect because of the statement "An operator function can be ... a nonmember function with at least one parameter that has class, reference to class, enumeration, or reference to enumeration type"

    Would you all agree?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Nonstatic member functions don't have an implicit this parameter, so you don't actually have access to the object you called the function with.

    Although there's no reason to blame IBM; that language appears in the ISO C++ standard (section 13.5), so there doesn't necessarily have to be a reason.

  6. #6
    Spam is Good
    Join Date
    Jan 2009
    Location
    In a cave on Mars
    Posts
    37
    Nonstatic member functions don't have an implicit this parameter
    . Sorry I don't understand this sentence. Please re-state.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,658
    Quote Originally Posted by coletek
    Would you all agree?
    Yes, except that CodeMonkey pointed out an exception for A, and due to the wording of A this exception makes A correct.
    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

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by coletek View Post
    . Sorry I don't understand this sentence. Please re-state.
    There's not much else there -- you can't say "this" in a static member function. So if you tried to do
    Code:
    object.static_member_function();
    you would be utterly unable to access "object" from inside the function.

  9. #9
    Spam is Good
    Join Date
    Jan 2009
    Location
    In a cave on Mars
    Posts
    37
    There's not much else there -- you can't say "this" in a static member function.
    I see, I didn't read the "this" as the pointer "this".

    Yes, except that CodeMonkey pointed out an exception for A, and due to the wording of A this exception makes A correct.
    Hmmm... I don't really see this, as for a class member I thought there would be no parameter. Could you give an example to help me understand this exception. Thx.

  10. #10
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    I said D and E? Whoops. You knew what I meant. Honest

    There is a pre-increment operator and a post-increment operator. They both are denoted by the token ++

    The only difference is that one goes before the thing being incremented, and one goes after. But how do you specify this in a function declaration? The language does not have any such feature. So, the standard declares that operator++() is pre-increment and operator++(int) is post-increment. The int is a dummy -- a mere signifier (doesn't even get a name).
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

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