C++

Copy Code

class A1 : public A { public: A1(B1 *b1, C1 *c1) : A(b1, c1) {} };

See more:

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 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.

Is there any better way to implement it?

Tried :

A (B *b , C *c).

Tried :

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

Comments

There is no need to pass copies. Just pass the pointers to the derived classes to the constructor of your base class A:

C++

Copy Code

class A1 : public A { public: A1(B1 *b1, C1 *c1) : A(b1, c1) {} };

Comments

It is true only if b and b1 are the same, but that not seems to be the case... Or at least not clearly the case...

It requires b1 to be a pointer to a class derived from B (and similar for c1 / C). That is what I have read out of the question:

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

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900

When you are telling that A's constructor has two parameters of type B and C, do you mean of class B and class C mentioned just before?

So b and b1 are two different objects one of type class B and the other of type class B1?

Thanks for your reply.

Actually, I have implemented it in the same way as you implemented.

The problem comes now, whenever I call the constructor of A1, at that time I need to pass both B and B1 separately.

Before constructor call, I need to set fields in B and B1 separately (or copy), then I can use them in.

In that case how to pass B and C ?

But IDK how to pass B and C inside A1, as I have just use B1 and C1 in A1's constructor argument.