惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Jina AI
Jina AI
博客园 - 司徒正美
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
J
Java Code Geeks
博客园 - 聂微东
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
美团技术团队
腾讯CDC
博客园 - Franky
MyScale Blog
MyScale Blog
人人都是产品经理
人人都是产品经理
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
aimingoo的专栏
aimingoo的专栏
博客园_首页
V
V2EX
Martin Fowler
Martin Fowler
T
The Blog of Author Tim Ferriss

博客园 - 于光远

泛型编程 分区理论知识整理 SPI协议及驱动开发框架 I2C协议及驱动开发框架 使用qemu 加载linux-6.18.1内核 linux 内核 策略模式 单例模式 Linux性能分析:从监控到优化的完整工具链 C++模板元编程实现序列化与反序列化 引用 bootchart linux 脚本打印时间 接口编程 不定参数解析,结合tuple 独立于main运行的程序 std::ostream & operator<< Flash HIDL --HelloWorld
类模板继承
于光远 · 2022-03-10 · via 博客园 - 于光远

类模板继承

#include <iostream>
using namespace std;

class interface{
public:
    virtual int getAge()=0;
    virtual int getAge1()=0;
};

class person:virtual public interface{
public:
    person(int a){
        age = a;
        std::cout<<"age:"<<age<<endl;
    }
    int getAge(){
        return age;
    }
private:    
    int age;
};

class person1:virtual public interface{
public:
    person1(int a){
        age1 = a;
        std::cout<<"age1:"<<age1<<endl;
    }
    int getAge1(){
        return age1+12;
    }
private:    
    int age1;
};


template<typename ... _AttributeExtensions>
class student
    :virtual public _AttributeExtensions...{
    
    public:
        student(int a);
        ~student(){}
    int n;    
};

template <typename ... _AttributeExtensions>
student<_AttributeExtensions...>::student(int a):n(a),
        _AttributeExtensions(a)... {
}

int main(){
    student<person,person1> s(22);
    std::cout<<"age1:"<<s.getAge1()<<std::endl;
    std::cout<<"age:"<<s.getAge()<<std::endl;
    
return 0;
}
ygy@ygy-VirtualBox:~/work/tmp$ g++ c.cpp 
ygy@ygy-VirtualBox:~/work/tmp$ ./a.out 
age:22
age1:22
age1:34
age:22

posted @ 2022-03-10 15:58  于光远  阅读(109)  评论()    收藏  举报