[q]operator & (getting address)

M

Marchello

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 ?
 
P

Phil Endecott

Marchello said:
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.
 
S

Shezan Baig

Marchello said:
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-
 
P

Pete Becker

Marchello said:
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.
 
P

Phil Endecott

Rapscallion said:
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.
 
M

Marchello

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

Pete Becker

Marchello said:
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.
 
S

Samee Zahur

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,591
Members
45,100
Latest member
MelodeeFaj
Top