Call to a constructor while executing (obj < 3) . Kindly explain?
-
class CA{
private:
int m_Var;
public:
CA(int x=0): m_Var(x){cout<<"1";}
bool operator < (const CA &obj2) { cout<<"2"; return (m_Var < obj2.m_Var) ? true : false;}
};
int main(void) {
CA obj1=2;
if(obj1 < 3){cout << "In If block" << endl;}
}the output of this code is 112In If block .. i can understand the reason behind the first 1 and "2In If block" .. i dont understand when the second 1 is being called? as per my understanding when
(obj1<3)
executes it may callobj1.operator<(3)
But constructor is being called but whats makes it to call? kindly explain why the second 1 is being displayed? -
class CA{
private:
int m_Var;
public:
CA(int x=0): m_Var(x){cout<<"1";}
bool operator < (const CA &obj2) { cout<<"2"; return (m_Var < obj2.m_Var) ? true : false;}
};
int main(void) {
CA obj1=2;
if(obj1 < 3){cout << "In If block" << endl;}
}the output of this code is 112In If block .. i can understand the reason behind the first 1 and "2In If block" .. i dont understand when the second 1 is being called? as per my understanding when
(obj1<3)
executes it may callobj1.operator<(3)
But constructor is being called but whats makes it to call? kindly explain why the second 1 is being displayed?Member 3974347 wrote:
as per my understanding when (obj1<3) executes it may call obj1.operator<(3)
Not actually. It is actually called like obj1.operator<( CA(3));// A local temporary object is created here. Here the integer 3 is automatically converted to a CA object by involking the constructor. Such conversions are called implicit conversions. You can prevent such conversions using the explicit keyword[^]
-
class CA{
private:
int m_Var;
public:
CA(int x=0): m_Var(x){cout<<"1";}
bool operator < (const CA &obj2) { cout<<"2"; return (m_Var < obj2.m_Var) ? true : false;}
};
int main(void) {
CA obj1=2;
if(obj1 < 3){cout << "In If block" << endl;}
}the output of this code is 112In If block .. i can understand the reason behind the first 1 and "2In If block" .. i dont understand when the second 1 is being called? as per my understanding when
(obj1<3)
executes it may callobj1.operator<(3)
But constructor is being called but whats makes it to call? kindly explain why the second 1 is being displayed?Member 3974347 wrote:
But constructor is being called but whats makes it to call?
Because constructor accepts a
int
.Member 3974347 wrote:
as per my understanding when (obj1<3) executes it may call obj1.operator<(3)
Your operator overload accepts
const CA &
but you specified the expressionobj1<3
, since type CA has a constructor which takes int, a temporary will be created by passing 3 to the constructor ofCA
and this is the reason for the second time constructor call. Useexplicit
keyword to avoid the confusion. :)Navaneeth How to use google | Ask smart questions
-
Member 3974347 wrote:
as per my understanding when (obj1<3) executes it may call obj1.operator<(3)
Not actually. It is actually called like obj1.operator<( CA(3));// A local temporary object is created here. Here the integer 3 is automatically converted to a CA object by involking the constructor. Such conversions are called implicit conversions. You can prevent such conversions using the explicit keyword[^]
You are so fast! :-D
Navaneeth How to use google | Ask smart questions
-
You are so fast! :-D
Navaneeth How to use google | Ask smart questions
-
Member 3974347 wrote:
as per my understanding when (obj1<3) executes it may call obj1.operator<(3)
Not actually. It is actually called like obj1.operator<( CA(3));// A local temporary object is created here. Here the integer 3 is automatically converted to a CA object by involking the constructor. Such conversions are called implicit conversions. You can prevent such conversions using the explicit keyword[^]
Thanks. :)
-
Member 3974347 wrote:
But constructor is being called but whats makes it to call?
Because constructor accepts a
int
.Member 3974347 wrote:
as per my understanding when (obj1<3) executes it may call obj1.operator<(3)
Your operator overload accepts
const CA &
but you specified the expressionobj1<3
, since type CA has a constructor which takes int, a temporary will be created by passing 3 to the constructor ofCA
and this is the reason for the second time constructor call. Useexplicit
keyword to avoid the confusion. :)Navaneeth How to use google | Ask smart questions
Thanks :)