TARGET: I have to copy one object into another object .
I thought of copying it without using assignment or copy contructor but it doesnt compile. SO I have use copy contructor /assignment operator.
ISSUES
. Now I have 2 questions?
1) Do I really need copy constructor & assignment operator ? As far as I know I am not using any pointers in member variables so there is no need to use copy constructor?
Butt then my object is an pointer so may be I need it?
2) IF my below code is right why I am getting error shown below
can not convert from 'const Home_instance *' to 'Home_instance *'
home_data.h
struct home_data
{
QString area_address_;
QVector<QString> home_numbers_;
QMap< QString, QList<QByteArray> > area_to_home_mapping;
};
Home_instance.h
class Home_instance : public QObject
{
Q_OBJECT
public:
Home_instance( const Home_data& data, QObject* parent_p = 0 );
Home_instance();
Home_instance( const Home_instance& rhs );
Home_instance& operator=( const Home_instance& rhs );
~Home_instance();
// some otehr functions
private:
Home_data data_;
}
Home_instance.cpp
//constructors
Firmware_instance::Home_instance( )
{
}
Home_instance::Home_instance( const Home_data& data, QObject* parent_p )
: QObject( parent_p ),
data_ ( data )
{}
Home_instance::Home_instance( const Home_instance& rhs )
{
data_.ip_address_ = rhs.data_.ip_address_;
data_.serial_numbers_ = rhs.data_.serial_numbers_;
data_.serial_to_slot_mapping = rhs.data_.serial_to_slot_mapping;
}
Home_instance& Home_instance::operator=( const Home_instance& rhs )
{
data_.ip_address_ = rhs.data_.ip_address_;
data_.serial_numbers_ = rhs.data_.serial_numbers_;
data_.serial_to_slot_mapping = rhs.data_.serial_to_slot_mapping;
return *this;
}
Home_instance::~Home_instance()
{
// do we actually need destructor ?
}
// in some other Dialog window say Council.h &cpp
class Council
{
// some other variable decl
private:
Home_instance* Home_instance_p_;
Home_data Home_data_p_;
}
in council.cpp
// in constructor
{
connect( udp_p_, SIGNAL( discovered_Home( const Home_instance* ) ), this, SLOT( new_Home( const Home_instance* ) ) );
}
// in function new_home
void Council::new_home( const Home_instance* Home_old_instance )
{
// bla bla
Home_instance_p_ = Home_old_instance; // error can not convert from 'const Home_instance *' to 'Home_instance *'
}
Thanks
This
Home_instance_p_ = Home_old_instance;
doesn't copy the objects, it tries to copy the pointer.
You want
Home_instance_p_ = new Home_instance(*Home_old_instance);
and then delete Home_instance_p_
in ~Council
.
Do I really need copy constructor & assignment operator ?
Normally, I would tell you YES, but when having a class inheriting from QObject, then the answer is NO. QObject classes are not meant to be copied.
See this : How do I copy object in Qt?
can not convert from 'const Home_instance *' to 'Home_instance *'
Yes, because the right hand operator is a const pointer, and left is non-const pointer. There is something called const correctness, that prevent this conversion.
This answers why : newbie question: c++ const to non-const conversion
Assuming that QList, QMap, and QByteArray all have proper copy constructors (or can use the synthesized one) you do not need one, because, as you said, you have no data that requires a deep copy. However, if the map, list, and bytearray do not copy correctly, you would need to implement one (and the best place would be within the Q-classes)
You are trying to copy a pointer to a const object to a pointer to a non-const object. This doesn't have anything to do with your copy constructor. You should just remove the const from the method definition (if Council::home_instance_p_ needs to be used to call non-const methods OR make your member variable const).
1) Do I really need copy constructor & assignment operator ? As far as I know I am not using any pointers in member variables so there is no need to use copy constructor?
If no pointers, no need of copy constructor & assignment operator.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.