473,322 Members | 1,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

overloading ->


Hi,

I am trying to overload the ->operator as follows :
class foo {
public:
int a;
int operator->() ;

};
int foo::operator->() {

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: no*****@cis.ohio-state.edu

__________________________________________________ ______________________________
Jul 19 '05 #1
7 2319

"ranjit mario noronha" <no*****@xi.cis.ohio-state.edu> wrote in message
news:Pi************************************@xi.cis .ohio-state.edu...

Hi,

I am trying to overload the ->operator as follows :
class foo {
public:
int a;
int operator->() ;

};
int foo::operator->() {

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: no*****@cis.ohio-state.edu

__________________________________________________ __________________________
____
///

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

also, void main should be int main.
Bernie
Jul 19 '05 #2
ranjit mario noronha wrote:
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::operator->() {

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: no*****@cis.ohio-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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #3
ranjit mario noronha wrote:
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::operator->(). 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).

Jul 19 '05 #4

"Bernd Jochims" <bj******@telus.net> wrote in message
news:bj************@ID-51131.news.uni-berlin.de...

"ranjit mario noronha" <no*****@xi.cis.ohio-state.edu> wrote in message
news:Pi************************************@xi.cis .ohio-state.edu...

Hi,

I am trying to overload the ->operator as follows :
class foo {
public:
int a;
int operator->() ;

};
int foo::operator->() {

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: no*****@cis.ohio-state.edu

__________________________________________________ __________________________ ____

///

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::operator->() {
cout<<"Hullo"<<endl;
return this;
}
int main() {
foo f;
cout<<(f->a);
}
Hullo
911Press any key to continue
Jul 19 '05 #5
Ron Natalie wrote:
ranjit mario noronha wrote:
foo *f=new foo();
cout<<f->a;


This doesn't even attempt to call foo::operator->(). 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.

--
Unforgiven

"Most people make generalisations"
Freek de Jonge

Jul 19 '05 #6
#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

Jul 19 '05 #7

"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
Jul 19 '05 #8

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

Similar topics

17
by: Terje Slettebø | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
15
by: Susan Baker | last post by:
Hello everybody, I'm new to C++ (I have some C background). I've read up on this topic a few times but it just dosen't seem to be sinking in. 1. Whats the difference between overloading and...
39
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any...
45
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
31
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
15
by: lordkain | last post by:
is it possible to do some kind of function overloading in c? and that the return type is different
11
by: placid | last post by:
Hi all, Is it possible to be able to do the following in Python? class Test: def __init__(self): pass def puts(self, str): print str
10
by: Matthew | last post by:
Am I correct in thinking there is no method/function overloading of any kind in any version of PHP? Thanks, Matthew
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.