overloading ->()

L

lnitzu

Hello everybody,

I'm not sure what I am doing wrong but can anybody explain why the
overloaded operator is never called?

Here is the sample:

#include <iostream>
using namespace std;



class CTest
{
public:
void AFunction(){cout<<"A Function\n";}
CTest* operator->()
{
cout <<"Operator ->() was accessed\n";
return this;
}
};

int main()
{

CTest *t;
t->AFunction();
return 0;
}

The result is only :
AFunction

Operator ->() was accessed is never desplayed.

Thanks a million....
 
G

Gerry Ford

Hello everybody,

I'm not sure what I am doing wrong but can anybody explain why the
overloaded operator is never called?

Here is the sample:

#include <iostream>
using namespace std;



class CTest
{
public:
void AFunction(){cout<<"A Function\n";}
CTest* operator->()
{
cout <<"Operator ->() was accessed\n";
return this;
}
};

int main()
{

CTest *t;
t->AFunction();
return 0;
}

The result is only :
AFunction

Operator ->() was accessed is never desplayed.

Thanks a million....
iostream.h

May I ask what is your platform?
 
A

Alf P. Steinbach

* (e-mail address removed):
Hello everybody,

I'm not sure what I am doing wrong but can anybody explain why the
overloaded operator is never called?

Here is the sample:

#include <iostream>
using namespace std;



class CTest
{
public:
void AFunction(){cout<<"A Function\n";}
CTest* operator->()
{
cout <<"Operator ->() was accessed\n";
return this;
}
};

int main()
{

CTest *t;
t->AFunction();
return 0;
}

The result is only :
AFunction

Operator ->() was accessed is never desplayed.

You cannot overload operators for raw pointers.

Use "CTest t", not "CTest* t".

Apart from that, the program above has Undefined Behavior, and so could
do anything or nothing, because the pointer is used without having been
initialized.


Cheers, & hth.,

- Alf
 
L

lnitzu

* (e-mail address removed):












You cannot overload operators for raw pointers.

Use "CTest t", not "CTest* t".

Apart from that, the program above has Undefined Behavior, and so could
do anything or nothing, because the pointer is used without having been
initialized.

Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Yes, sorry, I forgot to "new" the object *t=new CTest().
What do you mean by raw pointers? Thank you very much.
 
G

Gerry Ford

It's windows...does this matter?
It matters to my thesis that linux users are oblivious of filetypes.

I believe you need
#include <iostream.h>
, and it wouldn't hurt to
#include <stdio.h>
 
I

Ian Collins

*Please don't quote signatures*
Yes, sorry, I forgot to "new" the object *t=new CTest().
What do you mean by raw pointers? Thank you very much.

Overloaded operators are called on objects, not pointers. You can't
overload operators for pointers.
 
C

Christopher

It matters to my thesis that linux users are oblivious of filetypes.

I believe you need
#include <iostream.h>
, and it wouldn't hurt to
#include <stdio.h>


You couldn't be more wrong.
<iostream> provides std::cin and std::cout.
stdio.h is not needed. If it was, it would be <cstdlib>

I believe you have made the wrong assumption about whom is oblivious.
It seems you thought everyone else was mistaken when it was indeed
yourself. ...Oh, and I am a Windows user unless forced for pay to use
*nix :)

Your confusion lies in the common misunderstanding of _standard C++
library headers_ , because college professors and text books fail to
use the proper headers all too often, or worse use C code and call it C
++.

Any book that makes use of <iostream.h> and has C++ in the title
should be burned.
Any professor who makes use of <iostream.h> and is teaching C++ should
be slapped and fired.

http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.4
 
J

James Kanze

Which, of course, is completely wrong. Neither Windows nor Unix
supports filetypes at the system level. Both use a naming
convention hack. In fact, both use exactly the same naming
convention hack, in exactly the same way.
You couldn't be more wrong.
<iostream> provides std::cin and std::cout.
stdio.h is not needed. If it was, it would be <cstdlib>

Or <stdio.h>. One of the most frequent reasons for including
stdio is C compatibility, and if C compatibility is important in
I believe you have made the wrong assumption about whom is
oblivious. It seems you thought everyone else was mistaken
when it was indeed yourself. ...Oh, and I am a Windows user
unless forced for pay to use *nix :)
Your confusion lies in the common misunderstanding of
_standard C++ library headers_ , because college professors
and text books fail to use the proper headers all too often,
or worse use C code and call it C ++.

There's some of that. There's also history.
Any book that makes use of <iostream.h> and has C++ in the
title should be burned.

I certainly hope not. Some of the best and most important C++
books around use <iostream.h>. (The Barton and Nackman comes to
mind.) All it means is that they were written some time ago.
And while you shouldn't use such older text for learning the
language itself, books like Barton and Nackman are still very
pertinent today.
 
G

Gerry Ford

Ian Collins said:
Nonsense, <stdio.h> is the C standard I/O header.
Yikes!

While I might have been 100% wrong on this thread, I've now leafed through
my 1998 c++ reference, and think my sins were incidental to changes in the
syntax. Does anyone have a link for a gratis copy of the standard? Is wiki
up to date and informative with respect to c++?
 
I

Ian Collins

Gerry said:
Yikes!

While I might have been 100% wrong on this thread, I've now leafed through
my 1998 c++ reference, and think my sins were incidental to changes in the
syntax. Does anyone have a link for a gratis copy of the standard? Is wiki
up to date and informative with respect to c++?
Nope, iostreams have never been in the C standard library headers.
"classical" iostreams were declared in the deprecated header <iostream.h>

There isn't a legal gratis copy of the standard.
 
G

Gerry Ford

Ian Collins said:
Gerry Ford wrote:
Nope, iostreams have never been in the C standard library headers.
"classical" iostreams were declared in the deprecated header <iostream.h>

There isn't a legal gratis copy of the standard.
I'm not a lawyer, Ian.

I think you're confusing legal with legal.

Anyways, is wiki a good reference?
 
G

Gerry Ford

Ian Collins said:
and I don't condone copyright theft. If you want the standard, buy a
copy, it's not expensive.

Whatever. They never asked for permission to take my syntax before
returning it. I understand why there's a fee. My money this month is going
to get my wood-burning running. It's cold. Ergo, my budget for documents
that put food in Keith Thompson's fridge is ZERO.
Which one, there's thousands!
Don't know. Thought you might.
 
B

Bo Persson

Gerry said:
Whatever. They never asked for permission to take my syntax before
returning it. I understand why there's a fee. My money this month
is going to get my wood-burning running. It's cold. Ergo, my
budget for documents that put food in Keith Thompson's fridge is
ZERO.

Who's Keith? :)

If you mean Ken Thompson, he and Bjarne and K&R don't make any money
from your books, because they wrote it as part of their jobs. It's all
copyright Bell Labs and AT&T.


The Standard documents are copyright ISO, and the sales goes to paying
for its administration.


Bo Persson
 

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

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top