Archived
This forum has been archived. Please start a new discussion on GitHub.
what's the purpose of ::IceUtil::noncopyable ?
Comments
-
You can derive from this class to hide the copy constructor and assignment operator, so that objects of a certain type cannot be copied or assigned. You typically do this if you don't want or need copying and assignment for a certain type, and you want the compiler catch any code that attempts to do so.
Note that this is not an official Ice class, but used for internal purposes only. In your own code, you should hide the copy constructor and assignment operator as usual, e.g.:class Foo { public: // ... private: Foo(const Foo&); // No implementation. void operator=(const Foo&); // No implementation. };
0 -
Thank you. But I write the following code and it works well. It seems noncopyable has no impact on its derived class - A, why ?
#include "iostream.h" using namespace std; class noncopyable { protected: noncopyable() { } ~noncopyable() { } private: noncopyable(const noncopyable&); const noncopyable& operator=(const noncopyable&); }; class A: public noncopyable { public: int _mem; public: A() { cout << "This is in A()" << endl; _mem = 100; } A(const A& a) { cout << "This is in A(const A a)" << endl; _mem = a._mem; } const A& operator=(const A& a) { cout << "This is in operator=(const A& a)" << endl; _mem = a._mem; return *this; } }; int main() { A a1; a1._mem = 1000; A a2(a1); a2 = a1; }
The above code can be compiled and it runs as follows:
localhost%a.out
This is in A()
This is in A(const A a)
This is in operator=(const A& a)0 -
To be honest, I don't know all the details about C++ operator overriding and hiding either. It might be best if you ask this question in a C++ support group, as it is not really Ice specific.0
-
rc_hz wrote:Thank you. But I write the following code and it works well. It seems noncopyable has no impact on its derived class - A, why ?
Cheers,
Michi.
PS: As an aside, inheritance from noncopyable might as well be private because there is nothing in noncopyable that needs to be visible to a non-member.0 -
Thank you! I know!0
-
you can read this link for more info on this idiom:
http://www.boost.org/libs/utility/utility.htm#Class_noncopyable
(boost::noncopyable)0 -
Thank you!0