From: tni on
On 2010-08-12 9:08, Vladimir Jovic wrote:
> Kai-Uwe Bux wrote:
>> As for detecting shallow copies, what about something like this:
>>
>> class detect_shallow_copy {
>>
>> detect_shallow_copy * this_ptr;
>>
>> public:
>>
>> detect_shallow_copy ( void )
>> : this_ptr ( this )
>> {}
>>
>> detect_shallow_copy ( detect_shallow_copy const & )
>> : this_ptr ( this )
>> {}
>>
>> detect_shallow_copy & operator= ( detect_shallow_copy const & ) {
>> return ( *this );
>> }
>>
>> bool is_shallow ( void ) const {
>> return ( this != this_ptr );
>> }
>>
>> };
>>
>> #include <cstring>
>> #include <iostream>
>>
>> int main ( void ) {
>> detect_shallow_copy d;
>> detect_shallow_copy e;
>> std::memcpy( &e, &d, sizeof(d) );
>> std::cout << d.is_shallow() << "\n";
>> std::cout << e.is_shallow() << "\n";
>> }
>>
>
> Very nice trick.
> Does it depends on the compiler and how the hidden pointer *this is
> implemented?

Not really.

> Will it work if detect_shallow_copy inherit from a class with virtual
> destructor and methods?

Yes.

\\

memcpy is very dangerous in the presence of virtual stuff (and in C++98
formally only allowed for PODs).

Something like:

class A {
// has virtual stuff
...
};

A a1;
A a2;

memcpy(&a1, &a2, sizeof(a1));

will most likely work (but you are firmly in the area of undefined /
platform-specific behavior and the Compiler Vendor is not going to tell
you if it's supposed to work or not).

Something like:

class B : public A {
...
};

A a1;
B b1;
A& a2 = b1;

memcpy(&a1, &a2, sizeof(a1));

will most likely not work correctly.

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

From: Francis Glassborow on
Vladimir Jovic wrote:
> Kai-Uwe Bux wrote:
>> As for detecting shallow copies, what about something like this:
>>
>> class detect_shallow_copy {
>>
>> detect_shallow_copy * this_ptr;
>>
>> public:
>>
>> detect_shallow_copy ( void )
>> : this_ptr ( this )
>> {}
>>
>> detect_shallow_copy ( detect_shallow_copy const & )
>> : this_ptr ( this )
>> {}
>>
>> detect_shallow_copy & operator= ( detect_shallow_copy const & ) {
>> return ( *this );
>> }
>>
>> bool is_shallow ( void ) const {
>> return ( this != this_ptr );
>> }
>>
>> };
>>
>> #include <cstring>
>> #include <iostream>
>>
>> int main ( void ) {
>> detect_shallow_copy d;
>> detect_shallow_copy e;
>> std::memcpy( &e, &d, sizeof(d) );
>> std::cout << d.is_shallow() << "\n";
>> std::cout << e.is_shallow() << "\n";
>> }
>>
>
> Very nice trick.
> Does it depends on the compiler and how the hidden pointer *this is
> implemented?
> Will it work if detect_shallow_copy inherit from a class with virtual
> destructor and methods?
>
It may look like a nice trick but it does nothing useful. It is not
enough to have a member function that can detect a shallow copy, EVERY
member function of the class(es) in question must check. Why? Because
any programmer who realises the need to check will be clever enough to
avoid the problem in the first place.

In addition we have the problem that once the shallow copy has been
created we need a way to destroy it and the normal dtor will not suffice.

Basically, IMO, the original question is predicated on the idea that you
might write a library for a programmer who is liable to break his own
code. If you want to do that there are hundreds of other things you
would need to detect and deal with. It is not the job of the library
designer to write for incompetent users; that is the job of education.

--
Note that robinton.demon.co.uk addresses are no longer valid.

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