473,320 Members | 1,828 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,320 software developers and data experts.

An overloaded operator& needs the address of its argument

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
}

May 9 '07 #1
6 2297
"Angel Tsankov" <fn*****@fmi.uni-sofia.bgwrote in message
news:f1**********@aioe.org...
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
}
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.nlwrote in message
news:46*********************@news.xs4all.nl...
"Angel Tsankov" <fn*****@fmi.uni-sofia.bgwrote in message
news:f1**********@aioe.org...
>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
}

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.uni-sofia.bgwrote in message
news:f1**********@aioe.org...
>

--
Angel Tsankov
fn*****@fmi.uni-sofia.bg
"Sylvester Hesp" <s.****@oisyn.nlwrote in message
news:46*********************@news.xs4all.nl...
>"Angel Tsankov" <fn*****@fmi.uni-sofia.bgwrote in message
news:f1**********@aioe.org...
>>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
}

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_cast to a primitive type
on which the unary & does what you want. boost::addressof does it like that:

template<class TT* addressof(T& t)
{
return reinterpret_cast<T*>(&const_cast<char&>(reinterpre t_cast<const
volatile char&>(t)));
}

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

--
Angel Tsankov
fn*****@fmi.uni-sofia.bg
"Sylvester Hesp" <s.****@oisyn.nlwrote in message
news:46*********************@news.xs4all.nl...
>>"Angel Tsankov" <fn*****@fmi.uni-sofia.bgwrote in message
news:f1**********@aioe.org...
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
}
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_cast to a primitive type
on which the unary & does what you want. boost::addressof does it like
that:

template<class TT* addressof(T& t)
{
return reinterpret_cast<T*>(&const_cast<char&>(reinterpre t_cast<const
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...@rocketmail.comwrote:
"Sylvester Hesp" <s.h...@oisyn.nlwrote in message

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


"Angel Tsankov" <fn42...@fmi.uni-sofia.bgwrote in message
news:f1**********@aioe.org...
--
Angel Tsankov
fn42...@fmi.uni-sofia.bg
"Sylvester Hesp" <s.h...@oisyn.nlwrote in message
news:46*********************@news.xs4all.nl...
"Angel Tsankov" <fn42...@fmi.uni-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_cast to a primitive type
on which the unary & does what you want. boost::addressof does it like
that:
template<class TT* addressof(T& t)
{
return reinterpret_cast<T*>(&const_cast<char&>(reinterpre t_cast<const
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...@rocketmail.comwrote:
"Sylvester Hesp" <s.h...@oisyn.nlwrote in message

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


"Angel Tsankov" <fn42...@fmi.uni-sofia.bgwrote in message
news:f1**********@aioe.org...
--
Angel Tsankov
fn42...@fmi.uni-sofia.bg
"Sylvester Hesp" <s.h...@oisyn.nlwrote in message
news:46*********************@news.xs4all.nl...
"Angel Tsankov" <fn42...@fmi.uni-sofia.bgwrote in message
news:f1**********@aioe.org...
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
}
>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_cast to a primitive type
on which the unary & does what you want. boost::addressof does it like
that:
template<class TT* addressof(T& t)
{
return reinterpret_cast<T*>(&const_cast<char&>(reinterpre t_cast<const
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
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
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...
3
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: ...
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'. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.