Hi All,
After compiling below code , I am getting below error.
error: cannot convert a* to b* in assignment.
it seems to be assignment doesn't work with pointers . Can anybody throw some light on this?
i tried to use unique_ptr by transfering the ownership from a to b but it doesn't work.

#include <iostream>
#include <memory>
using namespace std;
class b ;
class a
{
int a1 ;
b *p;
public:
a(int x=0):a1(x){}
void fun1() {cout<<a1;}

};

class b
{
private:
a *p;
public:

b (int x=0)
{
p= new a(x);
//p1=new a(x);
}
b ( const b &obj)
{
p = new a (*obj.p);
}

const b& operator=(const a& obj)
{

if(&obj != p)
{
delete p;
p=new a(obj);

}
return *this;
}


void fun2(){p->fun1();}
~b () { delete p;}

};
int main()
{

b *obj1= new b (10);
a *obj2= new a(20);
obj1=obj2;
obj1->fun2();

/*
unique_ptr<b> obj1 ( new b (10));
unique_ptr<a> obj2 (new a(20));
obj1=move(obj2);
obj1->fun2();
*/

return 1;
}

Recommended Answers

All 8 Replies

it seems to be assignment doesn't work with pointers .
i tried to use unique_ptr by transfering the ownership from a to b but it doesn't work.

It's giving you an error because what you are trying to do makes no sense (to the compiler and to me). There are three questions here.

  1. Can I do this?
  2. Should I do this?
  3. What am I trying to do?

The answer to question 2 often depends on the answer to question 3, so answer question 3 before writing any code.

In C++, the answer to question 1 is almost always yes. C++ defers to the programmer and gives the programmer an infinite amount of rope to hang himself with. I can show you ways to have a class b object call a class a function. Who knows? It might not even crash your program.

But do you really want to do this? As mentioned, you can get "clever" and make the C++ language do things it was never intended to do. C++'s attitude is to warn you that you're doing something wrong, then if you insist, shrug its shoulders and say "Oooookkkkaaayyyy". So, are you trying to "trick" the compiler and see what happens when you do something that makes no sense or are you trying to make something work the way it was intended?

Figure out what you are trying to do and why. I for one cannot understand what you're trying to accomplish by trying to making a pointer to an object of one class point to an object of a different class.

If you are just trying to see what you can get away with and what happens when code executes, try changing line 53 to this:

obj1 = (b*) obj2;

It compiles for me, then the program crashes when I run it (as expected).

You can also add a b* extractor method the class a that returns the b* from the a object. Then your assignment would probably work.

operator b* () { return p; }

And change the assignment from obj1=obj2; to obj1=*obj2;
That works on my system.

That works on my system.

If "works" means getting a display of 1872356 or some other gibberish number instead of 10 or 20, then yes, it works.

Your code works fine Rubberman. The problem occurs before that. OP, I encourage you to do this experiment. Temporarily make all data members public (just to avoid having to create getters and setters. Again, temporarily). Change line 53 to Rubberman's line 53 and add his function. Now add these lines directly before Rubberman's line 53:

obj2->fun1();
obj1->fun2();
obj1->p->fun1();
obj2->p->fun2();

See if you perhaps see a 10, a 20, some gibberish number, and then the same gibberish number from line 54 after changing line 53 to Rubberman's line 53 (which would prove that Rubberman's code performs as expected. However, garbage in, garbage out. Not saying your code is garbage can-mohan, I'm saying check to make sure that no pointer is pointing to garbage (i.e. uninitialized) because there is no garbage collection in C++.

All this goes back to my original point. What are you trying to do? If the answer is "I'm just experimenting around and seeing what happens", that's an acceptable answer. It's a good way to learn. Just expect to get gibberish with this experimentation sometimes.

Wait a minute. Are you trying to make a call to this function?

const b& b::operator=(const a& obj)

If so, just change line 53 to this.

*obj1 = *obj2;

If you're trying to call your assignment operator, you need to use the objects as parameters, not pointers to the objects. If that's what you were asking, that should solve that.

Thanks AssetNULL, it works after assigning

*obj1=*obj2

. in current scenario both objects
needs to be created on heap so i didn't have any other option and class b needs to use class a's functionality hence assignment was necessary .It is always suggested to use smart pointer instead of raw pointers .
I believe it answers your below two questions as that is the purpose of assignment operator .
Should I do this?
*What am I trying to do? *

@Rubberman : I am not able to compile the code after doing changes suggested by you .
Here is the error which is flashed by the compiler.

error: cannot convert a* to b* in return

Is there any other approach in your mind which doesn't require assignment operator here but still same result can be achieved ?

I believe it answers your below two questions as that is the purpose of assignment operator .

Once I figured out that you were trying to make a call to your already written assignment operator, it made sense and all that was needed was to correct the syntax of line 53. It was originally not obvious to me that that was what you wanted to do.

Line 8 threw me. You have a data member in class a called p which points to an object of type b. However, as far as I can tell, it is never used and it serves no purpose. In this and earlier threads, you've brought up class design issues like which classes should own and be responsibile for tasks in other classes (in particular, memory management / creation / destruction). These classes refer to each other with pointers called p. However, in your design, I see no reason for a to need to have a pointer to a b object, and again, I see nowhere that that data member p is assigned to point to anything, nor is it set to point to NULL (or nullptr). Classes a and b refer to each other. Why? I don't see any reason for an a object to need to get to a b object. A b object needs to be able to access the integer value that a holds, sure, so that part makes sense. If line 8 is not needed, delete it. If it is needed, do something with that pointer. That's why Rubberman's code produced gibberish. It is the equivalent of doing this in main (pretend all data members are public).

b *obj1= new b (10);
a *obj2= new a(20);
obj1=obj2->p; // obj1 and obj2->p both now point to a bad location
obj1->fun2(); // gibberish

I can't read Rubberman's mind, but when he posted that, I thought that he had figured out what you wanted and that it was something I had missed originally. Turns out you wanted to use the assignment operator, which I believe we both missed. His solution is the same as line 3 above, using a public operator in class a since p in class a is private in the actual code. I'm not sure why his code did not compile for you. You put this code as a public method in class a, correct?

operator b* () { return p; }

Again, you never assign p to point to anything, so the printout is gibberish.

Is there any other approach in your mind which doesn't require assignment operator here but still same result can be achieved ?

I think using the assignment operator is fine here. Again, I did not originally understand that that is what you wanted to do.

As far as the "What am I trying to do?" question, I think you should probably expand that question to your overall program instead of just line 53. Again, once I realized what you were trying to do, it was simply a matter of correcting line 53's syntax. Stick a few comments in the code to make it more clear what the overall design is intended to achieve. There is one single data member in the two classes, an integer, that holds any actual data. The whole thing is overly complicated for no apparent reason.

I seem to have made the same points over and over again, so sorry about that. I will end here.

Thanks for your observation. Line No. 8 is not needed there, I am going to remove this from original post as it is creating unncessarliy confusion.
above shared example is dummy one as i can't share the original code. In real time scenario my Device class will have Library class object and after connecting device with port i need to use library object to fetch the device details like temprature and other stuff and due to this scenario external Library object needs to be assign to Device object in order to fetch these details from device.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.