Does this question make any sense?

This is a discussion on Does this question make any sense? within the C++ Programming forums, part of the General Programming Boards category; Preparing for my 2nd semester C++ class final here, and this is one of the practice problems we have: Code: ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    19

    Does this question make any sense?

    Preparing for my 2nd semester C++ class final here, and this is one of the practice problems we have:
    Code:
    //Consider the following defnition:
    
    template<class Item>
    Item maximal (Item &a, Item &b)
    {
    if (a > b)
         return a;
    else
         return b;
    }
    //What member functions will be called from the Item class 
    //(assuming that all necessary functions exist)?
    The question seems to be assuming that Item is a class, but from this given code, Item is just a type template parameter, and could therefore be any data type. This in no way makes it seem like Item is actually a class. I thought that maybe my teacher was being tricky, and that this is a special overridden version for Item of a broader template, but I looked up how to do that and I'm pretty sure that this isn't an example of that. I also thought that he might be trying to trick people into thinking that Item is a class when it actually isn't, but when I've asked him he seems pretty stubborn about Item being a class. This looks like just a template function which for some reason uses <class Item> instead of, say, <typename T> for the template parameter. I tried to get my teacher to clarify what he's asking for here, but somehow he thinks that it is perfectly clear (this has been a problem all year, where for some reason he thinks things are clear, and then later realizes that they aren't, but by then it's too late).

    So, yeah. This question doesn't make sense to anyone I've talked to in the class. I understand everything that I've read and been taught about templates and classes, and this question just doesn't seem to make any sense. So, can anyone here maybe understand what my teacher is asking?

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    The question seems to be assuming that Item is a class, but from this given code, Item is just a type template parameter, and could therefore be any data type. This in no way makes it seem like Item is actually a class.
    I hate to be insensitive to your problem, but the question should make perfect sense if you've learned anything about the C++ type system. Even if Item is a plain old data type, the same things would be called in that case, as if the data type were a class. In case this is a clever attempt at getting the answer to that question, I won't explain the answer, but plain old data has everything provided that a class type would be using in that code.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    It is something of a trick question, because:
    • Item does not have to be a class type.
    • If Item is a class type, it does not have to have a member function that will be called in this context.
    • But... it might, and furthermore a constructor call may or may not be involved when returning from this function template.


    However, to keep it simple: consider operator overloading.
    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

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    19
    Well, I probably should have included what I'm thinking the answer is, because I guess I do look like some idiot trying to get a free answer. The > operator will be called, and then if Item is not a native data type like an int or double, the copy constructor for the class will be called to return by value, and then the destructor will be called to destroy the temporary object created by the return by value. This question just seemed to be too vague, as it was assuming that the type passed into the template was a class. I'm just trying to make sure I understand this, so is what I said correct?

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,699
    This is actually a Function Template. If Item were a class it should call the class:perator>() function.

    Jim

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Quote Originally Posted by Freddy92
    The > operator will be called, and then if Item is not a native data type like an int or double, the copy constructor for the class will be called to return by value, and then the destructor will be called to destroy the temporary object created by the return by value. This question just seemed to be too vague, as it was assuming that the type passed into the template was a class. I'm just trying to make sure I understand this, so is what I said correct?
    In terms of what I expect your instructor to have in mind, yes, you have the right idea.

    Now, here's what's wrong with your picture:
    • The copy constructor of the class might not be called on the return because it may be elided (return value optimisation), and the whole instance of the function template might even be inlined.
    • The operator> that will be called may be a non-member function.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does this make any sense?
    By xniinja in forum C Programming
    Replies: 20
    Last Post: 08-05-2010, 11:07 AM
  2. Can anyone make sense of this?
    By tjpanda in forum C++ Programming
    Replies: 4
    Last Post: 10-12-2007, 02:16 PM
  3. does this make sense ??
    By agarwaga in forum C++ Programming
    Replies: 14
    Last Post: 03-08-2006, 06:10 PM
  4. Can anyone help me make sense from this?
    By correlcj in forum C Programming
    Replies: 1
    Last Post: 07-23-2002, 07:31 PM
  5. anyone make any sense of this
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 10-03-2001, 10:29 PM

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