Solved

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

Posted on 2010-08-27
6
386 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 250 total points
ID: 33540826
0
 
LVL 86

Expert Comment

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

Author Comment

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

Assisted Solution

by:evilrix
evilrix earned 250 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

728 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