cloning objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cody Manix

    #1

    cloning objects

    iam trying to program a copy constructor that is able to create a bitwise
    copy of its argument:

    MyClass
    {
    int a;
    // lots of different fields goes here
    float x;
    MyClass (MyClass my)
    {
    // create an exact copy of mc here
    }
    }

    normaly, object.MemberWi seClone() would be the way to go but it seems its
    useless within a constructor.
    is there another way (without copying every item manually)?

    --
    cody

    Freeware Tools, Games and Humour

    [noncommercial and no fucking ads]



  • Theo Yaung [Microsoft]

    #2
    RE: cloning objects

    Hi Cody --

    Doing so might cause some tricky behavior for a class. If you think about
    classes being able to be inherited, it's possible that the instance passed
    into the "copy constructor" is actually a subclass of that type, with
    different fields, so one can't just always make a direct copy. For
    classes, I'm pretty sure that the only option is to write a programmatic
    copy.

    It's certainly possible to make a quick copy if your data structure is a C#
    struct. Structs have certain constraints, such as they can't be extended
    by other classes or structs. In addition, they're pass by value, so making
    a copy is semantically inherent. To make a copy constructor on a struct,
    you can do it like this:

    struct MyStruct {
    int a;
    // lots of different fields goes here
    float x;
    MyStruct (MyStruct my) {
    // create an exact copy of mc here
    this = my;
    }
    }

    In struct constructors, the keyword "this" has the semantic similar to an
    "out parameter" (if you're familiar with reference/output parameters in
    C#). This is documented in the C# specification.

    If you're bent on copying all the fields in a class though, you can do it
    via Reflection.

    using System.Reflecti on;

    class MyClass {
    int a;
    // lots of different fields goes here
    float x;

    MyClass() {
    // default constructor
    }

    MyClass (MyClass my) {
    // create an exact copy of mc here
    FieldInfo[] fields = typeof(MyClass) .GetFields(Bind ingFlags.Public
    | BindingFlags.No nPublic | BindingFlags.In stance);
    foreach(FieldIn fo f in fields) {
    f.SetValue(this ,f.GetValue(oth ));
    }
    }
    }

    Something like the above should work out for you, if not feel free to reply
    to this thread on the newsgroup.

    I hope this helps,

    --Theo


    Theo Yaung
    Visual Studio .NET
    Microsoft Corp.
    --------------------[color=blue]
    >Reply-To: "Cody Manix" <deutronium@web .de>
    >From: "Cody Manix" <deutronium@web .de>
    >Subject: cloning objects
    >Date: Mon, 18 Aug 2003 14:44:28 +0200
    >
    >iam trying to program a copy constructor that is able to create a bitwise
    >copy of its argument:
    >
    >MyClass
    >{
    > int a;
    > // lots of different fields goes here
    > float x;
    >MyClass (MyClass my)
    > {
    > // create an exact copy of mc here
    > }
    >}
    >
    >normaly, object.MemberWi seClone() would be the way to go but it seems its
    >useless within a constructor.
    >is there another way (without copying every item manually)?
    >
    >--
    >cody
    >
    >Freeware Tools, Games and Humour
    >http://www.deutronium.de.vu
    >[noncommercial and no fucking ads]
    >
    >
    >
    >[/color]


    --

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Use of included script samples are subject to the terms specified at
    http://www.microsoft.com/info/cpyright.htm

    Note: For the benefit of the community-at-large, all responses to this
    message are best directed to the newsgroup/thread from which they
    originated.

    Comment

    Working...