?
Solved

How to create a c++ operator ? (like + - * )

Posted on 2010-08-27
6
Medium Priority
?
389 Views
Last Modified: 2012-05-10
Hello, I miss some bases in c++ and a thing would help me is to be able to create operator to do some operation between custom variables.
For example:
struct quote
{
  float number;
  int coeff;
}
quote var1, var2;
...
quote result=var1+var2;

Open in new window

to have the "+" multiply the number and coeff before adding the to product.

Thanks
0
Comment
Question by:Greg2FS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 33540826
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33540943
Why is this in Java?
0
 
LVL 1

Author Comment

by:Greg2FS
ID: 33540949
Because I thought it was similar in java...
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 1000 total points
ID: 33540982
Two good resources on how to do operator overloading.

"Overloading operators"
http://www.cplusplus.com/doc/tutorial/classes2/

"Operator Overloading"
http://msdn.microsoft.com/en-us/library/5tk49fh2(VS.80).aspx
0
 
LVL 32

Expert Comment

by:phoffric
ID: 33541131
I'm not sure where my previous post went. Repeating it...Here is a good resource for operator overloading:    http://www.java2s.com/Tutorial/Cpp/0200__Operator-Overloading/Catalog0200__Operator-Overloading.htmHope it sticks this time.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question