From: MC on 7 Aug 2010 12:31 Hi I am writing a library in C++. In my library I want to implement a feature that if an application programmer instantiates a class object and later does a shallow copy by memcpy or by any other means. During the run time when ever any function of the shallow copy object is called it should print out a message Warning: Shallow Copy detected. What will be the best way to do this? Thanks. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Stefan van Kessel on 8 Aug 2010 06:50 On 8/8/2010 5:31 AM, MC wrote: > Hi I am writing a library in C++. In my library I want to implement a > feature that if an application programmer instantiates a class object > and later does a shallow copy by memcpy or by any other means. During > the run time when ever any function of the shallow copy object is > called it should print out a message Warning: Shallow Copy detected. > > What will be the best way to do this? > > Thanks. > One way of doing it, is keeping a copy of a this pointer. #include <iostream> class Foo { Foo* address_; void detectShallowCopy(){ if(address_ != this) std::cout<<"Warning: Shallow Copy"<<std::endl; } public: Foo(){address_ = this;} Foo(const Foo&) { address_ = this; } Foo& operator=(const Foo&) { address_ = this; return *this; } void bar() { detectShallowCopy(); } void baz() { detectShallowCopy(); } }; int main() { Foo foo1; foo1.bar(); Foo foo2(foo1); foo2.bar(); foo2 = foo1; foo2.baz(); std::cout<< "checkpoint" <<std::endl; memcpy(&foo2, &foo1, sizeof(Foo)); foo2.bar(); // output: // checkpoint // Warning: Shallow Copy } -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Vaclav Haisman on 8 Aug 2010 23:35 MC wrote, On 8.8.2010 5:31: > Hi I am writing a library in C++. In my library I want to implement a > feature that if an application programmer instantiates a class object > and later does a shallow copy by memcpy or by any other means. During > the run time when ever any function of the shallow copy object is > called it should print out a message Warning: Shallow Copy detected. I doubt the usefulness of any such "feature". However, it could help storing some sort of self reference into the instance to which you can later compare to. E.g. struct S { S () : self (this) { } S (S const & other) : self (this) { } // etc. void foo () { if (self != this) { /* bad copy detected */ } S const * const self; }; -- VH [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Mathias Gaunard on 8 Aug 2010 23:54 On Aug 8, 4:31 am, MC <manan.cho...(a)gmail.com> wrote: > Hi I am writing a library in C++. In my library I want to implement a > feature that if an application programmer instantiates a class object > and later does a shallow copy by memcpy or by any other means. During > the run time when ever any function of the shallow copy object is > called it should print out a message Warning: Shallow Copy detected. > > What will be the best way to do this? You could store the address of the object into the object itself, and everytime check if those are indeed the same. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: marcin.sfider on 8 Aug 2010 23:55 On Aug 8, 5:31 am, MC <manan.cho...(a)gmail.com> wrote: > Hi I am writing a library in C++. In my library I want to implement a > feature that if an application programmer instantiates a class object > and later does a shallow copy by memcpy or by any other means. During > the run time when ever any function of the shallow copy object is > called it should print out a message Warning: Shallow Copy detected. > > What will be the best way to do this? > > Thanks. > You could store object's address acquired from 'this' pointer in constructor and check if the stored value is equal to 'this' at begining of every method. However IMO using memcpy or any of similar means to "shallow copy" non-POD C++ object is such an fundamental error that you can ignore it. Someone who does such things will probably learn on his mistakes soon enough. Cheers Sfider -- [ 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 4 Prev: Set implementation in STL Next: Is there std function that calculates a bounded value? |