常量类成员副本构造函数

编程入门 行业动态 更新时间:2024-10-26 21:25:42
本文介绍了常量类成员副本构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

#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.

更多推荐

常量类成员副本构造函数

本文发布于:2023-11-22 07:06:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616433.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:常量   副本   函数   成员

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!