Default-evaluation of objects

M

Matthias Buelow

Hi,

I'm not sure if this is possible with C++ at all but what I'd like to
have is some kind of overloaded default evaluation of a class (or the
instantiated object), that is, instead of the object value, a member
function of the object is called, and that value returned, for example,
I could make a new class for characters:

struct Char {
char c;

Char(char c_) : c(c_) {}

bool isascii();
// ...
};

Now in a program fragment, I don't want to access the member "c"
explicitly or call a getter function. What I'd like to do is the following:

...
Char ch('a');

... ch ...; // some code, where ch is to have type char
// and yield ch.c

To make it a bit more clear, hypothetically, I'd like to have a member
function "eval" (or so), that is treated specially for each object, and
that is automatically called if the object's value is to be determined:

struct Char {
char c;
...
char eval() { return c; }
...
};

so that f(ch) would be equivalent to f(ch.eval()).

Kind of like a counterpart to a constructor, only for return values.
In some cases this probably could be achieved with operator overloading
(which would be tedious) but (imho) not in the case of a function call.
Is there any way to do something like this?
 
J

Jim Langston

Matthias said:
Hi,

I'm not sure if this is possible with C++ at all but what I'd like to
have is some kind of overloaded default evaluation of a class (or the
instantiated object), that is, instead of the object value, a member
function of the object is called, and that value returned, for
example,
I could make a new class for characters:

struct Char {
char c;

Char(char c_) : c(c_) {}

bool isascii();
// ...
};

Now in a program fragment, I don't want to access the member "c"
explicitly or call a getter function. What I'd like to do is the
following:

...
Char ch('a');

... ch ...; // some code, where ch is to have type char
// and yield ch.c

To make it a bit more clear, hypothetically, I'd like to have a member
function "eval" (or so), that is treated specially for each object,
and that is automatically called if the object's value is to be
determined:

struct Char {
char c;
...
char eval() { return c; }
...
};

so that f(ch) would be equivalent to f(ch.eval()).

Kind of like a counterpart to a constructor, only for return values.
In some cases this probably could be achieved with operator
overloading (which would be tedious) but (imho) not in the case of a
function call.
Is there any way to do something like this?

operator char may be what you are looking for.

#include <iostream>

struct Char
{
Char(char c_) : c(c_) {}
operator char() { return c; }
char c;
};

void bar( char x )
{
std::cout << x << "\n";
}

int main()
{
Char ch('a');

// Outputs a
std::cout << ch << "\n";

// Outputs a
bar( ch );
}
 
M

Matthias Buelow

Jim said:
operator char may be what you are looking for.

Ah, thanks.. this is exactly what I was looking for.
JFTRC, in the general case, the hypothetical eval() function is then
operator Returntype().
 
J

Jim Langston

Matthias said:
Ah, thanks.. this is exactly what I was looking for.
JFTRC, in the general case, the hypothetical eval() function is then
operator Returntype().

Yes. operator int, operator float, operator MyClass, etc...

Be aware that these should be used with char. It is easy to get conversions
that you don't mean to and not know it. Also haveing two conversions in one
class can cause ambiguity.

Output of the following on my system is:
97
a

#include <iostream>

struct Char
{
Char(char c_) : c(c_) {}
operator char() { return c; }
operator int() { return c; }
char c;
};

void bar( char x )
{
std::cout << x << "\n";
}

int main()
{
Char ch('a');

// Outputs a
std::cout << ch << "\n";

// Outputs a
bar( ch );
}
 
J

Jim Langston

Jim said:
Yes. operator int, operator float, operator MyClass, etc...

Be aware that these should be used with char.

Er, that was supposed to say:
"Be aware that these should be used with caution."
 
M

Matthias Buelow

Jerry said:
It certainly works with all the compilers I have handy.

Hmm, indeed it does now. Maybe I had made some mistake. Or you have
triggered an inverse Heisenbug. :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top