Thread: Overloading a function like this

  1. #1
    Registered User
    Join Date
    Mar 2002
    Location
    South Africa
    Posts
    35

    Question Overloading a function like this

    Here is my code - can anyone pls tell me why my compiler won't allow me to do this?

    Code:
    class TMyClass
    {
      public:
        Ansistring Message();
        char* Message();
      
      private:
        AnsiString sMessage;
    };
    my compiler won't allow me to overload Message() like this. Why?
    (I use Borland C++Builder 5.0)
    I code.
    I think.
    Ergo, I think in code.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    What's the actual error

  3. #3
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Ansistring Message();
    char* Message();

    Isnt this double declaring?

  4. #4
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    I think I know.



    You can't overload a function by just changing their return types.
    You have to modify the types of your function arguments.

    Ansistring message();
    char *message();

    is equivalent to

    Ansistring message(void);
    char *message(void);

    This is illegal so the compiler won't allow it.

    You have to modify it to something like this:

    Ansistring message(Sometype myvariable);
    char *message(char myvariable);

    or

    Ansistring message(Sometype myvariable);
    char *message(Sometype myvariable, int variabletoo, char hehe);

    Just as long as your function parameter types are not that identical.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Location
    South Africa
    Posts
    35

    "This is illegal so the compiler won't allow it. "

    I know that - but why is it illegal?

    Thanx for the reply though.

  6. #6
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Because the compiler can't determine which one to call based on the return type, doing so would be extremely error prone when it isn't absolutely impossible.

    So either change the function name or the functions parameters.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: "This is illegal so the compiler won't allow it. "

    Originally posted by Koedoe
    I know that - but why is it illegal?

    Thanx for the reply though.
    Because when you run the function Message(void) the compiler has two functions to choose from, and he doesn't know which one to take. This is illegal and illogical.
    If the functions have different argument lists, then it knows which one to pick.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Location
    South Africa
    Posts
    35
    I know that the compiler mangles the function names using the argument lists - why doesn't it just include the return type in the mangled name?
    I code.
    I think.
    Ergo, I think in code.

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Koedoe
    I know that the compiler mangles the function names using the argument lists - why doesn't it just include the return type in the mangled name?
    Because C isn't strictly typed as compared to Ada . You can return a char but store it in an integer variable.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post your games here...
    By Hammer in forum Game Programming
    Replies: 132
    Last Post: 02-28-2013, 09:29 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM