Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: constructing an object from bytes rather than a constructor

Reply
Thread Tools

Re: constructing an object from bytes rather than a constructor

 
 
Mike Wahler
Guest
Posts: n/a
 
      07-30-2003
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



 
Reply With Quote
 
 
 
 
Novice
Guest
Posts: n/a
 
      08-01-2003
"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


 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      08-01-2003


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

 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      08-01-2003
"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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Read STDIN as bytes rather than a string Jason Friedman Python 0 06-18-2012 11:53 PM
Re: Read STDIN as bytes rather than a string Jason Friedman Python 0 06-18-2012 11:49 PM
Re: Read STDIN as bytes rather than a string Benjamin Kaplan Python 0 06-18-2012 11:18 PM
Read STDIN as bytes rather than a string Jason Friedman Python 0 06-18-2012 11:13 PM
Why is top-level an object rather than just Object? John Mair Ruby 12 10-23-2010 05:44 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57