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;
}

Recommended Answers

All 14 Replies

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.

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.

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.

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

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

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.

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.

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.

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.

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.

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;
};

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

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.