zoukankan      html  css  js  c++  java
  • 模板与类的继承混合使用

    注意以下代码:

      template<class MYT>

      class NEWCLASS: public MYT


      这种用法是我最近才发现的,在WTL中,如果我们用类向导创建的单文档中,有一个CAboutDlg这个类中就用到的了这种方式。

      哪位大神,帮我瞧瞧这种方法的优缺点在哪里?在什么场合下使用比较多?

    #include <iostream>
    using namespace std;
    
    class A
    {
    public:
    	void SetData(int _First = 0, int _Second = 0 )
    	{
    		a = _First;
    		b = _Second;
    	}
    	void Show()
    	{
    		cout<<"A::a: "<<a<<endl;
    		cout<<"A::b: "<<b<<endl;
    	}
    private:
    	int a;
    	int b;
    };
    
    template<class MYT>
    class NEWCLASS: public MYT
    {
    public:
    	void SetNewClassData(int _data = 5)
    	{
    		c = _data;
    	}
    
    	void NewClassShow()
    	{
    		cout<<"NEWCLASS::c: "<<c<<endl;
    	}
    private:
    	int c;
    };
    
    int main()
    {
    	typedef NEWCLASS<A> STDClass;
    	STDClass a;
    
    	a.SetData(5,6);
    	a.SetNewClassData(8);
    	a.Show();
    	a.NewClassShow();
    
    	return 0;
    }
    

     

    另外一个例子,代码是没有任何问题的,我更想知道的是这些大概有什么用处?

    #include <iostream>
    using namespace std;
    
    class A
    {
    public:
    	void SetData(int _First = 0, int _Second = 0 )
    	{
    		a = _First;
    		b = _Second;
    	}
    	void Show()
    	{
    		cout<<"A::a: "<<a<<endl;
    		cout<<"A::b: "<<b<<endl;
    	}
    private:
    	int a;
    	int b;
    };
    
    class B
    {
    public:
    	void BSetData(int _First = 0)
    	{
    		d = _First;
    	}
    	void BShow()
    	{
    		cout<<"B::d: "<<d<<endl;
    	}
    private:
    	int d;
    };
    
    template<class MYT>
    class NEWCLASS: public MYT,public B
    {
    public:
    	void SetNewClassData(int _data = 5)
    	{
    		c = _data;
    	}
    
    	void NewClassShow()
    	{
    		cout<<"NEWCLASS::c: "<<c<<endl;
    	}
    private:
    	int c;
    };
    
    int main()
    {
    	typedef NEWCLASS<A> STDClass;
    	STDClass a;
    
    	a.SetData(5,6);
    	a.SetNewClassData(8);
    	a.BSetData(66);
    	a.Show();
    	a.BShow();
    	a.NewClassShow();
    
    	return 0;
    }
    

      

  • 相关阅读:
    iOS API 概述
    iOS开发的一些奇巧淫技3
    iOS开发的一些奇巧淫技1&2
    iOS-一键拨号
    iOS层次架构
    简单block 和 代理
    iOS开发-单例GCD
    简单的归档、接档
    通知中心与本地通知
    安装linux centos 7.7 遇到 DRM:Pointer to TMDS table invalid
  • 原文地址:https://www.cnblogs.com/BreakMind/p/2285508.html
Copyright © 2011-2022 走看看