473,666 Members | 2,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

An overloaded operator& needs the address of its argument

How can an overloaded operator& take the address of its argument:

template<typena me T>
Smth operator &(T& SomeObject)
{
// The address of SomeObject is needed here
}

May 9 '07 #1
6 2317
"Angel Tsankov" <fn*****@fmi.un i-sofia.bgwrote in message
news:f1******** **@aioe.org...
How can an overloaded operator& take the address of its argument:

template<typena me T>
Smth operator &(T& SomeObject)
{
// The address of SomeObject is needed here
}
It's argument is always 'this', as you can't define the unary & as a
non-member. And since 'this' is a pointer, you already have it's address :)

- Sylvester Hesp
May 9 '07 #2


--
Angel Tsankov
fn*****@fmi.uni-sofia.bg
"Sylvester Hesp" <s.****@oisyn.n lwrote in message
news:46******** *************@n ews.xs4all.nl.. .
"Angel Tsankov" <fn*****@fmi.un i-sofia.bgwrote in message
news:f1******** **@aioe.org...
>How can an overloaded operator& take the address of its
argument:

template<typen ame T>
Smth operator &(T& SomeObject)
{
// The address of SomeObject is needed here
}

It's argument is always 'this', as you can't define the unary &
as a non-member. And since 'this' is a pointer, you already
have it's address :)
Does the standard say that unary address-of operator must be a
member? If so, where?

May 9 '07 #3

"Angel Tsankov" <fn*****@fmi.un i-sofia.bgwrote in message
news:f1******** **@aioe.org...
>

--
Angel Tsankov
fn*****@fmi.uni-sofia.bg
"Sylvester Hesp" <s.****@oisyn.n lwrote in message
news:46******** *************@n ews.xs4all.nl.. .
>"Angel Tsankov" <fn*****@fmi.un i-sofia.bgwrote in message
news:f1******* ***@aioe.org...
>>How can an overloaded operator& take the address of its argument:

template<type name T>
Smth operator &(T& SomeObject)
{
// The address of SomeObject is needed here
}

It's argument is always 'this', as you can't define the unary & as a
non-member. And since 'this' is a pointer, you already have it's address
:)

Does the standard say that unary address-of operator must be a member? If
so, where?
You're absolutely right, I was mistaken.
You could take the address by using a reinterpret_cas t to a primitive type
on which the unary & does what you want. boost::addresso f does it like that:

template<class TT* addressof(T& t)
{
return reinterpret_cas t<T*>(&const_ca st<char&>(reint erpret_cast<con st
volatile char&>(t)));
}

- Sylvester Hesp
May 9 '07 #4
"Sylvester Hesp" <s.****@oisyn.n lwrote in message
news:46******** *************@n ews.xs4all.nl.. .
>
"Angel Tsankov" <fn*****@fmi.un i-sofia.bgwrote in message
news:f1******** **@aioe.org...
>>

--
Angel Tsankov
fn*****@fmi.uni-sofia.bg
"Sylvester Hesp" <s.****@oisyn.n lwrote in message
news:46******* **************@ news.xs4all.nl. ..
>>"Angel Tsankov" <fn*****@fmi.un i-sofia.bgwrote in message
news:f1****** ****@aioe.org.. .
How can an overloaded operator& take the address of its argument:

template<typ ename T>
Smth operator &(T& SomeObject)
{
// The address of SomeObject is needed here
}
It's argument is always 'this', as you can't define the unary & as a
non-member. And since 'this' is a pointer, you already have it's address
:)

Does the standard say that unary address-of operator must be a member? If
so, where?

You're absolutely right, I was mistaken.
You could take the address by using a reinterpret_cas t to a primitive type
on which the unary & does what you want. boost::addresso f does it like
that:

template<class TT* addressof(T& t)
{
return reinterpret_cas t<T*>(&const_ca st<char&>(reint erpret_cast<con st
volatile char&>(t)));
}
I must be missing something. Why wouldn't

template<class TT* addressof(T& t)
{
return &*t;
}

work?
May 10 '07 #5
On May 9, 10:02 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
"Sylvester Hesp" <s.h...@oisyn.n lwrote in message

news:46******** *************@n ews.xs4all.nl.. .


"Angel Tsankov" <fn42...@fmi.un i-sofia.bgwrote in message
news:f1******** **@aioe.org...
--
Angel Tsankov
fn42...@fmi.uni-sofia.bg
"Sylvester Hesp" <s.h...@oisyn.n lwrote in message
news:46******* **************@ news.xs4all.nl. ..
"Angel Tsankov" <fn42...@fmi.un i-sofia.bgwrote in message
news:f1****** ****@aioe.org.. .
How can an overloaded operator& take the address of its argument:
....
Does the standard say that unary address-of operator must be a member? If
so, where?
You're absolutely right, I was mistaken.
You could take the address by using a reinterpret_cas t to a primitive type
on which the unary & does what you want. boost::addresso f does it like
that:
template<class TT* addressof(T& t)
{
return reinterpret_cas t<T*>(&const_ca st<char&>(reint erpret_cast<con st
volatile char&>(t)));
}

I must be missing something. Why wouldn't

template<class TT* addressof(T& t)
{
return &*t;
}
Try it out, say, with an int:

template <intint * addressof(int& t)
{
return &*t; // Error: invalid type argument of 'unary *'
}

Greg

May 10 '07 #6
On 10 Maj, 07:02, "Jim Langston" <tazmas...@rock etmail.comwrote :
"Sylvester Hesp" <s.h...@oisyn.n lwrote in message

news:46******** *************@n ews.xs4all.nl.. .


"Angel Tsankov" <fn42...@fmi.un i-sofia.bgwrote in message
news:f1******** **@aioe.org...
--
Angel Tsankov
fn42...@fmi.uni-sofia.bg
"Sylvester Hesp" <s.h...@oisyn.n lwrote in message
news:46******* **************@ news.xs4all.nl. ..
"Angel Tsankov" <fn42...@fmi.un i-sofia.bgwrote in message
news:f1****** ****@aioe.org.. .
How can an overloaded operator& take the address of its argument:
>>template<type name T>
Smth operator &(T& SomeObject)
{
// The address of SomeObject is needed here
}
>It's argument is always 'this', as you can't define the unary & as a
non-member. And since 'this' is a pointer, you already have it's address
:)
Does the standard say that unary address-of operator must be a member?If
so, where?
You're absolutely right, I was mistaken.
You could take the address by using a reinterpret_cas t to a primitive type
on which the unary & does what you want. boost::addresso f does it like
that:
template<class TT* addressof(T& t)
{
return reinterpret_cas t<T*>(&const_ca st<char&>(reint erpret_cast<con st
volatile char&>(t)));
}

I must be missing something. Why wouldn't

template<class TT* addressof(T& t)
{
return &*t;
}

work?
First you dereference t (which means that T must either be a pointer
of implement operator *) and then you take the address of what was
returned. So if T was a normal pointer then you would return a copy of
t right?

However since the return-type is T* this does not compile for normal
pointers, nor for builtin functions. The only thing I can see this
working for is something like this:

struct Foo {
Foo& operator*() {return *this;}
};

I think you must have forgotten something in your previous post.

--
Erik Wikström

May 10 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
1702
by: Marchello | last post by:
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)
2
4580
by: David Laub | last post by:
I know there is no C# exponentiation operator. But since the double class is sealed, there seems no way to add the operator override without creating a new class which uses containment (of a double value) This seems a major pain, and would probably wind up being more syntactically messy than just calling Math.Pow(x,y) Surely greater minds than I have already wrestled with this problem...
3
6980
by: Dave Bailey | last post by:
When running the following code: if (typeCheck.Checked == true && typeList.SelectedItem == "3") { return " and wopm3 = '3' or wopm3 = '3C' or wopm3 = '3I'"; } I get the following error: Operator '&&' cannot be applied to operands of type 'bool'
39
4704
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'. <html> <head> <% @Import Namespace="System.Web.Mail" %>
0
8454
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8785
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8644
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7389
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5671
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4200
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2776
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.