Suppose there are two object : num1 , num2 each store integer number of 5 .
I want to add both object's value using non member function so result is : 10 .
But THE OUTPUT show value : 5 .
Is there any error in class member function or *this pointer ?
main.cpp
#include<iostream>
#include"Person.h"
using namespace std ;
int main(){
Person num1 ;
Person num2 ;
num1.InValueCapture(5) ;
num2.InValueCapture(5) ;
add(num1,num2); //non class member function : num1+num2 expecting result : 10
cout << "THE OUTPUT : " << num1.GetValueCapture() << endl; //but now we get 5
return 0 ;
}
Person.h
#ifndef PERSON.H
#define PERSON.H
struct Person{
private:
int value_capture ;
public:
int InValueCapture(int value){
value_capture = value ;
}
int GetValueCapture() const {
return value_capture ;
}
//adding two data
Person& combine(const Person &Rdata)
{
value_capture += Rdata.value_capture ;
std::cout << "value capture : " << value_capture <<std::endl; // use to check value inside DataSum.value_capture
return *this ;
}
};
Person add(const Person &Ldata , const Person &Rdata) //nonmember function
{
Person DataSum = Ldata;
DataSum.combine(Rdata); // add Rdata to Ldata , call class function combine
return DataSum ; //
}
#endif
Thank you for your guidance , appreciate your help .