Re: constructing an object from bytes rather than a constructor

Discussion in 'C++' started by Mike Wahler, Jul 30, 2003.

  1. Mike Wahler

    Mike Wahler Guest

    Novice <> wrote in message
    news:bgckl0$693$...
    > Hi all, has there been any research into creating a method (no pun

    intended)
    > for doing exact bit copies of objects without invoking a copy constructor

    of
    > an object?


    No need. If you don't define a copy ctor, one
    will be synthesized for you, which does a member-
    wise assignment.

    >
    > For example:
    >
    > class A {
    > int foo;
    > public:
    > A (int foo_):foo(foo_){}
    > ...
    > };
    >
    > A a1(42);
    > file://note I'm aware there is no clone function in the standard namespace
    > file://that creates an exact bit copy of an object - this is just an

    example of
    > how it might work
    > A a2 = std::clone(a1);
    >
    > I don't know enough about how flexible C++ can be in terms of writing a
    > function like this


    And how how this be superior to a copy ctor?

    >I mean I guess it would have to make use of the (void *)
    > type if it didn't make use of templates -


    Eh? type 'void*' and templates have nothing to do with
    your question.

    >but then if it did (make use of
    > templates) - perhaps it would look like this:
    >
    > A a2 = std::clone<A>(a1);
    >
    > Then however, I definitely don't know enough C++ to construct an object

    from
    > its bytes,


    You don't need to unless you need a 'deep copy' etc.
    It happens automatically in a default copy ctor.

    >but I guess the function would look something like this:
    >
    > namespace std{
    > template <class A>
    > A clone (const A &a){
    > file://copy each byte of A into a new A object
    > }
    > }
    >
    > I'm sure this idea has been discussed before -


    I doubt it. I don't see you pointing out any need
    that hasn't been addressed by the language.

    >is probably implemented in
    > the std and if not has definitely been done by some third party - but I
    > wasn't able to find any information on it (though I only spent a minute on
    > google).
    >
    > Can anyone tell me about this sort of functionality and where I can find
    > more information on it?


    A copy constructor creates a new object with the same
    'value' as an existing one. Read about copy constructors
    in a good C++ textbook.

    -Mike
    Mike Wahler, Jul 30, 2003
    #1
    1. Advertising

  2. Mike Wahler

    Novice Guest

    "Mike Wahler" <> wrote in message
    news:bgcmnd$ira$...
    | Novice <> wrote in message
    | news:bgckl0$693$...
    | > Hi all, has there been any research into creating a method (no pun
    | intended)
    | > for doing exact bit copies of objects without invoking a copy
    constructor
    | of
    | > an object?
    |
    | No need. If you don't define a copy ctor, one
    | will be synthesized for you, which does a member-
    | wise assignment.
    |
    | >
    | > For example:
    | >
    | > class A {
    | > int foo;
    | > public:
    | > A (int foo_):foo(foo_){}
    | > ...
    | > };
    | >
    | > A a1(42);
    | > file://note I'm aware there is no clone function in the standard
    namespace
    | > file://that creates an exact bit copy of an object - this is just an
    | example of
    | > how it might work
    | > A a2 = std::clone(a1);
    | >
    | > I don't know enough about how flexible C++ can be in terms of writing a
    | > function like this
    |
    | And how how this be superior to a copy ctor?

    It would remove the need to write a copy constructor for each class that you
    write - as you point out the need for a deep copy of an object.

    |
    | >I mean I guess it would have to make use of the (void *)
    | > type if it didn't make use of templates -
    |
    | Eh? type 'void*' and templates have nothing to do with
    | your question.

    I'm really bad with communicating (and C++ as the my nick name implies) and
    if makes you feel better about yourself - you are smarter than me. I'm dumb
    and hardly have the brain activity to maintain my autonomic bodily
    functions - yes I said it - not you. Do you feel better about yourself?
    I'm simply making an inquiry into the language - I think there is a need
    for this - perhaps I didn't convey that clearly I will attempt to do it
    again.

    (void *) a function that makes use of (void *) does not require the type to
    be specified at compile type - thus this would be useful for a function that
    could return an object of any type.

    Templates would be useful because you could do the following as I originally
    posted:

    namespace std{
    template <class A>
    A clone (const A &a){
    //copy each byte of A into a new A object
    }
    }

    then call it like this:
    A a2 = std::clone<A>(a1);

    [snip]


    | You don't need to unless you need a 'deep copy' etc.
    | It happens automatically in a default copy ctor.
    |


    Yes - well there you go - that would be a case in which it could be useful.

    [snip]

    |
    | I doubt it. I don't see you pointing out any need
    | that hasn't been addressed by the language.

    A method of creating deep copies without copy constructors

    [snip]

    |
    | A copy constructor creates a new object with the same
    | 'value' as an existing one. Read about copy constructors
    | in a good C++ textbook.

    I will take this suggestion.

    Look, I'm aware you know more than me about C++ - as the nickname implies -
    I'm a novice at C++ - I'm simply wondering if this idea has been addressed.
    Is it even possible?

    I'm also rushed for time so I'm sorry if I took your post in the wrong way.

    I will do some more reading on copy constructors - perhaps I will be able to
    find my answers there.

    Novice
    Novice, Aug 1, 2003
    #2
    1. Advertising

  3. Novice wrote:
    >
    >
    > | You don't need to unless you need a 'deep copy' etc.
    > | It happens automatically in a default copy ctor.
    > |
    >
    > Yes - well there you go - that would be a case in which it could be useful.
    >
    > [snip]
    >
    > |
    > | I doubt it. I don't see you pointing out any need
    > | that hasn't been addressed by the language.
    >
    > A method of creating deep copies without copy constructors


    I think you don't understand the differences between when a copy
    constructor is needed and when it is not needed. Search for
    'rule of three' on the web or in FAQ's. They will show you why,
    when and how you need to write a copy constructor and an assignment
    operator.

    The point is: if you only need a memberwise copy, then don't write a
    copy constructor on your own, the compiler will do it. Of course,
    if the memberwise copy is indistinguishable from a bitwise copy, the
    compilers optimizer might replace the memberwise copy with a bitwise
    copy. But this none of your business. Leave such optimizations to
    the compiler. It makes much less errors in doing so.

    But if a memberwise copy is not good enough, a standard clone function
    would be of no help. If it were, then others would have noticed it
    years ago and compilers would have implemented it. It is in the nature
    of such cases, that they must be tailored to the exact internals of
    the class. Thus a one-size-fits-all clone function would be of no help.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Aug 1, 2003
    #3
  4. Mike Wahler

    Gavin Deane Guest

    "Novice" <> wrote in message news:<bgcnc8$80v$>...
    > "Mike Wahler" <> wrote in message
    > news:bgcmnd$ira$...
    > | Novice <> wrote in message
    > | news:bgckl0$693$...
    > | > Hi all, has there been any research into creating a method (no pun
    > intended)
    > | > for doing exact bit copies of objects without invoking a copy
    > constructor
    > of
    > | > an object?
    > |
    > | No need. If you don't define a copy ctor, one
    > | will be synthesized for you, which does a member-
    > | wise assignment.
    > |
    > | >
    > | > For example:
    > | >
    > | > class A {
    > | > int foo;
    > | > public:
    > | > A (int foo_):foo(foo_){}
    > | > ...
    > | > };
    > | >
    > | > A a1(42);
    > | > file://note I'm aware there is no clone function in the standard
    > namespace
    > | > file://that creates an exact bit copy of an object - this is just an
    > example of
    > | > how it might work
    > | > A a2 = std::clone(a1);
    > | >
    > | > I don't know enough about how flexible C++ can be in terms of writing a
    > | > function like this
    > |
    > | And how how this be superior to a copy ctor?
    >
    > It would remove the need to write a copy constructor for each class that you
    > write - as you point out the need for a deep copy of an object.


    <snip>

    >
    > | You don't need to unless you need a 'deep copy' etc.
    > | It happens automatically in a default copy ctor.
    > |
    >
    >
    > Yes - well there you go - that would be a case in which it could be useful.
    >
    > [snip]
    >
    > A method of creating deep copies without copy constructors


    I think you missed Mike's point [1]. I'm certain you misread Mike's
    attitude, but that happens sometimes in this sort of faceless
    communication. Not anyone's fault, it just happens.

    A clone function couldn't be of any use for deep copies. For example,
    if your class has a pointer member, there is no way for clone() to
    know how large a chunk of memory it points to. So clone() can't do a
    deep copy. If you want a deep copy, you have to write it yourself
    because only you know exactly how / whether your class remembers how
    much memory it has allocated.

    If you want a shallow copy (such that the pointer members of the
    source and destination both hold the same value hence point to the
    same place, and all other members of the source and destination end up
    identical too) then that is precisely what the compiler generated copy
    ctor does for you so you don't need clone().

    hth
    GJD

    [1] Alternatively, I might have missed Mike's point. In which case,
    the views expressed herein are mine alone and Mike is free to disagree
    if he wishes :)
    Gavin Deane, Aug 1, 2003
    #4
    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. John Mair
    Replies:
    12
    Views:
    186
    Intransition
    Oct 23, 2010
  2. Jason Friedman

    Read STDIN as bytes rather than a string

    Jason Friedman, Jun 19, 2012, in forum: Python
    Replies:
    0
    Views:
    238
    Jason Friedman
    Jun 19, 2012
  3. Benjamin Kaplan

    Re: Read STDIN as bytes rather than a string

    Benjamin Kaplan, Jun 19, 2012, in forum: Python
    Replies:
    0
    Views:
    263
    Benjamin Kaplan
    Jun 19, 2012
  4. Jason Friedman
    Replies:
    0
    Views:
    243
    Jason Friedman
    Jun 19, 2012
  5. Jason Friedman
    Replies:
    0
    Views:
    236
    Jason Friedman
    Jun 19, 2012
Loading...

Share This Page