constructor as type conversion operator

J

Jarek Blakarz

Hi

I can see that Int non-default constructor is called at line 3 of main "I =
c;". The "I" object has been constructed in line 1 of main.
I guess that in line 3 the temporary object is beeing created and then assigned
to "I". Am I right ?

thanks for answer.




#include <iostream>

using namespace std;

class Int {
public:
Int(void) { cout << "Default constructor" << endl; }
Int(const char &c) : i(c)
{
cout << "Int(char c), i=" << i << endl;
}

private:
int i;
};

int main(void)
{
Int I;
char c = 'P';
I = c;

return 0;
}
 
V

Victor Bazarov

I can see that Int non-default constructor is called at line 3 of main "I =
c;". The "I" object has been constructed in line 1 of main.
I guess that in line 3 the temporary object is beeing created and then assigned
to "I". Am I right ?
Yes.


thanks for answer.




#include <iostream>

using namespace std;

class Int {
public:
Int(void) { cout << "Default constructor" << endl; }
Int(const char &c) : i(c)
{
cout << "Int(char c), i=" << i << endl;
}

private:
int i;
};

int main(void)
{
Int I;
char c = 'P';
I = c;

return 0;
}

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,707
Messages
2,569,343
Members
44,635
Latest member
Matt231

Latest Threads

Top