c++ - Why parameter can be passed to constructor by assignment operator at object initialization? -


class {     a(int b); } main(){     int b=5;     tempa=b; } 

how work?

i came across problem in hw?

can help? thanks.

a constructor 1 shown in question called conversion constructor.

it allows compiler take value of argument type , convert instance of class.

in example, line

a = b; 

is equal to

a = a(5); 

(well, exception of conflict of having both class , variable named a)

if want disallow it, have make constructor explicit

class { public:     explicit a(int); }; 

then compiler can not use constructor conversions 1 described above. have explicitly use constructor.


Comments

Popular posts from this blog

javascript - Confirm a form & display message if form is valid with JQuery -

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

ionic framework - Meteor - Error: Failed to execute 'insertBefore' on 'Node' -