constructor as type conversion operator

Discussion in 'C++' started by Jarek Blakarz, Oct 10, 2012.

  1. 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;
    }
     
    Jarek Blakarz, Oct 10, 2012
    #1
    1. Advertising

  2. On 10/10/2012 10:22 AM, Jarek Blakarz wrote:
    > 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
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Oct 10, 2012
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Lehn
    Replies:
    7
    Views:
    369
    Alf P. Steinbach
    Oct 31, 2005
  2. Generic Usenet Account
    Replies:
    10
    Views:
    2,246
  3. Replies:
    5
    Views:
    448
    James Kanze
    Dec 12, 2007
  4. Martin T.
    Replies:
    7
    Views:
    820
    Martin T.
    Mar 10, 2008
  5. , India
    Replies:
    2
    Views:
    478
    Fraser Ross
    Sep 15, 2009
Loading...

Share This Page