473,804 Members | 2,787 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 1780
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
9726
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
2623
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
3100
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
2070
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
8673
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
1760
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
2090
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
3634
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
1507
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
9705
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
10323
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
9138
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...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5516
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
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4292
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
3813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2988
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.