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.

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.

You can't (reasonably) assign to an object of a class that has const members.

That's why you get an error about the copy assignment operator.

You're not getting a complaint about the copy constructor.


In other news:

  • In C++ ALL UPPERCASE names are a convention for macros. If they're used for anything else (e.g. constants, as in Java) then you increase the risk of name collisions and inadvertent text replacement. Besides it's an eyesore, read by many as extra heavy emphasis. Java doesn't have a preprocessor. C++ does have one.

  • It's a good idea to pass non-basic-type arguments in general as reference to const (you only added const). There are some extra considerations for large arguments that are copied. In C++11 these are best passed by value and moved.

  • Simple "getter" member function should be declared const so that they can be called on a const object.

Regarding the Java-inspired Get prefixes, consider GetSin(u)+GetCos(v) versus sin(u)+cos(v). In Java a Get prefix can have some value for tools that use introspection. Java has introspection. C++ doesn't have instrospection. The conventions employed should better be adapted to the language used.


Need Your Help

How to create a personal wiki+blog on github using org-mode?

github blogs wiki org-mode mathjax

I Love org-mode! I tried to create my personal pages using org-export-html. Org-mode can export latex-math using mathjax very well, and many many other feathers. I love that! I want a tidy and beau...

How to add a ItemSorter to a table container in Vaadin?

java-ee sorting containers vaadin order

I have a Container class which is extends from BeanItemContainer. I want to add a ItemSorter to DATE_CREATED attribute to sort the values in descending order.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.