Default-evaluation of objects

Discussion in 'C++' started by Matthias Buelow, May 28, 2008.

  1. 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?
    Matthias Buelow, May 28, 2008
    #1
    1. Advertising

  2. Matthias Buelow

    Jim Langston Guest

    Matthias Buelow wrote:
    > 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 );
    }


    --
    Jim Langston
    Jim Langston, May 28, 2008
    #2
    1. Advertising

  3. Jim Langston wrote:

    > 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().
    Matthias Buelow, May 28, 2008
    #3
  4. Matthias Buelow

    Jim Langston Guest

    Matthias Buelow wrote:
    > Jim Langston wrote:
    >
    >> 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().


    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 );
    }




    --
    Jim Langston
    Jim Langston, May 28, 2008
    #4
  5. Matthias Buelow

    Jim Langston Guest

    Jim Langston wrote:
    > Matthias Buelow wrote:
    >> Jim Langston wrote:
    >>
    >>> 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().

    >
    > 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."

    > 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 );
    > }




    --
    Jim Langston
    Jim Langston, May 28, 2008
    #5
  6. Jim Langston wrote:

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


    What about operator unsigned int()? This doesn't work, apparently.
    Matthias Buelow, May 29, 2008
    #6
  7. Matthias Buelow

    Jerry Coffin Guest

    In article <>, says...
    > Jim Langston wrote:
    >
    > >> Yes. operator int, operator float, operator MyClass, etc...

    >
    > What about operator unsigned int()? This doesn't work, apparently.


    It certainly works with all the compilers I have handy.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, May 31, 2008
    #7
  8. Jerry Coffin wrote:

    > 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. :)
    Matthias Buelow, Jun 2, 2008
    #8
    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. Fred Ma
    Replies:
    8
    Views:
    339
    Fred Ma
    Apr 13, 2004
  2. Nick Coghlan
    Replies:
    8
    Views:
    398
    Carl Banks
    Mar 10, 2005
  3. Ilias Lazaridis
    Replies:
    2
    Views:
    389
    Ilias Lazaridis
    Apr 24, 2005
  4. Ilias Lazaridis
    Replies:
    74
    Views:
    748
    Ilias Lazaridis
    Apr 4, 2005
  5. Ilias Lazaridis
    Replies:
    18
    Views:
    330
    Bill Guindon
    Apr 9, 2005
Loading...

Share This Page