Copy Constructor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bhadrisn
    New Member
    • Jun 2007
    • 3

    Copy Constructor

    Hello,

    I have a class A. I have a copy constructor inside that class.

    From the main function i am calling the copy constructor like below.


    A a;
    A b = a;

    Now whenever the compiler comes to the statement A b=a. how does this work.

    My doubt is whether the object b will be created first and a will be copied to b?

    Regards,
    Bhadri
  • Girish Kanakagiri
    New Member
    • May 2007
    • 93

    #2
    Originally posted by bhadrisn
    Hello,

    I have a class A. I have a copy constructor inside that class.

    From the main function i am calling the copy constructor like below.


    A a;
    A b = a;

    Now whenever the compiler comes to the statement A b=a. how does this work.

    My doubt is whether the object b will be created first and a will be copied to b?

    Regards,
    Bhadri
    From your statements

    A a;
    A b = a;

    It is clear that object a is created first. In the second statement object b is created by passing object "a" as a parameter to the copy constructor. It is converted in this fashion A b(a);

    If you don't want this automatic conversion, then supply keyword "explict" for
    the constructor so that it stops from such automatic conversions.

    If you think, I have answered to your question then don't forget to close this
    thread.

    Regards,
    Girish.

    Comment

    • bhadrisn
      New Member
      • Jun 2007
      • 3

      #3
      Originally posted by Girish Kanakagiri
      From your statements

      A a;
      A b = a;

      It is clear that object a is created first. In the second statement object b is created by passing object "a" as a parameter to the copy constructor. It is converted in this fashion A b(a);

      If you don't want this automatic conversion, then supply keyword "explict" for
      the constructor so that it stops from such automatic conversions.

      If you think, I have answered to your question then don't forget to close this
      thread.

      Regards,
      Girish.
      Hello,

      Thanks for your reply.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        This is not correct:
        Originally posted by Girish Kanakagiri
        If you don't want this automatic conversion, then supply keyword "explict" for
        the constructor so that it stops from such automatic conversions.
        The example is an explicit conversion. Explicit means that onlt the programmer can call the constructor aand i this example the programmer has called the constructor.

        Explicit constructors means that the compiler can't call the constructor. You use explicit constructors when you don't want he compiler calling constructors.

        Comment

        • bhadrisn
          New Member
          • Jun 2007
          • 3

          #5
          Originally posted by weaknessforcats
          This is not correct:


          The example is an explicit conversion. Explicit means that onlt the programmer can call the constructor aand i this example the programmer has called the constructor.

          Explicit constructors means that the compiler can't call the constructor. You use explicit constructors when you don't want he compiler calling constructors.

          In what scenarios EXPLICIT keyword should be used. ????

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Suppose you have a container with a constructor that has an int argument that is intended to create a container of a specified size:

            [code=cpp]
            MyContainer<int > obj(100);
            [/code]

            Let's suppose this contstructor creates room for 100 ints.

            Then suppose you have a function like this:

            [code=cpp]
            void fx(MyContainer< int> arg)
            {

            };
            [/code]

            If you call this function with an int. The MyContainer constructor will be called and an empty container will be passed to the function.

            [code=c]
            fx(1234); //creates a MyContainer with room for 1,234 ints.
            [/code]

            The above is the implicit call to the constructor. In fact, this is an error. The 1234 should not be used with fx(). So, you make the MyContainer constructor explicit and then you will get a compile error on fx(1234).

            Comment

            Working...