Thread: unable to overload functions

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well, to be even fairer, it is quite unlikely that you want an overload like this in the first place. There are a multitude of ways to resolve the ambiguity. Removing the copy constructor by making it private is yet another way. But causing the ambiguity in the first place makes the code seem funky.

    Overloaded functions are usually meant to do similar things with different inputs. Literally the only difference between the two functions is whether you copy arguments or not, and it would be better to just decide if you do or not and remove one function or the other. For example if copying is an inexpensive process, (and IMHO it usually is, unless it is an entire data structure instance) then by-value semantics conveys every advantage that constant reference semantics will.
    Last edited by whiteflags; 05-18-2014 at 02:52 PM.

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by whiteflags View Post
    Removing the copy constructor by making it private is yet another way.
    Making the copy constructor private does not solve the problem because it does not remove it from the overload resolution. The compiler still considers it a viable overload. So you still get an ambiguity error, or in worst case, you might even confuse the compiler to spit out that the copy constructor is private and that the call is ambiguous (e.g. Microsoft's compiler).

    Private functions are not removed from overload resolution because then you would get a "I cannot find this function" instead of "this function is private" when trying to call a function that is private. It just happens that the compiler picks the best matching function, and if it cannot, you get an ambiguity error, and it it can and the function is private, you get a function is private error.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User
    Join Date
    Nov 2013
    Location
    Norway
    Posts
    40
    Code:
    struct cat {
        cat() {}
        explicit cat(const cat&) {}
    };
    
    
    void cheezburger(cat) { std::cout << "i iz hungry, gief chezburger plz"; }
    void cheezburger(const cat&) { std::cout << "lolololol get #rekt s0n"; }
    
    
    int main()
    {
        cat lolCat;
        cheezburger(lolCat);
    }
    Sorry, I just saw some major errors in your code.. This version should increase read-ability and general humour by a thousand... and one.

    unable to overload functions-lolcat-jpg
    /thread
    "Derp, derp, derp" - Me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to overload [][]
    By chottachatri in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2008, 12:38 AM
  2. overload *
    By shuo in forum C++ Programming
    Replies: 5
    Last Post: 06-10-2007, 04:44 AM
  3. What is this overload?
    By Mortissus in forum C++ Programming
    Replies: 2
    Last Post: 07-02-2006, 10:38 PM
  4. overload
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 12-11-2001, 09:38 AM
  5. Overload (+) operator
    By kit in forum C++ Programming
    Replies: 8
    Last Post: 10-23-2001, 11:20 AM