gpt4 book ai didi

c++ - 控制如何将参数从子类构造函数声明发送到父类(super class)构造函数 (C++)

转载 作者:行者123 更新时间:2023-11-28 03:56:47 26 4
gpt4 key购买 nike

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

有点像

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

关于c++ - 控制如何将参数从子类构造函数声明发送到父类(super class)构造函数 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3242475/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com