473,890 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why does this work ? (operator overloading)

PKH
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
cAP.m_pcPointer->Testing(). I thought maybe it would be called with a bad
this-pointer, but it's the this-pointer is correct inside Testing(). What am
I not seeing here ?
template <class T> class TAutoPointer
{
private:
T
m_pcPointer;

public:
TAutoPointer(T t) : m_pcPointer(t) {}
~TAutoPointer() {if (m_pcPointer) delete m_pcPointer;}

T& operator -> ()
{
return m_pcPointer;
}
};
class CTest
{
int
m_nTest;

public:
CTest() : m_nTest(123) {}

void Testing()
{
int
i = 0;
}
};

typedef TAutoPointer < CTest* > CTestAutoPtr;

int main(int argc, char* argv[])
{
CTestAutoPtr
cAP(new CTest);
cAP->Testing();

return 0;
}

Jul 22 '05 #1
4 1783
PKH wrote:
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
cAP.m_pcPointer->Testing(). I thought maybe it would be called with a bad
this-pointer, but it's the this-pointer is correct inside Testing(). What
am I not seeing here ?


You're not making a difference between the operator-> and the x->y
statement. cAp->Testing() will first execute operator-> for cAp and then
call Testing on the returned pointer.
If you want to do that explicitly, you can write:

cAP.operator->()->Testing();

Jul 22 '05 #2
PKH wrote:
[snip] 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
cAP.m_pcPointer->Testing(). I thought maybe it would be called with a bad
this-pointer, but it's the this-pointer is correct inside Testing(). What am
I not seeing here ?


The returned pointer from operator-> is automatically dereferenced
for you in any case. So operator-> is a little bit special, as
it doesn't return the final 'result' of the operation, but it's
result is used for an additional operation. Same with operator*()

Without that behaviour it would not be possible to use those
operators without major syntax changes.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #3

"PKH" <no************ @online.no> schrieb im Newsbeitrag
news:19******** ************@ne ws4.e.nsc.no...
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
cAP.m_pcPointer->Testing(). I thought maybe it would be called with a bad
this-pointer, but it's the this-pointer is correct inside Testing(). What am I not seeing here ?
template <class T> class TAutoPointer
{
private:
T
m_pcPointer;

public:
TAutoPointer(T t) : m_pcPointer(t) {}
~TAutoPointer() {if (m_pcPointer) delete m_pcPointer;}

T& operator -> ()
{
return m_pcPointer;
}
};
class CTest
{
int
m_nTest;

public:
CTest() : m_nTest(123) {}

void Testing()
{
int
i = 0;
}
};

typedef TAutoPointer < CTest* > CTestAutoPtr;

int main(int argc, char* argv[])
{
CTestAutoPtr
cAP(new CTest);
cAP->Testing();

return 0;
}


The magic of operator -> !

operator -> is an unary (!) operator, and it's overloading has a very
special meaning:
an overloaded operator -> must return something to that another operator ->
can be applied.

An example for illustration:

struct S { int i; }
S s = { 4711 };

class X
{
public:
S* operator->() { return &s; }
};

class Y
{
public:
X operator->() { return X(); }
};

class Z
{
public:
Y operator->() { return Y(); }
};

Z z;
int v = z->i;

Here, v will be initialized with 4711!
Why? "z->" results to an object of type Y, which has an operator->. This
operator results to an X with an operator-> resulting an pointer S*. To this
pointer the simple C++ operator-> for pointers can be applied and gives the
value of s.i .
So
int v = z->i;
will be compiled as
int v = z.Z::operator->().Y::operat or->().X::operat or->()->i;

In your code cAP-> results to an CTest*& to which the operator -> for
pointers will be applied and call CTest::Testing( ).

Greeting from Cologne, Germany
Georg
Jul 22 '05 #4
PKH
Ok, thanks for clearing that up. I didn't know the operator worked like
that.

PKH

"Georg Krichel" <so***@no.spa m> wrote in message
news:bP******** ******@s080a106 0.group.rwe.com ...

"PKH" <no************ @online.no> schrieb im Newsbeitrag
news:19******** ************@ne ws4.e.nsc.no...
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
cAP.m_pcPointer->Testing(). I thought maybe it would be called with a
bad
this-pointer, but it's the this-pointer is correct inside Testing(). What

am
I not seeing here ?
template <class T> class TAutoPointer
{
private:
T
m_pcPointer;

public:
TAutoPointer(T t) : m_pcPointer(t) {}
~TAutoPointer() {if (m_pcPointer) delete m_pcPointer;}

T& operator -> ()
{
return m_pcPointer;
}
};
class CTest
{
int
m_nTest;

public:
CTest() : m_nTest(123) {}

void Testing()
{
int
i = 0;
}
};

typedef TAutoPointer < CTest* > CTestAutoPtr;

int main(int argc, char* argv[])
{
CTestAutoPtr
cAP(new CTest);
cAP->Testing();

return 0;
}


The magic of operator -> !

operator -> is an unary (!) operator, and it's overloading has a very
special meaning:
an overloaded operator -> must return something to that another
operator ->
can be applied.

An example for illustration:

struct S { int i; }
S s = { 4711 };

class X
{
public:
S* operator->() { return &s; }
};

class Y
{
public:
X operator->() { return X(); }
};

class Z
{
public:
Y operator->() { return Y(); }
};

Z z;
int v = z->i;

Here, v will be initialized with 4711!
Why? "z->" results to an object of type Y, which has an operator->. This
operator results to an X with an operator-> resulting an pointer S*. To
this
pointer the simple C++ operator-> for pointers can be applied and gives
the
value of s.i .
So
int v = z->i;
will be compiled as
int v = z.Z::operator->().Y::operat or->().X::operat or->()->i;

In your code cAP-> results to an CTest*& to which the operator -> for
pointers will be applied and call CTest::Testing( ).

Greeting from Cologne, Germany
Georg

Jul 22 '05 #5

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

Similar topics

18
9739
by: Joe Seigh | last post by:
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
16
2629
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
3104
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.
2
2071
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the overloading of operator<<. In the .cc of the respective class or in a file where I am overloading operator<< for all classes? Cheers,
67
8687
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used in low-level numerical computations, replacing Fortran in newer projects. Check, for example, sourceforge.net or freshmeat.net But neither language offers a primitive exp operator.
4
1762
by: Tony Johansson | last post by:
Hello!! I have done some operator overloading but my main testprogram doesn't work well. Have you any idea which of my methods are wrong? #include <iostream> #include <string> using namespace std;
6
2097
by: jay | last post by:
In the c++ primer ,i get a program. A class's name is TT,and it define the operator overload! TT first; //constructor TT second(30);//constructor TT thrid(40://constructor first=second.operator+; the question is the fourth line is all right?
5
3636
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), , ->, =. I wonder why there is such a restriction. Some tutorials say that 'new' and 'delete' can only be overloaded with static member functions, others say that all overloading function should be non-static. Then what is the fact, and why? ...
1
1510
by: Nikhil.S.Ketkar | last post by:
Hi, Does the following construct qualify as overloading on return type ? If not, in what way ? I overloaded the type conversion operator and used pointers to member functions. Its pretty straightforward but I am sure I have missed as this is not supposed to be possible. Am I misunderstanding overloading ?
0
9814
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,...
1
10903
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10449
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
7158
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
5834
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
6034
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4659
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
4257
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3266
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.