作者LPH66 (台大我回来了!)
看板Programming
标题Re: 请问c++的继承
时间Thu Sep 6 14:34:15 2007
※ 引述《[email protected] (大改名片档)》之铭言:
: 请问是否有语法可以设定
: 我这个class拒绝其他class继承我呢
java有个叫final class的东西
C++的话...看这篇文章吧:
http://www.codeproject.com/cpp/finalclass.asp
简而言之就是如此:
class Temp
{
private:
Temp();
friend class TheFinalClass;
};
class TheFinalClass: virtual public Temp
{
//...
};
加virtual的原因是如果某个DerivedClass继承TheFinalClass的话
DerivedClass的constructor会直接呼叫Temp的(private)constructor
因此compiler发现DerivedClass不是Temp的friend 就阻止编译
但如果只是实体化TheFinalClass
则因为它是Temp的friend constructor可以顺利往上呼叫
上篇文章中放进private的是它的destructor 意思是一样的
(因为constructor和desturctor都会呼叫父class的ctor/dtor
而文中如此做的原因是方便 毕竟ctor可以有很多个 dtor只能有一个)
: 还有,我在程式内如何得知
: 这个物件是由哪一个class产生的呢?
: 谢谢
这有点像java的instanceof
这篇问答可以看一下:
http://www.thescripts.com/forum/thread162106.html
简单说来 要考虑的东西不少
不过如果硬要用的话 似乎可以查一下typeid的用法
--
'Oh, Harry, dont't you
see?' Hermione breathed. 'If she could have done
one thing to make
absolutely sure that every single person in this school
will read your interview, it was
banning it!'
---'Harry Potter and the order of the phoenix', P513
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 219.84.44.21