Topic: Please Help overloading ->
Author: xwu@ucrengr.ucr.edu (xing wu)
Date: 23 May 93 19:39:38 GMT Raw View
Hi, Can anyone posting a sample code for C++
overloading "->" operator.
I wrote the following code, but it doesn't work.
------------------------------------------------------------------
Class A : public base
{
int id;
B *p;
}
Class B : public base
{
int id;
}
class base
{
public :
operator->(base *p);
}
A *a;
B *b;
a->p = b; // this doesn't call operator->()
a->b; // but this line woundn't compile
_________________________________________________________________
Please forward posting to xwu@ucrengr.ucr.edu also if possible.
Thanks.
Xingwu
Author: jimad@microsoft.com (Jim Adcock)
Date: 25 May 93 14:19:35 GMT Raw View
In article <31501@galaxy.ucr.edu> xwu@ucrengr.ucr.edu (xing wu) writes:
>Hi, Can anyone posting a sample code for C++
>overloading "->" operator.
#include <stdio.h>
class A
{
public:
int id;
A(int i) : id(i) {}
};
class APtr
{
A* aptr;
public:
APtr(A* pa) : aptr(pa) {}
A* operator->() const { printf("(%lX) ->", (long)aptr); return aptr;}
A& operator*() const { printf("*(%lX)", (long)aptr); return *aptr;}
A& operator[](int i) c
const { printf("(%lX)[%d]", (long)aptr, i); return aptr[i];}
};
main()
{
A a(123);
APtr p(&a);
printf(" id = %d\n", p->id);
printf(" . id = %d\n", (*p).id);
printf(" . id = %d\n", p[0].id);
return 0;
}
Author: grumpy@cbnewse.cb.att.com (Paul J Lucas)
Date: Tue, 25 May 1993 16:45:48 GMT Raw View
Author: lmcropa@noah.ericsson.se (Roxsan Payette)
Date: 26 May 93 16:33:32 GMT Raw View
In article 31501@galaxy.ucr.edu, xwu@ucrengr.ucr.edu (xing wu) writes:
>class base
>{
>public :
> operator->(base *p);
>}
>
>A *a;
>B *b;
>
>a->p = b; // this doesn't call operator->()
>a->b; // but this line woundn't compile
>
Try this:
(*a)->p;
An operator binds to an object, not a pointer to an object (I guess).
---
Roxsan Payette lmcropa@lmc.ericsson.se
Ericsson Communication Inc (LMC/U/SF) Tel. (514) 738-8300 #2508
All opinions are my own. Fax. (514) 345-7982