From: pfultz2 on
How would I go about writing a bitwise copy constructor for a class? I
would like to call memcpy() before any constructors are called on the
members. I cant seem to find any information on this.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Daniel Krügler on
On 20 Mrz., 09:26, pfultz2 <pful...(a)yahoo.com> wrote:
> How would I go about writing a bitwise copy constructor for a class? I
> would like to call memcpy() before any constructors are called on the
> members. I cant seem to find any information on this.

It is not really clear to me, what you
question is - a more specific use-case
would help. Are you asking for the
syntax to realize that? Or are you asking
how you can do that without provoking
undefined behavior (UB)?

The life-time and trivial type rules of
C++ put rather severe restrictions on
class types (I assume your class contains
members of class-type, because otherwise
your remark about constructors doesn't
make sense to me), so simply performing
memcpy on a std::string within or without
a constructor will rather easy lead
to UB land.

What are you going to achieve and why
isn't a normal copy constructor
sufficient?

Greetings from Bremen,

Daniel Kr�gler


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: pfultz2 on
On Mar 20, 3:18 pm, Daniel Kr�gler <daniel.krueg...(a)googlemail.com>
wrote:
> On 20 Mrz., 09:26, pfultz2 <pful...(a)yahoo.com> wrote:
>
> > How would I go about writing a bitwise copy constructor for a class? I
> > would like to call memcpy() before any constructors are called on the
> > members. I cant seem to find any information on this.
>
> It is not really clear to me, what you
> question is - a more specific use-case
> would help. Are you asking for the
> syntax to realize that? Or are you asking
> how you can do that without provoking
> undefined behavior (UB)?
>
> The life-time and trivial type rules of
> C++ put rather severe restrictions on
> class types (I assume your class contains
> members of class-type, because otherwise
> your remark about constructors doesn't
> make sense to me), so simply performing
> memcpy on a std::string within or without
> a constructor will rather easy lead
> to UB land.
>
> What are you going to achieve and why
> isn't a normal copy constructor
> sufficient?
>
> Greetings from Bremen,
>
> Daniel Kr�gler

Well if i call a copy constructor like this:
class foo
{
bar b;
public:
foo(cont foo& rhs)
{
memcpy(this, &rhs, sizeof(foo));
}
};

When it calls the copy constructor the constructor for the fields(such
as bar b) are called first, then memcpy() is called. I want to know
how to call memcpy and not call the constructors for the fields.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Mathias Gaunard on
On 20 mar, 08:26, pfultz2 <pful...(a)yahoo.com> wrote:
> How would I go about writing a bitwise copy constructor for a class? I
> would like to call memcpy() before any constructors are called on the
> members. I cant seem to find any information on this.

That's because you can't do it.
If you want to bitwise copy a class with non-POD members, you must do
that outside of the copy constructor, and still, that's obviously
highly UB and something you shouldn't do.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Nevin :-] Liber on
In article
<5f856b43-6878-4a3a-b2eb-8de6ef7572d8(a)r1g2000yqj.googlegroups.com>,
pfultz2 <pfultz2(a)yahoo.com> wrote:

> > What are you going to achieve and why
> > isn't a normal copy constructor
> > sufficient?
>
> Well if i call a copy constructor like this:
> class foo
> {
> bar b;
> public:
> foo(cont foo& rhs)
> {
> memcpy(this, &rhs, sizeof(foo));
> }
> };


> When it calls the copy constructor the constructor for the fields(such
> as bar b) are called first, then memcpy() is called.

Again, what are you trying to achieve (the goal, not the mechanism)?

> I want to know
> how to call memcpy and not call the constructors for the fields.

You can't, and that is a good thing. You can pick which constructors
are called (via the member initialization list), but you cannot
circumvent calling constructors.

--
Nevin ":-)" Liber <mailto:nevin(a)eviloverlord.com> 773 961-1620

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 |  Next  |  Last
Pages: 1 2 3
Prev: Why is nullptr unsafe?
Next: Nice idiom for lambdas