C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: constructing an object from bytes rather than a construc

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Mike Wahler
Guest





PostPosted: Wed Jul 30, 2003 3:32 am    Post subject: Re: constructing an object from bytes rather than a construc Reply with quote



Novice <cassidy (AT) cs (DOT) queensuDOTca> wrote

Quote:
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.

Quote:

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?

Quote:
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.

Quote:
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.

Quote:
but I guess the function would look something like this:

namespace std{
template 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.

Quote:
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




Back to top
Alf P. Steinbach
Guest





PostPosted: Fri Aug 01, 2003 3:27 am    Post subject: Re: constructing an object from bytes rather than a construc Reply with quote



On Thu, 31 Jul 2003 22:55:47 -0400, "Novice" <cassidy (AT) cs (DOT) queensuDOTca> wrote:

Quote:
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?

You get that automatically for a POD (Plain Old Datatype)
struct, via the assignment operator.

For other types it's not very meaningful.

If you absolutely must, e.g. for very lowlevel storage,
then you can std::memcpy. But keep in mind that it's the
most abused function in the C/C++ library. When you see
a call of this function you know that either (1) the code
is extremely low-level, dealing with e.g. marshaling,
memory allocation or some such, or (2) it was written by an
incompetent.


Back to top
Novice
Guest





PostPosted: Fri Aug 01, 2003 3:42 am    Post subject: Re: constructing an object from bytes rather than a construc Reply with quote



"Mike Wahler" <mkwahler (AT) mkwahler (DOT) net> wrote

Quote:
Novice <cassidy (AT) cs (DOT) queensuDOTca> wrote in message
news:bgckl0$693$1 (AT) knot (DOT) queensu.ca...
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.

Quote:

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]


Quote:
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]

Quote:

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]

Quote:

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



Back to top
Karl Heinz Buchegger
Guest





PostPosted: Fri Aug 01, 2003 8:54 am    Post subject: Re: constructing an object from bytes rather than a construc Reply with quote



Novice wrote:
Quote:


| 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
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
Gavin Deane
Guest





PostPosted: Fri Aug 01, 2003 1:24 pm    Post subject: Re: constructing an object from bytes rather than a construc Reply with quote

"Novice" <cassidy (AT) cs (DOT) queensuDOTca> wrote

Quote:
"Mike Wahler" <mkwahler (AT) mkwahler (DOT) net> wrote in message
news:bgcmnd$ira$1 (AT) slb0 (DOT) atl.mindspring.net...
| Novice <cassidy (AT) cs (DOT) queensuDOTca> wrote in message
| news:bgckl0$693$1 (AT) knot (DOT) queensu.ca...
| > 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>

Quote:

| 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 :-)

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.