473,378 Members | 1,469 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 1684
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.