473,687 Members | 3,230 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How does dereference operator overloading really work?

Is there a good write on this. The textbooks I have fluff over
on this? Specifically, I trying to dereference with 2 levels
of type conversion not 1, i.e.

X<T> -> z => Y<T> -> z => T* -> z
and
*X<T> => *Y<T> => *T
The Y<T> conversion has to be done as an expression temp. It cannot be done
inside a method body for X.

I think I sort of had it working for the -> operator but the same
technique doesn't work for the * operator. The * appears to getting
consumed.

Joe Seigh
Jul 19 '05 #1
18 9700
Joe Seigh wrote:
Is there a good write on this. The textbooks I have fluff over
on this? Specifically, I trying to dereference with 2 levels
of type conversion not 1, i.e.

X<T> -> z => Y<T> -> z => T* -> z
and
*X<T> => *Y<T> => *T
The Y<T> conversion has to be done as an expression temp. It cannot be done
inside a method body for X.

I think I sort of had it working for the -> operator but the same
technique doesn't work for the * operator. The * appears to getting
consumed.


I don't really get what you're trying to do. Maybe posting a chunk o
code that mostly compiles and show us what you'd like to do ?

Jul 19 '05 #2
Joe Seigh wrote:
Is there a good write on this. The textbooks I have fluff over
on this?
Uh... Did somebody swap the '?' and '.' keys on your keyboard?
Specifically, I trying to dereference with 2 levels
of type conversion not 1, i.e.

X<T> -> z => Y<T> -> z => T* -> z
and
*X<T> => *Y<T> => *T
The Y<T> conversion has to be done as an expression temp. It cannot be done
inside a method body for X.

I think I sort of had it working for the -> operator but the same
technique doesn't work for the * operator. The * appears to getting
consumed.


I'm sorry, I have absolutely no idea what you are asking.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #3


Gianni Mariani wrote:

Joe Seigh wrote:
Is there a good write on this. The textbooks I have fluff over
on this? Specifically, I trying to dereference with 2 levels
of type conversion not 1, i.e.

X<T> -> z => Y<T> -> z => T* -> z
and
*X<T> => *Y<T> => *T
The Y<T> conversion has to be done as an expression temp. It cannot be done
inside a method body for X.

I think I sort of had it working for the -> operator but the same
technique doesn't work for the * operator. The * appears to getting
consumed.


I don't really get what you're trying to do. Maybe posting a chunk o
code that mostly compiles and show us what you'd like to do ?


T * X<T>::operator ->();

gives one level of conversion. I don't want that. Something like
Y<T> X<T>::operator ->();
T * Y<T>::operator ->();
I need a Y<T> temp generated. Strictly speaking, I would think no
overloading of -> should be needed. Type conversion should be sufficient
if I have a conversion path from X<T> to T*.

Joe Seigh
Jul 19 '05 #4
Joe Seigh wrote in news:3F******** *******@xemaps. com:
Is there a good write on this. The textbooks I have fluff over
on this? Specifically, I trying to dereference with 2 levels
of type conversion not 1, i.e.

X<T> -> z => Y<T> -> z => T* -> z
and
*X<T> => *Y<T> => *T
The Y<T> conversion has to be done as an expression temp. It cannot
be done inside a method body for X.

I think I sort of had it working for the -> operator but the same
technique doesn't work for the * operator. The * appears to getting
consumed.


We really need to know what you are trying to do, otherwise
all you can expect is guess, If you havent got any code to show
perhapse give an example of the code whant to write and what you
want it to actually do.

Anyway here's my punt:
#include <iostream>
#include <ostream>

template < typename T > struct thing;
template < typename T >
struct ptr_holder
{
T *temp;

T *operator -> () const { return temp; }

ptr_holder( T *arg ) : temp( arg )
{
thing< T >::aquire( temp );
}

~ptr_holder()
{
thing<T>::relea se( temp );
}
};

template < typename T >
struct ref_holder
{
T *temp;

operator T &() const { return *temp; }

ref_holder( T *arg ) : temp( arg )
{
thing< T >::aquire( temp );
}

~ref_holder()
{
thing<T>::relea se( temp );
}
};
template < typename T >
struct thing
{
T *data;

static void aquire( T * data )
{
std::cout << "aquire( " << (void *)data << " )\n";
}
static void release( T * data )
{
std::cout << "release( " << (void *)data << " )\n";
}

ptr_holder< T > operator -> () const
{
return ptr_holder< T >( data );
}

ref_holder< T > operator * () const
{
return ref_holder< T >( data );
}

thing( T & ref ) : data( &ref ) {}

};

void function( int &arg )
{
std::cout << "function( " << arg << " )\n";
}

struct X
{
int y;
X( int zz ) : y( zz ) {}
};

void function( X & x )
{
std::cout << "function( X( " << x.y << " )& )\n";
}

int main()
{
int a = 1;
thing< int > at( a );
function ( *at );

X x( *at );
thing< X > xt( x );
std::cout << "inline: " << xt->y << "\n";
function( *xt );
}
Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #5
Joe Seigh wrote:

T * X<T>::operator ->();

gives one level of conversion. I don't want that. Something like
Y<T> X<T>::operator ->();
T * Y<T>::operator ->();
I need a Y<T> temp generated.
Why would you need that? I suppose you could do something like this:

T* X<T>::operator->()
{
return T<T>()->something;
}

But I can't think of any reason to need a temporary.
Strictly speaking, I would think no
overloading of -> should be needed. Type conversion should be sufficient
if I have a conversion path from X<T> to T*.


I still don't quite understand. If you want conversion from X<T> to T*,
just define operator T*().

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #6


Kevin Goodsell wrote:
I need a Y<T> temp generated.
Why would you need that? I suppose you could do something like this:

T* X<T>::operator->()
{
return T<T>()->something;
}

But I can't think of any reason to need a temporary.


It's for a smart pointer. Temps are local. If I get a local temp
copy of the smart pointer generated, I can guarantee that the reference
count will not go to zero for the duration of the expression. I can't
make that guarantee if T* is generated directly.
Strictly speaking, I would think no
overloading of -> should be needed. Type conversion should be sufficient
if I have a conversion path from X<T> to T*.


I still don't quite understand. If you want conversion from X<T> to T*,
just define operator T*().


That didn't appear to work. I had a ctor Y<T>::Y(X<T>) and a
Y<T>::operator T*() defined.

Joe Seigh
Jul 19 '05 #7

Kevin Goodsell wrote:
I need a Y<T> temp generated.


Why would you need that? I suppose you could do something like this:

T* X<T>::operator->()
{
return T<T>()->something;
}

But I can't think of any reason to need a temporary.


It's for a smart pointer. Temps are local. If I get a local temp
copy of the smart pointer generated, I can guarantee that the reference
count will not go to zero for the duration of the expression. I can't
make that guarantee if T* is generated directly.


Though I suppose I could just not define -> for X:: and force using a Y ctor
explicitly.

struct thing {
int z;
};

X<thing> p;

p->z; // error, not allowed
(Y<thing>(p))->z; // instead
I did sort of get it working but when I did the * operator, I got
*p doesn't work but **p does, so I'm not sure I know what is going
on anymore. To be consistent, p->z would have to not work but
p->->z would.

Joe Seigh
Jul 19 '05 #8
Joe Seigh wrote:

It's for a smart pointer. Temps are local. If I get a local temp
copy of the smart pointer generated, I can guarantee that the reference
count will not go to zero for the duration of the expression. I can't
make that guarantee if T* is generated directly.


That makes it a bit more clear, but still not totally clear. Do you
think you could post a very simple (but complete, so we can
copy-paste-compile) program that demonstrates this problem?

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #9
"Joe Seigh" <js*******@xema ps.com> wrote in message
news:3F******** *******@xemaps. com...
[...]
T * X<T>::operator ->();

gives one level of conversion. I don't want that. Something
like
Are you sure you understand how smart pointers work?
Typically, operator->() just returns the underlying pointer,
and the compiler dereferences that pointer to get at the
actual member. Given the operator above, there should
be no problem with your later syntax:

struct thing {
int z;
};

X<thing> p;

p->z; // error, not allowed

The above should be fine.

(Y<thing>(p))->z; // instead

Not sure where this comes from.
[...]
I need a Y<T> temp generated.
Why? Does the temp c'tor or d'tor have side effects? That
would be very peculiar indeed.
Strictly speaking, I would think no overloading of -> should
be needed.
[...]


If you are writing a smart pointer, overloading operator->
is one of the most important things you need to do. But
why write YASP (Yet Another Smart Pointer)? Do take
a look at Loki::SmartPtr before reinventing the wheel for
the Nth time.

Dave
Jul 19 '05 #10

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

Similar topics

16
2602
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class, but I would like to use internally my own = operator for some of my value types, so I can say "x = y;" rather than "x.Assign(y);". The op_Assign operator seems impossibly broken since it takes __value copies of the two objects. Perhaps there is...
16
3087
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class has two int memebers "dataA", "dataB". The derived class has an additional int member "dataC". I am simply trying to overload the + operator so that 'adding' two objects will sum up the corresponding int members.
4
1773
by: PKH | last post by:
This is a part of an autopointer template i'm using. I originally had a Get() member that returned the pointer, but thought it would nice to be able to use the -> operator. The question is why does the following line work, and call cAP.m_pcPointer->Testing() ? cAP->Testing(); // ??? cAP-> returns a CTest*, so it looks to me like the line reads cAP.m_pcPointer <whitespace> Testing(), but it actually does
51
3577
by: Jojo | last post by:
Is there any way to get to the left-hand side of an operator? Consider the following (this is not meant to be perfect code, just an example of the problem): class Matrix { public: int data; Matrix() {}
6
5410
by: TuxC0d3 | last post by:
Hi! I'm diving into the some more ++ specific aspects of c++ (and finally accepting that c++ is more than "a plus added to c" :), so that means using namespaces, templates, std::strings, lists, vectors, operator overloading and what not.. And i was wondering if there is a way to override the global dereference operator, so to be able to check the address that one tries to dereference. This gives the ability to throw an exception when...
14
468
by: Bit byte | last post by:
Came accross the ff code: double array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; std::vector<double> values(array, array + 9); //<- is this a 2D vector ?
2
2916
by: allan.mcrae | last post by:
I am having trouble with overloading the += operator when template parameters are used. I have a class holding an array (called "derived" in the following example) which derives from a base class ("base"). I want to be able to add: 1) any derived array holding class to any other derived array holding class 2) any derived array holding class to a literal value (e.g int, double, etc) for which addition is defined for the type in the...
6
3544
by: Bill foust | last post by:
I'm running into a situation there I think an operator overload would solve the issue, but I'm unable to make it work for some reason. If anyone can help here I would appreciate it. I have a base class that is common to many other classes. public class Base .... end class I have 2 seperate classes that inherit from base
22
3611
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float v);
0
8590
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
8528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9072
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8947
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
8786
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
7621
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
5807
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();...
1
2964
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
3
1950
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.