In the code given below, to access private data member of B we can use member function of class B and return the data member, assign that to the data member of A within the constructor (converter function). But I am unable to do so. Please suggest some help. Other way can be making Class A friend of B but there is need for accessing by constructor.
#include <iostream>
using namespace std;
class B
{
int i_;
public:
B(int i = 3) : i_(i) {}
int give() { return i_; }
};
class A
{
int i_;
public:
A(int i = 0) : i_(i) { cout << "A::A(i)\n"; }
A(const B &b1) : i_(b1.i_) {} // Type casting using constructor
int display() { cout << i_; }
};
int main()
{
A a;
B b; // object is constructed
a = static_cast<A>(b); // B::operator A(), converting data member of class B to class A
a.display();
return 0;
}