LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-20-2005, 01:12 PM   #1
uman
Member
 
Registered: Sep 2004
Distribution: Gentoo
Posts: 134

Rep: Reputation: 15
C++ operator +=


Hi, is this the proper way to implement the += operator?

Code:
Scene& Scene::operator +=(Scene param)
{
	*this=*this+param;
	return *this;
}
Thanks for the help!
 
Old 02-20-2005, 04:37 PM   #2
lyle_s
Member
 
Registered: Jul 2003
Distribution: Slackware
Posts: 392

Rep: Reputation: 55
That's pretty close:
Code:
Scene& Scene::operator+=(Scene const& param)
{
 return *this = *this + param;
}
By passing param in by reference, you're prefering pass-by-reference to pass-by-value (see Effective C++, Item 22.) By making that reference a const one, you're using const whenever possible (see Effective C++, Item 21.) which greatly contributes to being const correct (see http://www.parashift.com/c++-faq-lit...rrectness.html.)

This assumes that you have operator+ for Scenes defined which actually takes care of the addition.

Lyle

Last edited by lyle_s; 02-20-2005 at 06:05 PM.
 
  


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
operator precedence in C Majjj Programming 2 07-07-2004 11:48 AM
shift operator linuxanswer Programming 1 05-01-2004 07:33 PM
c++ [] operator overloading exodist Programming 12 04-17-2004 03:06 PM
~ operator linuxanswer Programming 7 04-08-2004 04:56 PM
what's the opposite of operator!() jhorvath Programming 5 11-02-2003 08:21 PM

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

All times are GMT -5. The time now is 09:56 PM.

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