zoukankan      html  css  js  c++  java
  • 参数列表调用子类构造函数

    有时候,基类构造函数是带有参数,而子类构造函数是不带参数的,如下:
    class Animal
    {
    public:
        Animal(int width, int height) { this->width = width; this->height = height; }
    private:
        int width, height;
    };
    
    class Cat:public Animal
    {
    public:
        Cat() { cout << "init cat" << endl; }
    };
    在建立Cat类对象时,编译出错:
    C:Documents and SettingsAdministrator桌面abcText1.cpp(104) : error C2512: 'Animal' : no appropriate default constructor available
    
    解决这个问题应该在Cat的构造函数中显式调用基类的带参构造函数。因为在基类中定义了带参构造函数,编译器不会提供默认构造函数。
    (或者可以在基类中增加一个不带参数的构造函数)这个问题将解决。
    
    下面采用的是调用基类带参构造函数的方式:
    class Cat:public Animal
    {
    public:
        Cat():Animal(100,200) { cout << "init cat" << endl; }//参数列表调用子类构造函数用法
    };
    
    即在构造函数的后面增加一个冒号,后面是基类的构造函数。
    这种方式同样可以用来初始化类中的常量。
    
    由于在类定义中不允许对成员进行初始化,常量也不允许。
    如下所示:
    class Cat:public Animal
    {
    public:
        Cat():Animal(100,200),Age(2),IsLikeFish(true) { cout << "init cat" << endl; }
    private:
        const int Age;
        const bool IsLikeFish;
    };
  • 相关阅读:
    R语言学习——数据框
    R语言学习——数组
    R语言学习——矩阵
    R语言学习——向量
    SSM的项目结构
    simple-spring-memcached简介
    Arrays
    AbstractCollection 类
    Collections 类
    Map接口
  • 原文地址:https://www.cnblogs.com/zzyoucan/p/4198456.html
Copyright © 2011-2022 走看看