繁体   English   中英

控制如何从子类构造函数声明(C ++)将参数发送到超类构造函数

[英]Controlling how parameters are sent to superclass constructor from subclass constructor declaration (C++)

可以吗? 我正在寻找与使用成员初始化列表不同的东西(因为它存在于定义中,而不一定是声明中)。

就像是

class(args) : superclass(fn of args);

您的要求没有任何意义。

在C ++中,每个类都有一个构造函数。
对于构造函数,声明定义参数。
对于构造函数,定义定义了如何使用参数(例如如何将其传递给基类)。

例:

Plop.h
=============
class Point
{
    public:
       Point(int x,int y);
};

class MyPoint: public Point
{
    public:
        MyPoint(int x);
        MyPoint(int x,int y);
        MyPoint(double z);
};

Plop.cpp
========
Point::Point(int x,int y)
{}

MyPoint::MyPoint(int x):       Point(x,x)           {}
MyPoint::MyPoint(int x,int y): Point(x*2,y-x)       {}
MyPoint::MyPoint(double z):    Point(sin(z),cos(z)) {}

是什么禁止您使用成员函数?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM