Yes it is possible to call overloaded operator with thread. Here are some of the commonly used methods.
Let check with example where we shall try to compare two objects (sObj & newSuggest) of Suggest class.
#include<iostream> #include <thread>#include<string> using namespace std; class Suggest { string m_value; public: Suggest() { cout<<"Constructor(default) :: Called"<<endl; m_value="Default Suggestion Given"; } Suggest(string str) { cout<<"Constructor(string) :: Called"<<endl; m_value=str; } //Overload == operator bool operator==(const Suggest& obj)const { if(this ->m_value == obj.m_value) { cout<<"SAME OBJECTS"<<endl; return true; } cout<<"DIFFERENT OBJECT"<<endl; return false; } void display() { cout<<"Suggestion:: "<<m_value<<endl; } }; int main() { Suggest sObj("TechSujhav"); sObj.display(); //Call Overloaded operator== from thread cout<<"\nCall Overloaded operator== from thread"<<endl; Suggest newSuggest; newSuggest.display(); std::thread objThrd2 ((&Suggest::operator==),sObj,newSuggest); objThrd2.join(); cout<<"\nCall Overloaded operator== using lambda from thread" <<endl; //Call Overloaded operator== using lambda from thread //passing argument in Capture list std::thread objThrd3 ([sObj,newSuggest](){ if(sObj == newSuggest) cout<<"Perform Activity-1"<<endl; else cout<<"Perform Activity-2" <<endl; }); objThrd3.join(); return 0; } |
Command: g++ equalToOperator.cpp -o equalToOperator -lpthread |
Output: ./equalToOperator Constructor(string) :: Called Suggestion:: TechSujhavCall Overloaded operator== from thread Constructor(default) :: Called Suggestion:: Default Suggestion Given DIFFERENT OBJECT Call Overloaded operator== using lambda from thread DIFFERENT OBJECT Perform Activity-2
|
PREV:: How to call functor from thread
Your Comments /Suggestions & Questions are always welcome.
We would like to help you with best of our knowledge.
So feel free to put Questions
No comments:
Post a Comment