LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-17-2009, 10:31 AM   #1
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
C++ operator overloading - strange behavior...?


I'm from a C/Java background, new-ish to C++, and I came across a problem with operator overloading (I think) that rather puzzled me.

The basic plan is simple - a class with a string that can have bits appended to it throughout its life (as well as doing other things).

Things all go dandy when I give my string at construction-time, but when I use the default constructor, I get a weird error message. The following code snippet is a boiled-down version of my problem (but still complete & compilable):

Code:
#include <string>
#include <sstream>

using namespace std;

class A
{
public:
	A(void) : msg() { }
	A(string str) : msg(str) { }

	string msg;

	friend A& operator<<(A& a, string str);
};

A& operator<<(A& a, string str)
{
	stringstream ss (a.msg);
	ss << str;
	a.msg.assign(ss.str());
	return a;
}

int main(void)
{
	//A a();  // <- gives an error...?
	A a("cake");
	a << "cake";
}
When I compile as it is above, I get no warnings. When I compile with the 'A a();' line uncommented, and the 'A a("cake")' commented, I get the following error:

Code:
a.cpp: In function ‘int main()’:
a.cpp:29: error: invalid operands of types ‘A()’ and ‘const char [5]’ to binary ‘operator<<’
What's going on here? The types involved in 'a << "cake"' are valid when I use the string-accepting constructor, but invalid when I use the default constructor.

I'd be grateful if anyone has any ideas...???



John G



P.S. I'm using g++ version 4.4.1 on Ubuntu 9.10, if it makes any difference.
 
Old 12-17-2009, 10:50 AM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
"A a();" actually declares a function called "a" that accepts nothing and returns an A. "A a;" creates a default-constructed A called "a".

Either way, stringstreams already do exactly what you want.
 
1 members found this post helpful.
Old 12-17-2009, 11:03 AM   #3
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Original Poster
Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by tuxdev View Post
"A a();" actually declares a function called "a" that accepts nothing and returns an A. "A a;" creates a default-constructed A called "a".

Either way, stringstreams already do exactly what you want.
Thank you! I'm too used to Java's way of things, obviously!
 
Old 12-17-2009, 11:35 AM   #4
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: "North Shore" Louisiana USA
Distribution: Mint-20.1 with Cinnamon
Posts: 1,771
Blog Entries: 3

Rep: Reputation: 108Reputation: 108
Quote:
Originally Posted by tuxdev View Post
"A a();" actually declares a function called "a" that accepts nothing and returns an A. "A a;" creates a default-constructed A called "a".
May I add that A a( ); creates a forward reference to the function. Any code between this statement and the actual declaration of that function may now use expressions that call that function.

Another observation is that 'a' is a terrible name for something. How might you interpret this fragment
Code:
    SpecialString  myFunc( );
in contrast to your use of AAAAaaaaa? While I understand the desire to keep the posting short and the code fragment concise, good names are always good practice and in this case might have drawn attention to the fact that your declaration might not have been as intended.

~~~ *<;-}( )( )//
 
Old 12-17-2009, 12:34 PM   #5
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Original Poster
Rep: Reputation: 139Reputation: 139
Quote:
May I add that A a( ); creates a forward reference to the function.
Ah, okay - I had a nagging feeling you couldn't define functions within functions (which I think is a pity, but hey ).


Quote:
Another observation is that 'a' is a terrible name for something.
Well, I'd never name a class that in real life, only for condensed examples as above. However (and it's obviously just a personal thing) for short examples I kind of like using abstract names like A, B, C for things - it keeps things abstract and about the code, so you're focusing on that, not on making an excuse-implementation. It also tells people who read it that I want to show them something about the syntax/semantics of the language, and not on said implementation.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed in operator>> overloading(C++) jaji Linux - Newbie 1 10-20-2008 09:32 PM
help overloading 'new' operator c++ qwijibow Programming 1 03-07-2007 04:58 PM
c++ [] operator overloading exodist Programming 12 04-17-2004 03:06 PM
c++ overloading input operator true_atlantis Programming 4 02-25-2004 07:24 PM
Operator Overloading - Java? Tesl Programming 4 02-13-2004 12:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration