c++ - One argument constructor and assignment operator -


consider following code:

#include <iostream> using namespace std;  class a{  private:     int a; public:     a(int);     void print();     void operator =(int); };  // 1 argument constructor a::a(int b){     cout<<"inside 1 argument constructor"<<endl;     this->a=b; }  void a:: operator =(int b){     cout<<"inside operator function"<<endl;     this->a = b; }  void a::print(){     cout<<"value of ="<<a<<endl; }  int main() {      /* initialization */     obj1=2;     obj1.print();      /* assignment */     obj1=3;     obj1.print();      return 0; } 

the output of above code can seen here: http://ideone.com/0hnzub . is:

inside 1 argument constructor value of =2 inside operator function value of =3 

so have observed during initialization, one-argument constructor called during assignment, overloaded assignment operator function called. behavior enforced c++ standard, or compiler specific? quote section standard defines behavior?

this standard behavior.

i searched in latest c++ standard, iso/iec 14882:2011(e), programming language c++.

the following code

a obj1 = 2; 

is initialization. described in section 8.5 initializers, clause 16.

16 semantics of initializers follows. destination type type of object or reference being initialized , source type type of initializer expression. if initializer not single (possibly parenthesized) expression, source type not defined.

following part of clause long. reference related sample code.

— if destination type (possibly cv-qualified) class type:

— if initialization direct-initialization, or if copy-initialization cv-unqualified version of source type same class as, or derived class of, class of destination, constructors considered. applicable constructors enumerated (13.3.1.3), , best 1 chosen through overload resolution (13.3). constructor selected called initialize object, initializer expression or expression-list argument(s). if no constructor applies, or overload esolution ambiguous, initialization ill-formed.

section 13.3.1.3 initialization constructor.

accroding 8.5 , 13.3.1.3, constructor

a(int); 

is selected initialize obj1.


as second one

obj1 = 3; 

this behavior defined 5.17 assignment , compound assignment operators, clause 4.

4 if left operand of class type, class shall complete. assignment objects of class defined copy/move assignment operator (12.8, 13.5.3).

13.5.3 assignment, clause 1.

1 assignment operator shall implemented non-static member function 1 parameter. because copy assignment operator operator= implicitly declared class if not declared user (12.8), base class assignment operator hidden copy assignment operator of derived class.

function

void operator =(int); 

is selected

obj1 = 3; 

according overload rule.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -