Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Conversation operator as a lval?

Reply
Thread Tools

Conversation operator as a lval?

 
 
Matt
Guest
Posts: n/a
 
      01-31-2005
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.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-31-2005
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
 
Reply With Quote
 
 
 
 
Matt
Guest
Posts: n/a
 
      01-31-2005
On Mon, 31 Jan 2005 12:01:17 -0500, Victor Bazarov
<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
forwarding "sections" of a conversation to someone on the outside (not in the original conversation) Joebrazeau@gmail.com Computer Support 5 08-19-2006 01:34 PM
type conversation problems Olaf Petzold VHDL 2 11-21-2005 02:37 PM
type conversation problems Olaf Petzold VHDL 8 09-30-2005 04:45 PM
Is there some URL prefix that can launch a MSN Messenger Conversation? Kevin Buchan ASP .Net 2 01-22-2004 05:32 PM
one way voice conversation Justin Cisco 0 12-14-2003 10:26 PM



Advertisments