Mistakes in Operator Overloading [duplicate] - c++

This question already has answers here:
What are the basic rules and idioms for operator overloading?
(8 answers)
Why pass by const reference instead of by value?
(8 answers)
Closed 3 years ago.
I just learnt the basics of operator overloading. After which I wrote the following code for vector addition of two points in a plane.
#include <bits/stdc++.h>
using namespace std;
struct point{
int x, y;
point operator+(point b){
point c;
c.x = x + b.x;
c.y = y + b.y;
return c;
}
};
int main()
{
point a, b, c;
a.x = 1, a.y = 2, b.x = 3, b.y = 4;
c = a + b;
cout<<c.x <<" "<< c.y;
return 0;
}
However most of the other operator overloading examples I find are coded very differently, example answers to this question. Even though I am getting the correct output using this, is there a cause for concern when I do it this way by declaring a new variable, or leaving out const?

Related

operator overloading for standard type; double % double [duplicate]

This question already has answers here:
Overloading operator= for double
(2 answers)
What are the basic rules and idioms for operator overloading?
(8 answers)
Closed 2 years ago.
Im hoping to be able to overload the % operator, or really any of them, to do something particular between two double amounts.
I'm attempting to do this because I need do a check on the value obtained in an operation like the following but I don't want to use space to store it to then do the checks. My ideal code would be:
double& operator% (const double& left, const doouble right){
while(left >= right){
left -= right;
return left;
}
int main(){
(double)s1 / 30269.0 + (double)s2 / 30307.0 + (double)s3 / 30323.0 % 1;
} ^
^

C++ issue with operator overloading with regard to addition [duplicate]

This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 5 years ago.
This is my main function:
int main(){
Complex c1(1.0, 5.0);
Complex c2(3.0,-2.0);
Complex c3(1.0, 2.0);
cout << "c1 + c2 + c3 = "
<< c1 + c2 + c3 << endl;
return 0;
}
This is the function I use to add up the required numbers
Complex& operator+(const Complex & x, const Complex & y){
Complex c;
c.a = x.a + y.a;
c.b = x.b + y.b;
return c;
}
a and b are private double variables in my class.
On running my program, I seem to get the output as 1+2i ie(c3) and it only seems to work properly when I add only 2 objects. Is there any way I can tweak my existing code to allow it to work for up-to n terms?
Your operator + returns a reference to the local variable c, which gets destroyed after it is done. Any further use of the result leads to undefined behavior.
You need to return a value, so it gets copied (or a rvalue), but not a reference:
Complex operator+(const Complex & x, const Complex & y)
{...}

C vs C++ sizeof [duplicate]

This question already has answers here:
sizeof operator returns different values for c & c++?
(2 answers)
Closed 7 years ago.
I just came across this simple code snippet and am wondering why output of this program when it's compiled by a C compiler is 4 and when it's compiled by a C++ one is 8.
#include <stdio.h>
int x;
int main(){
struct x {int a; int b;};
printf("%d", sizeof(x));
return 0;
}
C++ output is rational (8 = 4 + 4 = sizeof(x.a) + sizeof(x.b)), but output of C isn't. So, how does sizeof work in C?
C : https://ideone.com/zj5Qd2
C++ : https://ideone.com/ZZ4v6S
Seems C prefers global variables over local ones. Is it right?
In C, a struct definition like struct x { int a; int b; }; does not define a type x, it defines a type struct x. So if you remove the int x; global, you'll find the C version does not compile.

How can I re-define + operator? [duplicate]

This question already has answers here:
Can we overload operators for built-in types like int or float?
(3 answers)
Closed 7 years ago.
I would like to re-define
+
operator.
So, I make a simple code like below code.
int operator+(const int &a, const int &b)
{
int temp = a-b;
return temp;
}
int main()
{
int a = 10;
int b = 5;
cout << a+b << endl;
}
I can re-define with Class type... But I don't want to use Class.
How can I resolve this issue?
You cannot define the operators for plain old data types like int.
You could build your own int class (my_int, say) and include the line #define int my_int. But this would be extremely pernicious and wouldn't work with compile-time evaluated literal expressions.
You cannot redefine operator for int (or any other built in type).
If you say, what do you want to do, we might find some solution.

How to merge coordinates into one value and return it? [duplicate]

This question already has answers here:
Returning multiple values from a C++ function
(23 answers)
Closed 7 years ago.
As above, any tricky methods to do so?
Since you cannot pass an array, I wonder how to return coordinates (x/column and y/row) to the calling object.
Regards.
There's a number of possibilities in c++ to do so, but I'm giving you a simple example:
struct coords {
int x;
int y;
};
int calculate_value_of_x(int y);
int calculate_value_of_y(int x);
coords calc_coords() {
coords c;
c.x = calculate_value_of_x(c.y);
c.y = calculate_value_of_y(c.x);
return c;
}