Nebula2探秘05-RTTI

本文介绍 Nebula2 中 RTTI 的实现方式,主要依赖于 nClass 和 nObject 类。通过示例展示了如何使用 IsA() 和 IsInstanceOf() 方法来检查类的继承关系及对象实例所属。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Nebula2探秘05-RTTI

happykevins文 

       Nebula2的RTTI支持都是在nObject这一层实现的,每个继承自nObject的类都有IsA()和IsInstanceof()两个方法,前者用于判断继承关系,后者则用于判断一个对象是否为指定类的实例,下面的代码说明了他们的用法。

 

/****************************************************************************/
/*    Nebula2 - Tutorial 05                                                    */
/*  Nebula2 RTTI                                                            */
/*  author: happykevins                                                        */
/****************************************************************************/

///----------------------------------------------------------------------------
/// +必要头文件
#include "kernel/nkernelserver.h"
#include 
"kernel/ntimeserver.h"
#include 
"kernel/nfileserver2.h"
/// -必要头文件
///----------------------------------------------------------------------------

///----------------------------------------------------------------------------
/// +链接库
#pragma comment(lib, "wsock32.lib")
#pragma comment(lib, "d_nkernel.lib")
/// -链接库
///----------------------------------------------------------------------------

///----------------------------------------------------------------------------
/// +Application
int main(int argc, const char** argv)
{
    
/// 创建KernelServer
    nKernelServer* ks = n_new(nKernelServer);

    
///----------------------------------------------------------------------------
    
/// +Nebula2 RTTI Test
    
/// @note:
    
///        Nebula2 RTTI implement mostly depends on nClass && nObject

    
/// firstly: we got some instance (all inherited from nObject)
    nRoot* nroot = (nRoot*)ks->New("nroot");
    nObject
* nobject = (nObject*)ks->New("nobject");
    nTimeServer
* ntimeserver = (nTimeServer*)ks->Lookup("/sys/servers/time");
    nFileServer2
* nfileserver2 = (nFileServer2*)ks->Lookup("/sys/servers/file2");

    
/// secondly: Test RTTI Info.
    
/// use 'IsA' & 'IsInstanceOf' methods

    
// 1. nroot is a nroot?
    n_printf("[%s] is %s [%s] "
        nroot
->GetClass()->GetName(), 
        nroot
->IsA("nroot"? "/b" : "not"
        
"nroot");
    
// 2. nroot is a nobject?
    n_printf("[%s] is %s [%s] "
        nroot
->GetClass()->GetName(), 
        nroot
->IsA(ks->FindClass("nobject")) ? "/b" : "not"
        
"nobject");
    
// 3. nobject is a nroot?
    n_printf("[%s] is %s [%s] "
        nobject
->GetClass()->GetName(), 
        nobject
->IsA("nroot"? "/b" : "not"
        
"nroot");
    
// 4. ntimerserver is a nroot?
    n_printf("[%s] is %s [%s] "
        ntimeserver
->GetClass()->GetName(), 
        ntimeserver
->IsA("nroot"? "/b" : "not"
        
"nroot");
    
// 5. nroot is instance of nroot?
    n_printf("[%s] is %s instance of [%s] "
        nroot
->GetClass()->GetName(), 
        nroot
->IsInstanceOf(ks->FindClass("nroot")) ? "/b" : "not"
        
"nroot");
    
// 6. nroot is instance of nobject?
    n_printf("[%s] is %s instance of [%s] "
        nroot
->GetClass()->GetName(), 
        nroot
->IsInstanceOf(ks->FindClass("nobject")) ? "/b" : "not"
        
"nobject");
    
// 7. nfileserver2 is a nobject?
    n_printf("[%s] is %s [%s] "
        nfileserver2
->GetClass()->GetName(), 
        nfileserver2
->IsA("nobject"? "/b" : "not"
        
"nobject");
    
// 8. nfileserver2 is a ntimeserver?
    n_printf("[%s] is %s [%s] "
        nfileserver2
->GetClass()->GetName(), 
        nfileserver2
->IsA("ntimeserver"? "/b" : "not"
        
"ntimeserver");
    
    
/// +Nebula2 RTTI Test
    
///----------------------------------------------------------------------------
    

    
/// 销毁KernelServer
    n_delete(ks);

    getchar();
    
return 0;
}
/// -Application
///----------------------------------------------------------------------------



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值