overload on return type

Discussion in 'C++' started by Shea Martin, Apr 8, 2006.

  1. Shea Martin

    Shea Martin Guest

    I have a struct like so:

    struct MyStruct
    {
    public:
    void Value( int newValue ) { mValue = newValue; }
    int Value() const { return mValue; }

    private:
    int mValue;
    };

    This is a compile error on g++, as it overloads on return type. But I
    think it compiles on MSVC++. I am at home now, and don't have access to
    a windows/MSVC++, so I can't verify this. Can someone tell me if I am
    losing my mind? I kind of like the semantics of having my get set
    methods like this. I don't use g++ a lot, but if it is not std C++, or
    if it won't be in the future, then I'll drop it.

    ~S
    Shea Martin, Apr 8, 2006
    #1
    1. Advertising

  2. Shea Martin

    KV Rani Guest

    It compiled on my g++.
    g++ (GCC) 3.4.4 (cygming special) (gdc 0.12, using dmd 0.125).
    KV Rani, Apr 8, 2006
    #2
    1. Advertising

  3. Shea Martin <> wrote:
    > I have a struct like so:
    >
    > struct MyStruct
    > {
    > public:
    > void Value( int newValue ) { mValue = newValue; }
    > int Value() const { return mValue; }
    >
    > private:
    > int mValue;
    > };
    >
    > This is a compile error on g++, as it overloads on return type.


    It is valid and actually pretty common. Note that you do not
    overload on the return type. You cannot overload a function solely on
    its return type. In your case, both of your functions have different
    number of arguments, thus it is fine.

    hth
    --
    jb

    (reply address in rot13, unscramble first)
    Jakob Bieling, Apr 8, 2006
    #3
  4. Shea Martin

    Rolf Magnus Guest

    Shea Martin wrote:

    > I have a struct like so:
    >
    > struct MyStruct
    > {
    > public:
    > void Value( int newValue ) { mValue = newValue; }
    > int Value() const { return mValue; }
    >
    > private:
    > int mValue;
    > };
    >
    > This is a compile error on g++, as it overloads on return type.


    What version of g++ is that? It should compile. The return type is just not
    considered in function overloading, and since the two functions differ in
    both the parameter list and the cv-qualifiers, they are valid overloads.
    Rolf Magnus, Apr 8, 2006
    #4
  5. Shea Martin schrieb:
    > I have a struct like so:
    >
    > struct MyStruct
    > {
    > public:
    > void Value( int newValue ) { mValue = newValue; }
    > int Value() const { return mValue; }
    >
    > private:
    > int mValue;
    > };
    >
    > This is a compile error on g++, as it overloads on return type. But I
    > think it compiles on MSVC++. I am at home now, and don't have access to
    > a windows/MSVC++, so I can't verify this. Can someone tell me if I am
    > losing my mind? I kind of like the semantics of having my get set
    > methods like this. I don't use g++ a lot, but if it is not std C++, or
    > if it won't be in the future, then I'll drop it.


    on the Linux platform gcc is the most used compiler
    and I never had problems with it so far
    I started using it from 2.95 Version up to 4.0.2 now

    but in general it's always a good idea to try your code
    with (newer) different compilers

    as other pointed out you don't overload on the return type

    void foo(){} //
    int foo(){} // here is what you assumed
    but this is an error and will be flaged by compiler

    you even could go so far and overload methods (non static class
    functions) on their cv qualifiers. cv stads for const volatile

    #include <iostream>
    #include <cstdlib>

    using std::cout;
    using std::endl;

    #define P(msg) cout << #msg << endl;

    struct Q {
    void Value(int x) { xx = x; }
    int & Value() { P(ref); return xx; }
    int Value()const { P(val); return xx; }
    int Value()volatile { P(volatile); return xx; }
    int Value()const volatile { P(const-volatile); return xx; }
    Q(int x=0x0ABCDEF0) : xx(x) {}
    int xx;
    };

    int main()
    {
    Q q = 1;
    q.Value(2);
    cout << q.Value() << endl;

    Q const qq(3);
    //qq.Value(4); // only const-qualified methods can be called
    on const object
    cout << qq.Value() << endl;

    Q volatile qqq;
    cout << qqq.Value() << endl;

    const Q /*const*/ volatile qqqq;
    cout << qqqq.Value() << endl;

    return EXIT_SUCCESS;
    }


    hth, Daniel
    =?ISO-8859-1?Q?Sch=FCle_Daniel?=, Apr 9, 2006
    #5
  6. Shea Martin

    Shea Martin Guest

    Thanks for the replies. I thought I had done it before. It compiles
    now. I must have had an error somewhere else, and not read the error
    that closely. I am not that efficient at reading gcc error messages, as
    I used to use Sun's compilers for my job, now I use MSVC++ for my job...

    GCC is for my hobby stuff.

    Thanks.

    ~S
    Shea Martin, Apr 9, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Piotre Ugrumov
    Replies:
    3
    Views:
    368
    Nick Hounsome
    Jan 25, 2004
  2. Atlas
    Replies:
    6
    Views:
    422
    Ron Natalie
    Dec 28, 2004
  3. Fabio Fracassi
    Replies:
    5
    Views:
    326
  4. jrbcast
    Replies:
    8
    Views:
    772
    DeMarcus
    Feb 20, 2010
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    248
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page