简体   繁体   English

我需要复制构造函数吗? 以及如何使用复制构造函数将const对象复制到非const对象

[英]do i need copy constructor ? and how copy const object to non const object using copy constructor

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 ? 1)我真的需要复制构造函数和赋值运算符吗? 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 2)如果我的以下代码正确,为什么我会收到以下所示的错误
can not convert from 'const Home_instance *' to 'Home_instance *' 无法从“ const Home_instance *”转换为“ 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 . 然后delete Home_instance_p_~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继承一个类时,答案是“否”。 QObject classes are not meant to be copied. QObject类不是要复制的。

See this : How do I copy object in Qt? 参见: 如何在Qt中复制对象?

can not convert from 'const Home_instance *' to 'Home_instance *' 无法从“ const Home_instance *”转换为“ Home_instance *”

Yes, because the right hand operator is a const pointer, and left is non-const pointer. 是的,因为右手运算符是const指针,而左运算符是非const指针。 There is something called const correctness, that prevent this conversion. 有一种称为const正确性的东西可以防止这种转换。

This answers why : newbie question: c++ const to non-const conversion 这回答了为什么: 新手问题:C ++ const到非const转换

  1. 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. 假设QList,QMap和QByteArray都具有适当的副本构造函数(或可以使用合成的副本构造函数),则您不需要一个副本构造函数,因为正如您所说的,您没有需要深层副本的数据。 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) 但是,如果映射,列表和字节数组未正确复制,则需要实现一个(最好的位置在Q类内)

  2. You are trying to copy a pointer to a const object to a pointer to a non-const object. 您试图将指向const对象的指针复制到指向非const对象的指针。 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). 您应该只从方法定义中删除const(如果需要使用Council :: home_instance_p_来调用非const方法或将成员变量设为const)。

1) Do I really need copy constructor & assignment operator ? 1)我真的需要复制构造函数和赋值运算符吗? 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. 如果没有指针,则不需要复制构造函数和赋值运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM