C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 15th, 2003, 06:01 PM
homncruse homncruse is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 14 homncruse User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Overloading << and >> operators (syntax)

How do I go about coding that in the .h and .cpp files? Currently I have it all in the .h file, but this is causing problems (although the overloading works I think - doesn't give any compile errors) with other parts of the program. I was wondering how the syntax would go as far as declaring the function declaration in the .h file, but putting the actual code in the .cpp?

Last edited by homncruse : May 15th, 2003 at 10:17 PM.

Reply With Quote
  #2  
Old May 18th, 2003, 05:01 PM
pschmerg pschmerg is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: Blacksburg VA/Philly PA
Posts: 38 pschmerg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 29 m
Reputation Power: 6
Send a message via AIM to pschmerg
Re: Overloading << and >> operators (syntax)

Quote:
Originally posted by homncruse
How do I go about coding that in the .h and .cpp files? Currently I have it all in the .h file, but this is causing problems (although the overloading works I think - doesn't give any compile errors) with other parts of the program. I was wondering how the syntax would go as far as declaring the function declaration in the .h file, but putting the actual code in the .cpp?

this is pretty easily done, but the overloaded << and >> cannot be members of the class, they have to be declared friends of it. So anywhere outside of the class just declare

friend ostream& operator(ostream&, cont Object&);


then in the cpp file just provide the implementation

Reply With Quote
  #3  
Old May 18th, 2003, 08:58 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,587 Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 21 h 21 m 5 sec
Reputation Power: 997
>> but the overloaded << and >> cannot be members of the class, they have to be declared friends of it.
Depends on what you're doing with the overloaded operators. It is entirely possible for you to declare an overloaded << without declaring it as a friend function. For instance, in the following example, I've overloaded << to add a number and return the resulting int value. BTW homncruse, this answers your question as well :) :
Code:
myint.h
======
class MyInt {
private:
  int val;
public:
  MyInt(int init);
  int operator <<(int val2);
};


myint.C
======
#include <iostream>
#include "myint.h"
using namespace std;

MyInt::MyInt(int init) {
  val = init;
}

int MyInt::operator<< (int val2) {
  val += val2;
  return val;
}

int main(void) {
  int x;
  MyInt foo(4);
  x = foo << 2;

  cout << x << endl;
  return 0;
}

Reply With Quote
  #4  
Old May 19th, 2003, 01:03 PM
homncruse homncruse is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 14 homncruse User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for the help, I've got the overloading working now, but I'm having a problem with pointers
To keep subject lines consistent with subjects (so we don't go "off-topic") I'll post that problem in a new thread here

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Overloading << and >> operators


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT