Home > Back-end >  For help on C output operator overloading
For help on C output operator overloading

Time:09-23

To write such a test program

The header file:


 # # ifndef C_TEST_BASICCONCEPTTEST_H 
# define C_TEST_BASICCONCEPTTEST_H
# include & lt; Iostream>
The class CAL {
Public:
Friend STD: : ostream & amp; Operator<(STD: : ostream & amp; OS, const CAL & amp; The print);

Friend CAL operator + (const CAL & amp; Left, const CAL & amp; Right);

CAL ()=default;

CAL (int left, int right) : member1_ (left), member2_ (right) {}

CAL (int right) : CAL (3, right) {}

CAL (const CAL & amp; Another) : member1_ (another member1_), member2_ (another. Member2_) {}

~ CAL ()=default;
Cal& The operator + (const cal& Right);

CAL & amp; Operator=(const CAL & amp; Right);

Private:
Int member1_;
Int member2_;
};
The class CAL & amp; CAL: : operator + (const class CAL & amp; Right) {
STD: : cout & lt; <"Member function + invoked" & lt; This - & gt; Member1_ +=right. Member1_;
This - & gt; Member2_ +=right. Member2_;
return *this;
}

The class CAL & amp; CAL: : operator=(const class CAL & amp; Right) {
This - & gt; Member1_=right. Member1_;
This - & gt; Member2_=right. Member2_;
return *this;
}
STD: : ostream& The operator & lt; <(STD: : ostream& OS, const cal& The print) {
OS & lt; <"Member1_ :" & lt;
}
The class CAL operator + (const class CAL & amp; Left, const class CAL & amp; Right) {
STD: : cout & lt; <"The friend function + invoked" & lt; CAL temp.
The temp. Member1_=left. Member1_ + right. Member1_;
The temp. Member2_=left. Member2_ + right. Member2_;
return temp;
}
# endif//C_TEST_BASICCONCEPTTEST_H

This is the source file
 
# include "basicConceptTest. H"

Int main (int arg c, char * * argv) {

CAL data1 (1, 2), data2 (6), data3 (data1);
STD: : cout & lt; <"Data1:" & lt; STD: : cout & lt; <"Data2:" & lt; STD: : cout & lt; <"Data3:" & lt; STD: : cout & lt; Data3=data1 data2 +;//(1, 2) + (3, 6)=(4, 8) after that data1 changed to (4, 8)
STD: : cout & lt; <"Data1:" & lt; STD: : cout & lt; <"Data2:" & lt; STD: : cout & lt; <"Data3:" & lt; STD: : cout & lt; Data3=operator + (data1, data3);//(4, 8) + (4, 8)=(8, 16)
STD: : cout & lt; <"Data1:" & lt; STD: : cout & lt; <"Data2:" & lt; STD: : cout & lt; <"Data3:" & lt; STD: : cout & lt; Data2=data1. Operator + (data3);
STD: : cout & lt; <"Data1:" & lt; STD: : cout & lt; <"Data2:" & lt; STD: : cout & lt; <"Data3:" & lt; STD: : cout & lt;
return 0;
}

Perform and found everything is normal, in line with expectations

So did a little change

 void test (void); 
Int main (int arg c, char * * argv) {
test();

return 0;
}
Void test (void)
{
CAL data1 (1, 2), data2 (6), data3 (data1);
STD: : cout & lt; <"Data1:" & lt; STD: : cout & lt; <"Data2:" & lt; STD: : cout & lt; <"Data3:" & lt; STD: : cout & lt; Data3=data1 data2 +;//(1, 2) + (3, 6)=(4, 8) after that data1 changed to (4, 8)
STD: : cout & lt; <"Data1:" & lt; STD: : cout & lt; <"Data2:" & lt; STD: : cout & lt; <"Data3:" & lt; STD: : cout & lt; Data3=operator + (data1, data3);//(4, 8) + (4, 8)=(5, 10)
STD: : cout & lt; <"Data1:" & lt; STD: : cout & lt; <"Data2:" & lt; STD: : cout & lt; <"Data3:" & lt; STD: : cout & lt; Data2=data1. Operator + (data3);
STD: : cout & lt; <"Data1:" & lt; STD: : cout & lt; <"Data2:" & lt; STD: : cout & lt; <"Data3:" & lt; STD: : cout & lt; }


Something strange happened, error Process finished with exit code 139 (interrupted by 11: signal SIGSEGV), I tried to use the debug mode to run again, found that can run normally again. Not with release patterns. Works just fine, and the main function in a function inside have a problem, how to return a responsibility?

Then checked the code, just detection, I wrote the operator overloading function has a problem:

 STD: : ostream& The operator & lt; <(STD: : ostream& OS, const cal& The print) {
OS & lt; <"Member1_ :" & lt;
}


No returns an output stream

In a return statement after

 
STD: : ostream& The operator & lt; <(STD: : ostream& OS, const cal& nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related