Conversation operator as a lval?

Discussion in 'C++' started by Matt, Jan 31, 2005.

  1. Matt

    Matt Guest

    Does C++ provide a means to use a conversion operator as a lval?

    ie, I want:

    class superInt
    {
    public:
    operator int() { return this->value; }
    private:
    int value;
    };

    ....to instead be this:

    class superInt
    {
    public:
    int& operator int() { return this->value; }
    private:
    int value;
    };

    ....which will not compile.

    I suspect the C++ language designers avoided this on purpose, but I
    thought I would ask here just in case there is a means to do this (in
    some other way then I present above).

    Such a thing would save me time re-writing a bunch of overloaded
    operators for superInt (among other things).

    -Matt
    --
    Remove the "downwithspammers-" text to email me.
     
    Matt, Jan 31, 2005
    #1
    1. Advertising

  2. Matt wrote:
    > Does C++ provide a means to use a conversion operator as a lval?\


    Sure.

    > ie, I want:
    >
    > class superInt
    > {
    > public:
    > operator int() { return this->value; }


    Should probably be

    operator int() const { return this->value; }

    > private:
    > int value;
    > };
    >
    > ...to instead be this:
    >
    > class superInt
    > {
    > public:
    > int& operator int() { return this->value; }


    operator int&() { return this->value; }

    > private:
    > int value;
    > };
    >
    > ...which will not compile.


    Of course.

    >
    > I suspect the C++ language designers avoided this on purpose,


    No, they didn't.

    > but I
    > thought I would ask here just in case there is a means to do this (in
    > some other way then I present above).


    Very good choice.

    > Such a thing would save me time re-writing a bunch of overloaded
    > operators for superInt (among other things).


    But it is rather dangerous. Be careful.

    Victor
     
    Victor Bazarov, Jan 31, 2005
    #2
    1. Advertising

  3. Matt

    Matt Guest

    On Mon, 31 Jan 2005 12:01:17 -0500, Victor Bazarov
    <> wrote:
    >
    > operator int&() { return this->value; }


    Great, I'm glad I asked here. Thanks!

    >
    >> Such a thing would save me time re-writing a bunch of overloaded
    >> operators for superInt (among other things).

    >
    >But it is rather dangerous. Be careful.


    Yes, I can see where it can be quite dangerous. I think I will avoid
    it whenever I can...especially since I've already written all my
    overloaded operators for the task at hand.

    -Matt
    --
    Remove the "downwithspammers-" text to email me.
     
    Matt, Jan 31, 2005
    #3
    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. Olaf Petzold

    type conversation problems

    Olaf Petzold, Sep 24, 2005, in forum: VHDL
    Replies:
    8
    Views:
    886
    Mike Treseler
    Sep 30, 2005
  2. Olaf Petzold

    type conversation problems

    Olaf Petzold, Nov 20, 2005, in forum: VHDL
    Replies:
    2
    Views:
    5,252
    Ajeetha
    Nov 21, 2005
  3. Kevin Buchan
    Replies:
    2
    Views:
    549
    Kevin Buchan
    Jan 22, 2004
  4. Mr. Berserker

    Really Weird Conversation

    Mr. Berserker, Jul 23, 2003, in forum: C Programming
    Replies:
    11
    Views:
    550
    Chris Torek
    Jul 29, 2003
  5. Replies:
    2
    Views:
    388
    Steve Holden
    Aug 31, 2004
Loading...

Share This Page