By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 1,002 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

Conversation operator as a lval?

P: n/a
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.
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Jul 23 '05 #2

P: n/a
On Mon, 31 Jan 2005 12:01:17 -0500, Victor Bazarov
<v.********@comAcast.net> 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.
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.