Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Const Class Member Copy Constructor

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

like image 531
user3718058 Avatar asked Sep 30 '14 23:09

user3718058


1 Answers

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.
like image 189
quantdev Avatar answered Sep 25 '22 01:09

quantdev