472,354 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

[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 1605
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Grant Austin | last post by:
Hi all, I'm trying to overload the '==' operator for a class, Page, that I wrote so that I can use the STL List 'find' algorithm. I'm having difficulties getting a definition that g++ likes and...
3
by: Cheng Mo | last post by:
When overriding operator new & delte of one class, the method is implicitly declared as static. However, overriding operator new & delete of template cannot be static.The compiler says cannot...
3
by: chris | last post by:
Here is my code for the call to ioctl: for ( ; ; ) { if ( (buf = malloc(len)) == NULL) errQuit("malloc error while allocating ifconf buffer"); ifc.ifc_len = len; ifc.ifc_buf = buf; if...
21
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as...
5
by: martin | last post by:
Hi, I would be extremly grateful for some help on producing an xml fragemt. The fragment that I wish to produce should look like this <Addresses> <Address>&qout;Somebody's Name&quot;...
2
by: Tom Smith | last post by:
I'm having difficulty with overloading ==, and it's making my brain melt - can you help...? What I want to have is: 1) A base class A with virtual operator== defined (no problem) 2) A class B...
6
by: Angel Tsankov | last post by:
How can an overloaded operator& take the address of its argument: template<typename T> Smth operator &(T& SomeObject) { // The address of SomeObject is needed here }
39
by: dancer | last post by:
Can somebody tell me why I get this message with the following code? Compiler Error Message: BC30452: Operator '&' is not defined for types 'String' and 'System.Web.UI.WebControls.TextBox'. ...
1
by: zaidalin79 | last post by:
I am in a JavaScript class, and we have to get all of our code to validate at the w3c website... Here is my code, it does what I want it to do which is require the user to enter the name and either...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made but the http to https rule only works for...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.