常量类成员副本构造函数 [英] Const Class Member Copy Constructor

查看:77
本文介绍了常量类成员副本构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include booking.h 
#include< iostream>
booking :: booking(const std :: string p_title,const std :: string p_notice,const category p_category,const person p_person,const booking :: Type p_type,const double p_value):
m_type {p_type },
m_title {p_title},
m_notice {p_notice},
m_person {p_person},
m_category {p_category},
m_value {p_value}
{
std :: cout<< 已创建预订<< std :: endl; //调试消息
}

这些是文件(我需要知道的所有信息

  #pragma一旦
#include< string>
#include person.h
#include category.h
类预订
{
public:
枚举类型{TYPE_REVENUE,TYPE_EXPENDITURE} ;
booking(const std :: string p_title,const std :: string p_notice,const category p_category,const person p_person,const booking :: Type p_type,const double p_value); //基本构造函数
〜booking();
类型GetType();
std :: string GetTitle();
std :: string GetNotice();
类别GetCategory();
double GetValue();

私人:
类型m_type;
std :: string m_title;
std :: string m_notice;
类别m_category;
人m_person;
double m_value;

};

如果我放置一个类成员(如m_type或double值,则不要紧)到const,它会引发以下错误:


Fehler 1错误C2280: booking& booking :: operator =( const booking&):尝试引用已删除的函数C:\Program Files(x86)\Microsoft Visual C ++ Compiler 2013年11月CTP\include\utility 53


我不明白为什么编译器会抱怨副本构造函数以及基本的问题。

解决方案

当您声明 const 成员时,编译器不会生成默认的赋值运算符(没有任何线索)。



注意:

strong>




  • 通过引用const传递参数。


#include "booking.h"
#include <iostream>
booking::booking (  const std::string p_title,  const std::string p_notice,  const category p_category,  const person p_person,  const booking::Type p_type,  const double p_value ) :
m_type{ p_type },
m_title{ p_title },
m_notice{ p_notice },
m_person{ p_person },
m_category{ p_category },
m_value { p_value }
{
    std::cout << "Booking was created" << std::endl; // Debug Message
}

These are the files (everything thats necessary to know in my opinion)

#pragma once
#include <string>
#include "person.h"
#include "category.h"
class booking
{
public:
    enum Type { TYPE_REVENUE, TYPE_EXPENDITURE };
    booking ( const std::string p_title, const std::string p_notice, const category p_category, const person p_person, const booking::Type p_type, const double p_value ); //Basic Constructor
    ~booking();
    Type GetType ( );
    std::string GetTitle ( );
    std::string GetNotice ( );
    category GetCategory ( );
    double GetValue ( );

private:
     Type m_type;
     std::string m_title;
     std::string m_notice;
     category m_category;
     person m_person;
     double m_value;

};

If i put one of the class members (like m_type or the double value, it doesnt matter which) to const, it throws following error:

Fehler 1 error C2280: booking &booking::operator =(const booking &) : attempting to reference a deleted function C:\Program Files (x86)\Microsoft Visual C++ Compiler Nov 2013 CTP\include\utility 53

I dont get why the compiler complains about the copy constructor and whats basicly the matter.

解决方案

When you declare a const member, the compiler does not generate a default assignment operator (it has no clue what to do with this member during assignment, after all, it is const ?), you will have to write the assignment operator yourself.

Note:

  • pass you parameters by reference to const.

这篇关于常量类成员副本构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆