471,579 Members | 1,283 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,579 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 1335
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Terje Slettebø | last post: by
20 posts views Thread by Randy Yates | last post: by
31 posts views Thread by | last post: by
4 posts views Thread by Tony Johansson | last post: by
14 posts views Thread by Pramod | last post: by
1 post views Thread by Madhav | last post: by
2 posts views Thread by Colonel | last post: by
reply views Thread by lumer26 | last post: by
1 post views Thread by lumer26 | last post: by

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.