4

Actually i didn't understand the concept of it, meaning that why and when it should be employed. Usually, we can assign values to an instance of a class but why we should send an object to another object like this :

    private void button8_Click(object sender, EventArgs e)
    {
        rectangle r1 = new rectangle(50, 70);
        rectangle r2 = new rectangle(r1);
    }



class rectangle
{
    private int length;
    private int breadth;


    public rectangle(rectangle x)
    {
        length = x.length;
        breadth = x.breadth;
        MessageBox.Show("I'm a Copy Constructor. \n Length= " + length.ToString() + "\n breadth= " + breadth.ToString());

    }
}
BJ Myers
  • 6,617
  • 6
  • 34
  • 50
behnam
  • 1,095
  • 2
  • 11
  • 30
  • It´s a simple way to copy your rectangle. From the outside this is not possible because your properties are private. Also you could modify your rectangle or something like that. – Fruchtzwerg Feb 27 '16 at 20:29
  • Well, I may have seen it used in some libraries but that's not a common sight in C#. Copy constructors are the obvious cornerstone of C++ programming, but in C# they are nothing more than a fancy(but proper) way to organize [deep cloning](http://stackoverflow.com/questions/129389/how-do-you-do-a-deep-copy-an-object-in-net-c-specifically)( at least that what I would expect from it). – Eugene Podskal Feb 27 '16 at 20:35
  • Does this answer your question? [Copy constructor versus Clone()](https://stackoverflow.com/questions/3345389/copy-constructor-versus-clone) – Michael Freidgeim Dec 22 '22 at 10:43

2 Answers2

1

This is a possible approach to fully or partially initialize an object copying members' values from the source object.

In your case, you can call this as mapping objects.

Alternatively, rectangle class could implement a static method like CreateFrom that would work as the current constructor and it would also work like a factory method:

class rectangle
{
    private int length;
    private int breadth;

    public static rectangle CreateFrom(rectangle x)
    {
        rectangle r = new rectangle();
        r.length = x.length;
        r.breadth = x.breadth;

        return r;
    }
}

...and maybe this way you find your event handler code more interesting:

private void button8_Click(object sender, EventArgs e)
{
    rectangle r1 = new rectangle(50, 70);
    rectangle r2 = rectangle.CreateFrom(r1);
}
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
1

A copy constructor is a concept from borrowed from C++: the only purpose of this constructor is to make a copy from the existing object. Additionally, the compiler defines the copy constructor in C++ automatically (implicit copy constructor) if the programmer does not provide one. And you can mix: for classes with special copy behaviour, you provide the copy constructor. For other classes, the implicit copy constructor calls your customized one, if the class has members of this type.

Summary: the copy constructor in C++ is a simple way to copy objects and it members. The trick is that you don't have to provide one explicitly and it is supported at compiler level.

As far as I know, there are no implicit copy constructors in C#. Instead of this special constructors, you can use cloning in C#. See this question, for example.

Another note: structs are copied on assigment, but not recursive. Even there is no implicit copy constructor, its only a simple memory copy.

For C++ copy constructors, see section 12.8, page 288 in the C++14 standard for example.

Community
  • 1
  • 1
ventiseis
  • 3,029
  • 11
  • 32
  • 49