Constructor initializer list using other variables

I would like to be able to initialize variables using other initialized variables. I am unable to workout how to formulate line 29. Any help will be gratefully received.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class RealIm20c
{
double re, im;

public:
RealIm20c(const  RealIm20c& c) {re=c.re; im=c.im;};
void Display20c(){
cout << "re = " << re << " im = " << im << endl;
}
};

class RealIm20b
{
double re, im;
friend class RealIm20c;
public:
RealIm20b() : re(0), im(0){};
void Display20b(){
cout << "re = " << re << " im = " << im << endl;
}
};


int main()
{
RealIm20b complex20b;
complex20b.Display20b();

RealIm20c complex20c = complex20b;
complex20c.Display20c();
}
You can either overload a RealIm20c contructor to get an object of RealIm20b as argument or overload a conversion operator from RealIm20b to RealIm20c
Many thanks to Bazzy for given comment. Could I ask for some clarification or example please?
I'll use an example with different class names as your can be confusing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Overload constructor:

class B; // forward declaration

class A
{
     // some members
     A ( const B& ); // construct from B -forward declaration-
};

class B
{
    // ...
};

A::A ( const B &b ) // constructor body after class B body so that it will know which are B's members
{
    // convert here
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Conversion operator
class A
{
     // ...
};

class B
{
    //...
    
    operator A () // conversion operator
    {
        A tmp;
        // modify 'tmp' as you wish so that it will be the converted value
        return tmp;
    }
};
Many thanks.
Topic archived. No new replies allowed.