"ranjit mario noronha" <no*****@kappa.cis.ohio-state.edu> wrote in message
news:Pi**************************************@kapp a.cis.ohio-state.edu...
#include <iostream>
using namespace std;
class foo {
foo* p;
public:
foo():a(911){}
int a;
foo* operator->() ;
};
foo* foo::operator->() {
cout<<"Hullo"<<endl;
return this;
}
int main() {
foo f;
cout<<(f->a);
}
Hullo
911Press any key to continue
This is great !! Thanks a lot. But what I really am trying to achieve is
to figure out which element or alternatively the address of the element
which is being accessed. For example for foo->a, the operator should be
able to tell that the element a is being accesses.
Thanks for responding,
Ranjit
I'm not sure what you are try to do, but this is what I came up with.
#include <iostream>
#include <vector>
using namespace std;
class foo {
public:
int a,b;
vector<int>vi;
foo(int x = 5763,int n=911):a(n),b(x){
vi.push_back(50);
vi.push_back(18);
vi.push_back(36);
}
foo* operator->(){
cout<<"Hello from ->"<<endl;
return this;
}
int* operator ->*(int *n)
{
cout<<"hello from ->*"<<endl;
return n;
}
};
int main() {
foo f,g;
cout<<(f->a)<<" address of f.a "<<&f.a<<endl;
cout<<(f->*(&f.a))<<" address of f.a "<<&f.a<<endl;
cout<<(f->b)<<" address of f.b "<<&f.b<<endl;
cout<<(f->*(&f.b))<<" address of f.b "<<&f.b<<endl;
cout<<(g->*(&g.a))<<" address of g.a "<<&g.a<<endl;
for (size_t i=0;i<g->vi.size();i++)
cout<<(g->*(&g.vi[i]))<<" address of "<<g.vi[i]<<endl;;
}
ouput vc7
Hello from ->
911 address of f.a 0012FEB4
hello from ->*
0012FEB4 address of f.a 0012FEB4
Hello from ->
5763 address of f.b 0012FEB8
hello from ->*
0012FEB8 address of f.b 0012FEB8
hello from ->*
0012FE90 address of g.a 0012FE90
Hello from ->
hello from ->*
002F1130 address of 50
Hello from ->
hello from ->*
002F1134 address of 18
Hello from ->
hello from ->*
002F1138 address of 36
Hello from ->
Press any key to continue
Bernie