谁能指点迷津.不能防止类被继承。局部类可以防止被继承,但是不是我想要的。

mujiok2003 2008-05-04 05:08:47
namespace Private
{
class NonDerivableHelper
{
protected:
NonDerivableHelper() {}
virtual ~NonDerivableHelper(){}
};
}
#define FINAL_CLASS : private virtual Private::NonDerivableHelper
class NonDerivable FINAL_CLASS
{
};
class GrandSon : public NonDerivable
{
public:
static int a;
};

int GrandSon::a = 10;
...全文
173 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
嵌云阁主 2008-05-05
  • 打赏
  • 举报
回复
从语法上不太可能防止继承
taodm 2008-05-05
  • 打赏
  • 举报
回复
这是一个没有实际意义的功能。
不要在上面多浪费时间。
或者,你可以转java、c#,它们有这个功能。
mujiok2003 2008-05-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 redleaves 的回复:]
Bjarne Stroustrup的方法确实不太好看.因为要等到生成实例的时候才会出错...不过C++现在也没有更好的办法.
[/Quote]
Bjarne Stroustrup的方法只能防止派生类生成实例,不能防止派生(继承)。
redleaves 2008-05-04
  • 打赏
  • 举报
回复
Bjarne Stroustrup的方法确实不太好看.因为要等到生成实例的时候才会出错...不过C++现在也没有更好的办法.
dahua010 2008-05-04
  • 打赏
  • 举报
回复
Bjarne Stroustrup的答案:
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 slizing)
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 do 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 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
dahua010 2008-05-04
  • 打赏
  • 举报
回复
不想被继承?
写成DLL,然后给出接口,不就好了
jieao111 2008-05-04
  • 打赏
  • 举报
回复
构造函数声明private

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧