c++模版的一些使用方法(二)

c++模版的其他技巧:

1.编号生成器

考虑下面的场景,对于int、char、double、bool、float这5种类型,我们希望对他们进行类型编号1-5,并且确保对于每种类型,它的编号不会改变,即每次调用函数获取该编号时,他都是唯一的且不变的。那么使用通过定义一个模版函数实现。

int g_id=0;//全局id计数

template<class T>
int GetTypeId()
{
    static int id = ++ g_id;
    return id;
}

int _tmain(int argc, _TCHAR* argv[])
{
    cout<<"id of int:  "<<GetTypeId<int>()<<endl;
    cout<<"id of char:  "<<GetTypeId<char>()<<endl;
    cout<<"id of double:  "<<GetTypeId<double>()<<endl;
    cout<<"id of float:  "<<GetTypeId<float>()<<endl;
    cout<<"id of bool:  "<<GetTypeId<bool>()<<endl;
    cout<<"id of bool:  "<<GetTypeId<bool>()<<endl;
    cout<<"id of int:  "<<GetTypeId<int>()<<endl;
    cout<<"id of int:  "<<GetTypeId<int>()<<endl;
    cout<<"id of double:  "<<GetTypeId<double>()<<endl;
    return 0;
}

注意:

static int id = ++g_id只在static类型的初始化时被唯一执行一次哦;

虽然对于不同的变量类型(int、bool等),他们调用的是GetTypeId这个函数,但是由于模版参数的类型不同,因此对于每个类型,在实例化这个模版时,都会生成不同的GetTypeId函数,他们在内存中的地址是不同的,比如

实例化int类型的GetTypeId后,该函数的地址是0x01;实例化bool类型的GetTypeId后,它的函数地址是0x02;同理其他

那么当重复调用GetTypeId<int>()时,访问的就是0x01处的函数,而GetTypeId<bool>()时,访问的是0x02处的函数。

所以他们的static int id也肯定不是一个哦。


2.模拟多态

模版可以用来模拟多态。多态基于继承,而继承是由虚表控制的,这里对虚表不多说了。

使用模版来模拟时,需要一个模版参数,代表它的子类的类型。使用时,定义一个基类类型,使用从该几类派生的一个子类做为参数实例化该模版,则基类在调用时将this指针强转为子类型。

example:

template <class ChildType>
class M
{
public:
    ChildType* _this()
    {
        return (ChildType*) this;
    }
    void func()
    {
        _this()->show();
    }
    void show()
    {
        cout<<"class M"<<endl;
    }
};


class A:public M<A>
{
public:
    void show()
    {
        cout<<"class A"<<endl;
    }
};

class B:public M<B>
{
public:
    void show()
    {
        cout<<"class B"<<endl;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    M<B> m1;
    m1.func();
    M<A> m2;
    m2.func();
	return 0;
}


3.插入继承链

下图是一个继承树,从x开始,有一个虚函数,virtual void Show(),且每一个子类都以下面的形式重写这个函数:

void Y::Show()

{

   //增加一些操作

   Z::Show();

}

其中Y表示X1、X2、A1、A2、B1、B2中的任意一个,Z表示Y的父类。

先来个代码看下:

class X
{
public:
    virtual void Show()
    {
        cout<<"X"<<endl;
    }
};

class X1:public X
{
public:
    virtual void Show()
    {
        cout<<"X1"<<endl;
        X::Show();
    }
};

class X2:public X
{
public:
    virtual void Show()
    {
        cout<<"X2"<<endl;
        X::Show();
    }
};

class A1:public X1
{
public:
    virtual void Show()
    {
        cout<<"A1"<<endl;
        X1::Show();
    }
};

class A2:public X1
{
public:
    virtual void Show()
    {
        cout<<"A2"<<endl;
        X1::Show();
    }
};

class B1:public X2
{
public:
    virtual void Show()
    {
        cout<<"B1"<<endl;
        X2::Show();
    }
};

class B2:public X2
{
public:
    virtual void Show()
    {
        cout<<"B2"<<endl;
        X2::Show();
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    cout<<"A1的继承关系:"<<endl;
    A1 a1;
    a1.Show();

    cout<<endl<<"A2的继承关系:"<<endl;
    A2 a2;
    a2.Show();

    cout<<endl<<"B1的继承关系:"<<endl;
    B1 b1;
    b1.Show();

    cout<<endl<<"B2的继承关系:"<<endl;
    B2 b2;
    b2.Show();
	return 0;
}

输出:


现在新的需要要求对A1和B1的Show函数中添加一些共同的操作,A2和B2的Show函数中添加一些共同的操作。

方案一:

修改A1、B1、A2、B2的代码...(此种方案太犀利,不讨论了)

方案二:

在继承链中插入模版类

//新加一个模版类,用于增加A1和B1共同需要的新功能
template <class Base>
class AddClass1:public Base
{
public:
    virtual void Show()
    {
        cout<<"A1和B1共同需要的新功能"<<endl;
        Base::Show();
    }
};

//新加一个模版类,用于增加A2和B2共同需要的新功能
template <class Base>
class AddClass2:public Base
{
public:
    virtual void Show()
    {
        cout<<"A2和B2共同需要的新功能"<<endl;
        Base::Show();
    }
};

class A1:public AddClass1<X1>//从AddClass1派生,并告诉AddClass1从X1派生
{
public:
    virtual void Show()
    {
        cout<<"A1"<<endl;
       AddClass1<X1>::Show();//基类Show
    }
};

class A2:public AddClass2<X1>//从AddClass2派生,并告诉AddClass2从X1派生
{
public:
    virtual void Show()
    {
        cout<<"A2"<<endl;
        AddClass2<X1>::Show();//基类Show
    }
};

class B1:public AddClass1<X2>//从AddClass1派生,并告诉AddClass1从X2派生
{
public:
    virtual void Show()
    {
        cout<<"B1"<<endl;
        AddClass1<X2>::Show();//基类Show
    }
};

class B2:public AddClass2<X2>//从AddClass2派生,并告诉AddClass2从X2派生
{
public:
    virtual void Show()
    {
        cout<<"B2"<<endl;
        AddClass2<X2>::Show();//基类Show
    }
};

输出:

插入后的继承树:

颜色表示继承路径

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值