what's the purpose of ::IceUtil::noncopyable ?

rc_hzrc_hz Member Eric RCOrganization: www.genband.comProject: No project yet ✭✭✭
I am sorry! I know this maybe is a silly question. But I don't understand this indeed! Could anybody say some words for me ? Thank you!

Comments

  • marcmarc FloridaAdministrators, ZeroC Staff Marc LaukienOrganization: ZeroC, Inc.Project: The Internet Communications Engine ZeroC Staff
    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.
    };
    
  • rc_hzrc_hz Member Eric RCOrganization: www.genband.comProject: No project yet ✭✭✭
    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)
  • marcmarc FloridaAdministrators, ZeroC Staff Marc LaukienOrganization: ZeroC, Inc.Project: The Internet Communications Engine ZeroC Staff
    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.
  • michimichi Member Michi HenningOrganization: Triodia TechnologiesProject: I have a passing interest in Ice :-) ✭✭✭
    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 ?
    Your class A defines a copy constructor and copy-assignment operator, so it should come as no surprise that the class indeed can be copied and assigned to. If you want a class to be non-copyable, you derive from noncopyable and don't define a copy constructor and copy-assignment operator in the derived class. Once you do that, you get a compile-time error as expected.

    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.
  • rc_hzrc_hz Member Eric RCOrganization: www.genband.comProject: No project yet ✭✭✭
    Thank you! I know!
  • evanevan Member
    you can read this link for more info on this idiom:
    http://www.boost.org/libs/utility/utility.htm#Class_noncopyable
    (boost::noncopyable)
  • rc_hzrc_hz Member Eric RCOrganization: www.genband.comProject: No project yet ✭✭✭
    Thank you!
Sign In or Register to comment.