Re: 請問c++的繼承
※ 引述《quota@kkcity.com.tw (大改名片檔)》之銘言:
: 請問是否有語法可以設定
: 我這個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
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):