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

[q]operator & (getting address)

P: n/a
Hi All.
I will try to explain my question on following example:

Having class CInteger (wraper on 'int' values):

class CInteger
{
public:
.....
void SetValueFromPoiner(int *new_value)
{
value = *new_value;
}
.....
private:
int value;
};

This class need an operator of getting address of 'value'.
Some like this:

CInteger A(...);
CInteger B(...);

B.SetValueFromPoiner(&A); // &A => (return &A.value).

How to write this operator ?
Jul 23 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Marchello wrote:
B.SetValueFromPoiner(&A); // &A => (return &A.value).

How to write this operator ?


You cannot overload operator&. It is one of a very few operators that
cannot be overloaded.

--Phil.
Jul 23 '05 #2

P: n/a
Phil Endecott wrote:
You cannot overload operator&. It is one of a very few operators that
cannot be overloaded.


Unfortunately you can. But you shoudn't!

Jul 23 '05 #3

P: n/a


Marchello wrote:
Hi All.
I will try to explain my question on following example:

Having class CInteger (wraper on 'int' values):

class CInteger
{
public:
....
void SetValueFromPoiner(int *new_value)
{
value = *new_value;
}
....
private:
int value;
};

This class need an operator of getting address of 'value'.
Some like this:

CInteger A(...);
CInteger B(...);

B.SetValueFromPoiner(&A); // &A => (return &A.value).

How to write this operator ?


You can't overload the '&' operator. I would suggest overloading the
'SetValueFromPointer' function so that it accepts a pointer to
'CInteger'.

Hope this helps,
-shez-

Jul 23 '05 #4

P: n/a
Marchello wrote:

class CInteger
{
public:
....
void SetValueFromPoiner(int *new_value)
{
value = *new_value;
}
....
private:
int value;
};

This class need an operator of getting address of 'value'.
Some like this:

CInteger A(...);
CInteger B(...);

B.SetValueFromPoiner(&A); // &A => (return &A.value).

How to write this operator ?


You can write it as a global operator:

int *operator&(const CInteger& obj) { return &obj.value; }

or as a member:

class CInteger
{
public:
// ...
int *operator&() const { return &value; }
};

But this usually leads to problems, because once you've done it you
can't easily take the address of a CInteger object.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #5

P: n/a
Rapscallion wrote:
Phil Endecott wrote:
You cannot overload operator&. It is one of a very few operators that
cannot be overloaded.

Unfortunately you can. But you shoudn't!


I stand corrected. FAQ section 13.5.

I can imagine that very many things will stop working properly if you
do, though.

Marchello, why do you want to do this? What you are describing can be
achieved by writing B=A.

--Phil.
Jul 23 '05 #6

P: n/a
> class CInteger
{
public:
// ...
int *operator&() const { return &value; }
};
Thanks, it's really work, but without 'const' (can't convert 'const int*' to
'int*').
But this usually leads to problems, because once you've done it you
can't easily take the address of a CInteger object.


Yes, I know. But it's just a little homework (I'm a student).
Jul 23 '05 #7

P: n/a
Marchello wrote:
class CInteger
{
public:
// ...
int *operator&() const { return &value; }
};

Thanks, it's really work, but without 'const' (can't convert 'const int*' to
'int*').


Whoops... But you probably also need a non-const version.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #8

P: n/a
> You can write it as a global operator:
or as a member:
But this usually leads to problems, because once you've done it you
can't easily take the address of a CInteger object.


Question:
If operator& is defined as a member function, how do I take the address
of that object? The only way I can think of is with a wrapper class ...
but does it really become that difficult?

Samee

Jul 23 '05 #9

P: n/a
Samee Zahur wrote:
You can write it as a global operator:
or as a member:
But this usually leads to problems, because once you've done it you
can't easily take the address of a CInteger object.

Question:
If operator& is defined as a member function, how do I take the address
of that object?


Try it.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.