Click here to Skip to main content
15,303,572 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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).
Posted
Updated 9-Jul-18 2:48am
v4
Comments
Kornfeld Eliyahu Peter 9-Jul-18 8:26am
   
All those A, B and C are confusing...
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?
CPallini 9-Jul-18 8:27am
   
Sorry, I don't get you: if you need to pass four arguments then you have to pass all of them: I see no shortcut.
Kornfeld Eliyahu Peter 9-Jul-18 8:32am
   
class B
{
    public:
        B();
};

class B1 : public B
{
    public:
        B1() : B()
        {
            
        };
};

class C
{
    public:
        C();
};

class C1 : public C
{
    public:
        C1() : C()
        {
            
        };
};

class A
{
    public:
        A(B* b, C* c);
};


class A1 : public A
{
    public:
        A1(B* b, C* c, B1* b1, C1* c1) : A(b, c)
        {
            
        };
};
Hemal Shah 9-Jul-18 8:55am
   
HI KornfeldEliyahuPete,

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.
Kornfeld Eliyahu Peter 9-Jul-18 8:57am
   
I can't see any shortcut for that - you designed a setup where A1 has four parameters... You have to pass four parameters...
Hemal Shah 9-Jul-18 9:02am
   
Is there any alternative way, like use only B1 and C1 in A1's constructor, and construct A inside A1 constructor call. ?
In that case how to pass B and C ?
Kornfeld Eliyahu Peter 9-Jul-18 9:08am
   
It is possible, but may be a bad design (should A1 know how to create A?)...
Hemal Shah 9-Jul-18 9:22am
   
Yes A1 knows, it has to use B and C during A constructor call.
But IDK how to pass B and C inside A1, as I have just use B1 and C1 in A1's constructor argument.
Kornfeld Eliyahu Peter 9-Jul-18 9:28am
   
So?
class A1 : public A {
  public:
    A1(B1* b1, C1* c1) {
      // create b from b1 and c from c1
      // call base constructor
    }
};

There is no need to pass copies. Just pass the pointers to the derived classes to the constructor of your base class A:
C++
class A1 : public A
{
public:
    A1(B1 *b1, C1 *c1) : A(b1, c1) {}
};
   
Comments
Kornfeld Eliyahu Peter 9-Jul-18 8:32am
   
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...
Jochen Arndt 9-Jul-18 8:43am
   
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."
Quote:
Before calling I have to prepare both b and b1 separately.
Is there any better way to implement it?

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)
   

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