469,658 Members | 1,810 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,658 developers. It's quick & easy.

[q]operator & (getting address)

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
9 1499
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
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


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

Similar topics

3 posts views Thread by Grant Austin | last post: by
3 posts views Thread by Cheng Mo | last post: by
3 posts views Thread by chris | last post: by
21 posts views Thread by siliconwafer | last post: by
5 posts views Thread by martin | last post: by
2 posts views Thread by Tom Smith | last post: by
39 posts views Thread by dancer | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.