码迷,mamicode.com
首页 > 其他好文 > 详细

参数列表调用子类构造函数

时间:2015-01-02 15:56:50      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

有时候,基类构造函数是带有参数,而子类构造函数是不带参数的,如下:
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 Settings\Administrator\桌面\abc\Text1.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;
};

 

参数列表调用子类构造函数

标签:

原文地址:http://www.cnblogs.com/zzyoucan/p/4198456.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!