Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > overload on return type

Reply
Thread Tools

overload on return type

 
 
Shea Martin
Guest
Posts: n/a
 
      04-08-2006
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
 
Reply With Quote
 
 
 
 
KV Rani
Guest
Posts: n/a
 
      04-08-2006
It compiled on my g++.
g++ (GCC) 3.4.4 (cygming special) (gdc 0.12, using dmd 0.125).

 
Reply With Quote
 
 
 
 
Jakob Bieling
Guest
Posts: n/a
 
      04-08-2006
Shea Martin <(E-Mail Removed)> 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)


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      04-08-2006
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.

 
Reply With Quote
 
=?ISO-8859-1?Q?Sch=FCle_Daniel?=
Guest
Posts: n/a
 
      04-08-2006
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
 
Reply With Quote
 
Shea Martin
Guest
Posts: n/a
 
      04-08-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Heterogeneous collection: return type overload jrbcast C++ 8 02-20-2010 01:45 PM
(Templated) Operator overload on return type. Fabio Fracassi C++ 5 09-09-2005 11:03 AM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
function overload (not operator overload) Ying-Chieh Liao Perl Misc 3 10-11-2004 11:24 AM
How use the overload of>> (or<<) of a class in the overload of << and >> of another class? Piotre Ugrumov C++ 3 01-25-2004 08:08 PM



Advertisments