Const 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 files (all there is 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 double, it doesn't matter which) to const, it throws the following error:
Fehler error 1 C2280::
booking &booking::operator =(const booking &)
Attempt to reference a deleted function C: \ Program Files (x86) \ Microsoft Visual C ++ Compiler November 2013 CTP \ include \ utility 53
I don't understand why the compiler is complaining about the copy constructor and what is the matter.
source to share
When you declare a member const
, the compiler does not generate a default assignment operator (it doesn't know what to do with that member during assignment, is it const after all?), you'll have to write the assignment operator yourself.
Note:
- pass you parameters by const reference.
source to share
You cannot (intelligently) assign a class object with const
members.
This is why you are getting an error with the copy assignment operator.
You don't get any complaints about the copy constructor.
In other news:
-
In C ++, all UPPERCASE names are a convention for macros. If they are used for anything else (like constants, as in Java), you increase the risk of name collisions and accidental text replacement. Plus, it's an eyesore read by many as an extra heavy emphasis. Java has no preprocessor. C ++ has one.
-
It's a good idea to pass non-base type arguments in general as a reference to
const
(you only addedconst
). There are a few additional considerations for large arguments that are copied. In C ++ 11, they are best passed by value and moved around. -
Simple getter member function must be declared
const
so that they can be caused by an objectconst
.
As for Get
Java-based prefixes , consider GetSin(u)+GetCos(v)
the comparison with sin(u)+cos(v)
. In Java, the prefix Get
can have some meaning for tools that use introspection. Java has introspection. C ++ doesn't have introspection. The conventions used are best adapted to the language used.
source to share
operator=
is not a copy constructor, it is an assignment operator.
const
objects cannot be updated, so in your assignment statement, you cannot modify the object.
If you don't declare your own assignment operator, the compiler generates one for you that copies the member. But if there is a constant member, that doesn't work, so it can't generate an assignment operator in the end. (In C ++ 11, this is called the remote assignment operator).
Finally, if you have code that is trying to use an assignment operator, you will get this error about trying to use a remote assignment operator. Some standard containers or library algorithms require an assignment operator. You didn't show all of your code, but somewhere you tried to perform an operation requiring an assignment.
source to share
referring to this thread , as for the constant member copy constructor,
general view of the copy constructor:
class Foo {
Foo( const Foo& f ) :
mem1( f.mem1 ), mem2( f.mem2 ) /* etc */
{}
};
where mem1 and mem2 are data members of Foo, which can be const members, non-const members, constant references, or non-const references.
source to share