0

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;
}
KnitahK
  • 11
  • 3

2 Answers2

0

Your problem is const correctness. int give() is a non-const member function, which can only be called on non-const objects. However const B &b1 is a reference to a const object.

Since you don't modify the B object when returning the value of the integer, make your code const correct by const qualifying the member function:

int give() const { return i_; }

And now the A c'tor isn't attempting an illegal operation.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • Thanks for useful info. The problem is still this : prog.cpp: In constructor ‘A::A(const B&)’: prog.cpp:18:32: error: ‘int B::i_’ is private within this context A(const B& b1) : i_(b1.i_) { } // Type casting using constructor ^~ prog.cpp:6:9: note: declared private here int i_; ^~ I want to know how to access that i_ of B without friend declaration. – KnitahK Sep 13 '17 at 06:33
  • @KnitahK - This isn't a forum or a debugging service. I pointed out the problem with the code that was obvious. If you have a *specific* issue, post a [mcve]. – StoryTeller - Unslander Monica Sep 13 '17 at 06:34
  • 1
    @KnitahK - And that error indicate you aren't using `get()`. So what does your code in the question has to do with the error? – StoryTeller - Unslander Monica Sep 13 '17 at 06:35
  • Sorry for not asking question correctly. I tried using get() function in conversion constructor of A but I think I may not be knowing exact way to do so. That was what I am trying to ask, the correct way to write code. So may you please explain it to me. – KnitahK Sep 13 '17 at 06:42
  • 1
    @KnitahK - Your use of a getter is okay. There is no unqualified "best" way. It's highly dependent on various factors. [Pick good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to take you through the various levels of knowing the language. And write a lot of code. This is something only experience can teach. – StoryTeller - Unslander Monica Sep 13 '17 at 06:45
0

It's not enough to follow @StoryTeller's advice, you also need to change constructor of A to use proper 'getter' method so that you stop accessing private member:

A(const B &b1) : i_(b1.give()) {}

vasek
  • 2,759
  • 1
  • 26
  • 30