开发者

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

Can it be done? I am looking for something different than using the member initialization list (because that is present in the definition, not necessarily the decl开发者_StackOverflow社区aration).

Something like

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


What you are asking makes no sense.

In C++ each class has a constructor.
For a constructor the declaration defines the parameters.
For a constructor the definition defines how the parameters are used (like how they are passed to the base class).

Example:

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)) {}


What forbid you from using member functions?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜