472,980 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,980 software developers and data experts.

Does the following construct qualify as overloading on return type ?

Hi,
Does the following construct qualify as overloading on return type ?
If not, in what way ? I overloaded the type conversion operator and
used pointers to member functions. Its pretty straightforward but I
am sure I have missed as this is not supposed to be possible. Am I
misunderstanding overloading ?
Thanks,
Nikhil

// foo() overloads on both input parameters and return value
//-------------------------------------------------------------
// int int_output = foo(int &int_input);
// double double_output = foo(int &int_input);
// int int_output = foo(double &double_input);
// double double_output = foo(double &double_input);
//-------------------------------------------------------------

#include <iostream>

class foo
{
public:
foo(int *given) {given_int = given; int_ = &foo::int_int; double_ =
&foo::double_int;}
foo(double *given) {given_double = given; int_ = &foo::int_double;
double_ = &foo::double_double;}
operator double () { (this->*double_)(); }
operator int () { (this->*int_)(); }
private:
double double_double() { std::cout << "--foo called with input
double and output double\n";}
double double_int() { std::cout << "-->foo called with output
double and output int was called\n";}
int int_int() { std::cout << "-->foo called with input int and
output int was called\n";}
int int_double() { std::cout << "-->foo called with input int and
output double was called\n";}
int *given_int;
double *given_double;
int (foo::*int_)();
double (foo::*double_)();
};

int main(int argc, char *argv)
{
double double_input, double_output;
int int_input, int_output;

std::cout << "Trying foo with int input and int output\n";
int_output = foo(&int_input);

std::cout << "Trying foo with int input and double output\n";
double_output = foo(&int_input);

std::cout << "Trying foo with double input and int output\n";
int_output = foo(&double_input);

std::cout << "Trying foo with double input and double output\n";
double_output = foo(&double_input);

}

// Output

// Trying foo with int input and int output
// -->foo called with input int and output int was called
// Trying foo with int input and double output
// -->foo called with output double and output int was called
// Trying foo with double input and int output
// -->foo called with input int and output double was called
// Trying foo with double input and double output
// --foo called with input double and output double

Aug 15 '08 #1
1 1450
Ni*************@gmail.com kirjutas:
Hi,
Does the following construct qualify as overloading on return type ?
If not, in what way ? I overloaded the type conversion operator and
used pointers to member functions. Its pretty straightforward but I
am sure I have missed as this is not supposed to be possible. Am I
misunderstanding overloading ?
By definition, only functions can be overloaded. Your foo is a class
instead, so it cannot be overloaded. Neither can it be called like you
seem to think according to the text output messages. I'm pretty sure the
different conversion operators are not called overloads either (correct
me if I'm wrong!)

But yes, by helper classes like this one can accomplish more than by a
simple function. Functors come to mind first. Here the helper class
imitates overloading by the return value, but I guess other solutions to
this problem might be simpler in most cases.

BTW, I simplified your code by removing unneeded obfuscations, and fixed
some UB errors, here it is:

#include <iostream>

class foo
{
public:
foo(int given): double_(given) {
std::cout << "-->foo called with input int";
}
foo(double given): double_(given) {
std::cout << "-->foo called with input double";
}
operator int () {
std::cout << " and output int\n"; return int(double_);
}
operator double () {
std::cout << " and output double\n"; return double_;
}
private:
double double_;
};

int main()
{
double double_input=0, double_output;
int int_input=0, int_output;

std::cout << "Trying foo with int input and int output\n";
int_output = foo(int_input);

std::cout << "Trying foo with int input and double output\n";
double_output = foo(int_input);

std::cout << "Trying foo with double input and int output\n";
int_output = foo(double_input);

std::cout << "Trying foo with double input and double output\n";
double_output = foo(double_input);

}
Regards
Paavo

>
Thanks,
Nikhil

// foo() overloads on both input parameters and return value
//-------------------------------------------------------------
// int int_output = foo(int &int_input);
// double double_output = foo(int &int_input);
// int int_output = foo(double &double_input);
// double double_output = foo(double &double_input);
//-------------------------------------------------------------

#include <iostream>

class foo
{
public:
foo(int *given) {given_int = given; int_ = &foo::int_int; double_ =
&foo::double_int;}
foo(double *given) {given_double = given; int_ = &foo::int_double;
double_ = &foo::double_double;}
operator double () { (this->*double_)(); }
operator int () { (this->*int_)(); }
private:
double double_double() { std::cout << "--foo called with input
double and output double\n";}
double double_int() { std::cout << "-->foo called with output
double and output int was called\n";}
int int_int() { std::cout << "-->foo called with input int and
output int was called\n";}
int int_double() { std::cout << "-->foo called with input int and
output double was called\n";}
int *given_int;
double *given_double;
int (foo::*int_)();
double (foo::*double_)();
};

int main(int argc, char *argv)
{
double double_input, double_output;
int int_input, int_output;

std::cout << "Trying foo with int input and int output\n";
int_output = foo(&int_input);

std::cout << "Trying foo with int input and double output\n";
double_output = foo(&int_input);

std::cout << "Trying foo with double input and int output\n";
int_output = foo(&double_input);

std::cout << "Trying foo with double input and double output\n";
double_output = foo(&double_input);

}

// Output

// Trying foo with int input and int output
// -->foo called with input int and output int was called
// Trying foo with int input and double output
// -->foo called with output double and output int was called
// Trying foo with double input and int output
// -->foo called with input int and output double was called
// Trying foo with double input and double output
// --foo called with input double and output double

Aug 15 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Terje Slettebų | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
20
by: Randy Yates | last post by:
Why is this necessary? The identifiers provide by the programmer are unique, and that is what is important - why map what the user has specified to something else? -- Randy Yates Sony Ericsson...
4
by: PKH | last post by:
This is a part of an autopointer template i'm using. I originally had a Get() member that returned the pointer, but thought it would nice to be able to use the -> operator. The question is why...
31
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
4
by: Tony Johansson | last post by:
Hello!! I have done some operator overloading but my main testprogram doesn't work well. Have you any idea which of my methods are wrong? #include <iostream> #include <string> using...
6
by: flopbucket | last post by:
Could someone explain to me what the difference is between function template specialization and function overloading? I guess overloading can change the number of parameters, but otherwise they...
14
by: Pramod | last post by:
I have one question. Can I catch exception in function overloading.In my programm i want to create two function with same signature and same return type. I know its not possible...can i use...
1
by: Madhav | last post by:
Hi All, Very recently I had one iteresting question about overloading. The guy was arguing why we dont have overloading based on return type. I told him about the reason mentioned in "Thinking in...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.