overloading ->

  • Thread starter ranjit mario noronha
  • Start date
R

ranjit mario noronha

Hi,

I am trying to overload the ->operator as follows :


class foo {
public:
int a;
int operator->() ;

};
int foo::eek:perator->() {

cout<<"Hullo"<<endl;
return 1l;
}
void main() {
foo *f=new foo();
cout<<f->a;
}

However the value of a is printed (0) and the operator function is not
called. Does anybody know what mistake I'm making ?

Thanks,
__

Ranjit

________________________________________________________________________________

Ranjit Noronha
Graduate Research Associate
Dept. of Computer and Information Sciences
The Ohio State University
Phone: (614)477-9900
E-mail: (e-mail address removed)-state.edu

________________________________________________________________________________
 
B

Bernd Jochims

ranjit mario noronha said:
Hi,

I am trying to overload the ->operator as follows :


class foo {
public:
int a;
int operator->() ;

};
int foo::eek:perator->() {

cout<<"Hullo"<<endl;
return 1l;
}
void main() {
foo *f=new foo();
cout<<f->a;
}

However the value of a is printed (0) and the operator function is not
called. Does anybody know what mistake I'm making ?

Thanks,
__

Ranjit

____________________________________________________________________________
____

Ranjit Noronha
Graduate Research Associate
Dept. of Computer and Information Sciences
The Ohio State University
Phone: (614)477-9900
E-mail: (e-mail address removed)-state.edu

____________________________________________________________________________
____

///
try changing
foo *f=new foo();
to
foo* f= new foo;

also, void main should be int main.
Bernie
 
K

Kevin Goodsell

ranjit said:
Hi,

I am trying to overload the ->operator as follows :


class foo {
public:
int a;
int operator->() ;

operator-> can't return an int. It has to return something that '->' can
be applied to (usually a pointer).
};
int foo::eek:perator->() {

cout<<"Hullo"<<endl;
return 1l;
}
void main() {

In C++, main returns int. void is not and never has been acceptable.
foo *f=new foo();
cout<<f->a;

f->a is basically equivalent to this:

(f.operator->())->a

This is why operator-> cannot return an int, and must return something
that -> can be applied to. Read about operator-> in your C++ book. It's
not like other operators.

Also, since you did not initialize the foo you allocated, nearly any use
of it (other than giving it a value) invokes undefined behavior. You
can't "use" a variable that has never been assigned a value.

And you forgot to delete f.
}

However the value of a is printed (0) and the operator function is not
called. Does anybody know what mistake I'm making ?

Thanks,
__

Ranjit

________________________________________________________________________________

Ranjit Noronha
Graduate Research Associate
Dept. of Computer and Information Sciences
The Ohio State University
Phone: (614)477-9900
E-mail: (e-mail address removed)-state.edu

________________________________________________________________________________

Think you could trim that monster down a bit? A sig with more than about
4 or 5 lines is usually considered excessive. Wouldn't hurt if you used
a correct sig marker, either ("-- " is used to indicate the start of a
signature).

-Kevin
 
R

Ron Natalie

ranjit said:
I am trying to overload the ->operator as follows :

You've got a lot of problems here.

int operator->() ;

This isn't legal. Operator-> must return something that is legal to apply
another operator -> to. An int does not meet these requirements.

Essentially, when operator-> is overloaded f->a gets interpretted
as (f.operator->())->a
void main() {

main must return int.
foo *f=new foo();
cout<<f->a;

This doesn't even attempt to call foo::eek:perator->(). You invoked the operator
on a foo*. You can't overload on pointers (only classes and enums). If you
had written:
foo f;
cout << f->a;
things might have been different (of course, with your operator-> it still won't
compile).
 
B

Bernd Jochims

Bernd Jochims said:
____________________________________________________________________________
____________________________________________________________________________
try changing
foo *f=new foo();
to
foo* f= new foo;

also, void main should be int main.
Bernie
You will see that the data member named "a" is not initialized, returning
garbage. When you forced the initialization to zero, the pointer f returned
0 the value of a..
Using VC7 the following code returns the value of the data member a.

#
#include <iostream>
using namespace std;
class foo {
foo* p;
public:
foo():a(911){}
int a;
foo* operator->() ;
};
foo* foo::eek:perator->() {
cout<<"Hullo"<<endl;
return this;
}
int main() {
foo f;
cout<<(f->a);
}
Hullo
911Press any key to continue
 
U

Unforgiven

Ron said:
This doesn't even attempt to call foo::eek:perator->(). You invoked
the operator on a foo*. You can't overload on pointers (only
classes and enums). If you had written:
foo f;
cout << f->a;
things might have been different (of course, with your operator-> it
still won't compile).

And in case you're wondering, you can in fact make a call to the operator->
if you have only a pointer to the class: dereference it first.
foo *f = new foo;
(*f)->a;

But you'll still need to fix your other mistakes.
 
R

ranjit mario noronha

#include <iostream>
using namespace std;
class foo {
foo* p;
public:
foo():a(911){}
int a;
foo* operator->() ;
};
foo* foo::eek:perator->() {
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
 
B

Bernd Jochims

ranjit mario noronha said:
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))<<" address of "<<g.vi<<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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top