构造函数参数的继承。 [英] Inheritance in parameters of constructor.

查看:90
本文介绍了构造函数参数的继承。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基类是A,B,C,它们各自的子类是A1,B1,C1。



- >类构造函数使用两个参数。 (B和C)



喜欢,A(B * b,C * c)。



- > A1类构造函数使用两个参数B1和C1,我也想通过传递参数B和C同时调用一个构造函数。



我已经实现了它方式:A1(B * b,C * c,B1 * b1,C1 * c1):A(b,c)。



- >但问题是我必须保留两份副本;像基地b和孩子b1。 在打电话之前我必须分别准备b和b1。

有没有更好的方法来实现它?




我尝试了什么: < br $> b $ b

试过:



A(B * b,C * c)。



尝试:

A1(B * b,C * c,B1 * b1,C1 * c1):A(b,c )。

Base Classes are A, B, C and their respective Child classes are A1, B1, C1.

-> A class constructor uses two parameters. (B and C)

Like, A (B *b, C *c).

-> A1 class constructor uses two parameters B1 and C1 and I also want to call A constructor at the same time with passing argument B and C.

I have implemented it by this way : A1 (B *b, C *c, B1 *b1, C1 *c1) : A(b,c).

-> But the problem here is I have to maintain two copies; Like base b and child b1. Before calling I have to prepare both b and b1 separately.
Is there any better way to implement it?


What I have tried:

Tried :

A (B *b , C *c).

Tried :
A1 (B *b, C *c, B1 *b1, C1 *c1) : A(b,c).

推荐答案

无需传递副本。只需将指向派生类的指针传递给基类A的构造函数:

There is no need to pass copies. Just pass the pointers to the derived classes to the constructor of your base class A:
class A1 : public A
{
public:
    A1(B1 *b1, C1 *c1) : A(b1, c1) {}
};


引用:

在打电话之前我必须分别准备b和b1。

有没有更好的方法来实现它?

Before calling I have to prepare both b and b1 separately.
Is there any better way to implement it?



No.

As b b1 是单独的对象,你不能将 b 传递给 A 以任何其他方式......

(如果 b1 只是 b ,而不是传递 b1


No.
As b and b1 are separate objects, you can not pass b to A in any other way...
(If b1 was only a cast of b, than it would be enough to pass b1)


这篇关于构造函数参数的继承。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆