Hi Guys,
In below code you can see that i am able to execute the functionality of class a , inside the class b
through composition and object of b can be used to call the methods declared inside the a class.

#include <iostream>
using namespace std;
class a 
{
int a1;
public:
a( int _a1=0):a1(_a1)
{
}
void showname()
{
cout<<"class name a with value ="<<a1<<endl;  
}
};
class b
{
public:
b(a* cobj) : cobj(cobj)
{
cout<<"object initialization\n";  
}
void display()
{
 cobj->showname();  
}
~b ()
{
cout<<"deleted the object of a"<<endl;
delete cobj;    
}
private:
a *cobj;    
};

int main() {
a aobj(10);
b bobj(&aobj);  
bobj.display(); 
}

Now same thing i want to achieve through assignment operator and for the same i have overloaded the assignment operator inside the class b but
i am getting compilation error after executing below code snippet :
error:
cannot convert 'a' to 'b' in assignment

  bobj=aobj;
      ^

and due to this error it seems to be bobj->showname(); is not also functional as still content of aobj is not copied to bobj and it is still behaving
class b's object. I never been able to perform assignment between different kind of objects. Please suggest.
code snippet:

#include <iostream>
using namespace std;
class a 
{
int a1;
public:
a( int _a1=0):a1(_a1)
{
}
void showname()
{
cout<<"class name a with value ="<<a1<<endl;  
}

};

class b
{
public:
 const b& operator=(const b& rhs);
private:
a *cobj;    
};
 const b& b::operator=(const b& rhs)
{
a *origobj=cobj;
cobj=new a (*rhs.cobj);
delete origobj;
return *this;
}
int main() {
    a *aobj=new a;
    b *bobj=new b;
    bobj=aobj;
    bobj->showname();
    return 0;
}
Re: assignment operator overlaoding in context of different objects 80 80

The code looks dicey, especially how you incorrectly delete cobj in the first code. What are you really trying to accomplish with this? The class and variable names aren't enlightening as to the underlying design problem you want to solve here.

Re: assignment operator overlaoding in context of different objects 80 80

When doing operations like this you should look at using the copy and swap idiom.

Re: assignment operator overlaoding in context of different objects 80 80

Hi,
It is not incorrect way to delete the cobj operator.I am taking backupof this object and then performing the new operation , if new is successfull then only backup object will be deleted . It also take care of self assignment. Please refer scot meyers 55 way to improve the design book for more details.

Re: assignment operator overlaoding in context of different objects 80 80

It is not incorrect way to delete the cobj operator.

In your first snippet (you'll notice, that's what I referred to before), it very much is incorrect because the object passed into the class was not allocated using new.

Re: assignment operator overlaoding in context of different objects 80 80

I did the fix by removing delete a inside the destructor. Now could you let me know why after assignment i am getting error like a* can't be assigned to b* or vice versa

Re: assignment operator overlaoding in context of different objects 80 80

Can you post the code you have now? It's hard to read your screen from here.

Re: assignment operator overlaoding in context of different objects 80 80

could you let me know why after assignment i am getting error like a* can't be assigned to b* or vice versa

Because they're not compatible types. An a is not a b or convertable to a b, and subsequently a pointer to a is not a pointer to b. C++ is strongly typed, which means without careful scaffolding to support mixing and matching of otherwise unrelated classes, you're going to get errors like this.

So you've spent this thread explaining what you're trying to do, I'd still like to know why you want to do it. What's the problem being solved? There's very likely a better way to complete the higher level task than the way you're going about it.

Re: assignment operator overlaoding in context of different objects 80 80

I agree with the comments so far that you probably need to explain what it is that you're trying to acheive when writing this code. All these raw pointers are generally not necessary in modern C++.

Further to what Decepticon said about your assignment operator, your programm main looks like this:

int main() {
    a *aobj=new a;
    b *bobj=new b;
    bobj=aobj;
    bobj->showname();
    return 0;
}

So, when you do the assignment bobj = aobj, what you're actually doing is assigning a pointer to an object of type a to a pointer of type b. This is a differnt proposition to assigning an object of type a to one of type b. You'll be able to do it with reintepret_cast. (You wouldn't want to in this case because it would be plainly wrong. However, that's how to convert one type of pointer to another).

Even if the compiler knew how to correctly assign a pointer to an a to a pointer to a b, you wouldn't want to do that here either, as you'd be leaking the memory from that you allocated for the original b.

Re: assignment operator overlaoding in context of different objects 80 80

I can't use smart pointers like unique_ptr in my assignment as code needs to be executed in embeded environment. My job is here to assign different objects and i got to know it is possible in c++ to assign different objects.apart from this i have shared one more solution (composition) so just want to cross verify with you guys.

Re: assignment operator overlaoding in context of different objects 80 80

I would like to explain the same by giving more realistic problem;

class Bitmap{};
class widget
{

private:
Bitmap * obj;
};

here widget class contain obj which is heap allocated and there is no relation between bitmap and widget class.Now how would you assign you initialise bitmap object by assigning other bitmap object through widget object.

Re: assignment operator overlaoding in context of different objects 80 80

and there is no relation between bitmap and widget

Well, they're still coupled together in that widget knows about and contains a Bitmap. The problem from before is treating the two classes as entirely compatible, which isn't the case. Here, the usual method would be to provide a constructor, member function, friend function, or operator that handles the initialization/assignment (given that your Bitmap member is private) and simulates that compatibility.

Another problem is how much visibility you're providing from Bitmap. Is the type opaque to widget? Because if it is, you're extremely limited in what you can do with a pointer to Bitmap from within widget.

Re: assignment operator overlaoding in context of different objects 80 80

good observation.let me explain with more code here.

class widget
{
public:
const widget& operator=(widget &  rhs)
{
Bitmap *origobj=obj;// backup of original pb
obj=new widget (*rhs.obj);//make pb point oto a copy of *pb
delete origobj;//delete the original pb
return *this
}

private:
Bitmap *obj;
};
Re: assignment operator overlaoding in context of different objects 80 80

Does the Widget own its Bitmap, or does something else own it?

Re: assignment operator overlaoding in context of different objects 80 80

widget own Bitmap and assignment is happening through widget's copy constructor. It is simialr to the way smart pointer class does this operation (copy constructor and assignment operator) for the other classes.

Be a part of the DaniWeb community

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