CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Unhappy Question about using constructor to do type conversion.

    Hi, everyone!


    I have a sample about using constructor to do type conversion.
    In my sample, I change an int to type "A". I just want to learn
    more deeply into this topic -- using constructor to do type
    conversion. Are there some online materials that I can make a
    reference?

    BTW: Is the topic in Bjarne Stroustrup's "The C++ Programming language
    special edition"? I can not find that topic in this book.


    Here is my example:

    --------
    #include <iostream.h>

    class A {

    private:
    int a;

    public:
    A (int a)
    {
    this->a = a;
    }

    void output()
    {
    cout << "a is: " << this->a << endl;
    }
    };

    void functionA (A a)
    {
    a.output();
    }

    int main()
    {
    functionA (10);
    return 1;
    }
    --------


    Thanks in advance,
    George

  2. #2
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    Yes, a constructor like that will be used for implicit conversions unless it is declared explicit.
    Code:
    class A
    {
    public:
         A():i_(0){}
         A(int i):i_(i){}
    private:
         int i_;
    };
    
    class B
    {
    public:
         B():i_(0){}
         explicit B(int i):i_(i){}
    private:
         int i_;
    };
    
    int main()
    {
          A a = 1; // ok (1)
          B b = 1; // error  (2)
         return 0;
    }
    On the line marked (1) the integer 1 is implicitely converted to a temporary A using A's second ctor. Then the implicit copy ctor of A is used to initialize a.

    On the line marked (2) a temporary B object cannot be created, because the ctor is declared explicit, thus the error.

    Take care when using ctors to do implicit conversions (and also when writing cast operators) because they can lead to subtle bugs.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  3. #3
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Talking

    Thanks, Gabriel buddie!

    George

    Originally posted by Gabriel Fleseriu
    Yes, a constructor like that will be used for implicit conversions unless it is declared explicit.
    Code:
    class A
    {
    public:
         A():i_(0){}
         A(int i):i_(i){}
    private:
         int i_;
    };
    
    class B
    {
    public:
         B():i_(0){}
         explicit B(int i):i_(i){}
    private:
         int i_;
    };
    
    int main()
    {
          A a = 1; // ok (1)
          B b = 1; // error  (2)
         return 0;
    }
    On the line marked (1) the integer 1 is implicitely converted to a temporary A using A's second ctor. Then the implicit copy ctor of A is used to initialize a.

    On the line marked (2) a temporary B object cannot be created, because the ctor is declared explicit, thus the error.

    Take care when using ctors to do implicit conversions (and also when writing cast operators) because they can lead to subtle bugs.

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    To expand on Gabriel's answer:

    The point about using "explicit" on a ctor is just that - if you want the type conversion, then you have to be explicit about it:
    Code:
        B b = B(1);   // OK
    or:
    Code:
    void f(A a);
    
    void g(B b);
    
    int main()
    {
        f(1); // OK, but a potential bug
        g(B(1)); // Explicit type conversion, so less likely to cause a bug.
    }
    Note that "explicit" is only of value if the ctor can be called with a single argument. If it has no arguments, or it needs two or more, then it can't be involved in implicit type conversion.
    Code:
    class foo
    {
    public:
        foo();
        explicit foo(int);
        explicit foo(char c = 'a', int i); // could be called with single char arg.
        foo(int, int);
    };
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  5. #5
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    Originally posted by Graham
    Code:
    class foo
    {
    public:
        foo();
        explicit foo(int);
        explicit foo(char c = 'a', int i); // could be called with single char arg.
        foo(int, int);
    };
    Am I wrong assuming that the parameters that have a default value must come last (or, the other way around, those that don't have default values first)?
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  6. #6
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Talking

    Thanks, Graham buddie!

    Your experience is valuable to me!


    regards,
    George

    Originally posted by Graham
    To expand on Gabriel's answer:

    The point about using "explicit" on a ctor is just that - if you want the type conversion, then you have to be explicit about it:
    Code:
        B b = B(1);   // OK
    or:
    Code:
    void f(A a);
    
    void g(B b);
    
    int main()
    {
        f(1); // OK, but a potential bug
        g(B(1)); // Explicit type conversion, so less likely to cause a bug.
    }
    Note that "explicit" is only of value if the ctor can be called with a single argument. If it has no arguments, or it needs two or more, then it can't be involved in implicit type conversion.
    Code:
    class foo
    {
    public:
        foo();
        explicit foo(int);
        explicit foo(char c = 'a', int i); // could be called with single char arg.
        foo(int, int);
    };

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured