어떻게 c+클래스 를 계승 할 수 없 게 합 니까?

참고 문헌:
http://www.research.att.com/~bs/bs_faq2.html#no-derivation http://dev.csdn.net/article/14/14193.shtm
 
Can I stop people deriving from my class?
Yes, but why do you want to? There are two common answers:
  • for efficiency: to avoid my function calls being virtual
  • for safety: to ensure that my class is not used as a base class (for example, to be sure that I can copy objects without fear of slicing)

  • In my experience, the efficiency reason is usually misplaced fear. In C++, virtual function calls are so fast that their real-world use for a class designed with virtual functions does not to produce measurable run-time overheads compared to alternative solutions using ordinary function calls. Note that the virtual function call mechanism is typically used only when calling through a pointer or a reference. When calling a function directly for a named object, the virtual function class overhead is easily optimized away.
    If there is a genuine need for "capping" a class hierarchy to avoid virtual function calls, one might ask why those functions are virtual in the first place. I have seen examples where performance-critical functions had been made virtual for no good reason, just because "that's the way we usually do it".
    The other variant of this problem, how to prevent derivation for logical reasons, has a solution. Unfortunately, that solution is not pretty. It relies on the fact that the most derived class in a hierarchy must construct a virtual base. For example:
    	class Usable;
    
    	class Usable_lock {
    		friend class Usable;
    	private:
    		Usable_lock() {}
    		Usable_lock(const Usable_lock&) {}
    	};
    
    	class Usable : public virtual Usable_lock {
    		// ...
    	public:
    		Usable();
    		Usable(char*);
    		// ...
    	};
    
    	Usable a;
    
    	class DD : public Usable { };
    
    	DD dd;  // error: DD::DD() cannot access
            	// Usable_lock::Usable_lock(): private  member
    

    좋은 웹페이지 즐겨찾기