If I have my class Fecha
with a constructor similar to this:
Fecha::Fecha (int d, int m,int a): dia_(d),mes_(m), anno_(a)
{
//...
}
And I need to create a date from another, for example:
Fecha mifecha(mifecha2);
Y
Fecha mifecha = otrafecha;
To do that, what kind of constructors would I need? Maybe a copy constructor for the second case, but what about the first?
In both cases the copy constructor is used:
And why in both cases the same constructor is called?
In both cases you are creating a copy of an existing object and that functionality is supported solely and exclusively by the copy constructor. Another different case would be that both objects already exist, in that case the assignment operator would be called:
Namely:
By the way, note that by default the copy constructor and the assignment operator receive a default implementation... so the program compiles without implementing them. Unless you need to perform special operations in these operations, it is preferable to leave that implementation by default... as a general rule, the compiler will know how to manage these functions much better than us.
To understand when you need a copy constructor, an assignment operator, and a destructor, let's first look at when they are called.
When you do this you call the copy constructor:
And when you do this too:
Both call the copy constructor which would be:
The assignment function is called in assignment, and not in the declaration that is preceded by the data type.
Now let's see when you need them. Normally you don't need to define these special functions since C++ does it for you. C++ defines a default assignment operator, a default copy constructor, and a default destructor. Additionally C++11 also gives you a default move copy constructor and a default move assignment operator.
So for your comfort and to have to write less code and therefore to have less error margins you don't need to define any. In addition, a compiler will normally always know how to define these functions better than you.
Another thing is that your class uses pointers. In that case, get ready because it's time to define all these functions. And if the constructors allocate memory the destructor should release it.
That is why with pointers and dynamic memory the default copy constructor and assignment operator are useless since they merely perform a shallow copy or copy member by member, therefore in an assignment or initialization to another object what you get is that both objects hold pointers to the same memory location and try to delete twice there. Which ends up being a disaster, your program aborts and everything goes to hell.
So whether you decide to have c.copy and o.allocate do a deep copy or decide to keep track of static variables to free up that memory just once, it's time to redefine the 5 special functions. Still it is still possible to avoid explicitly defining these functions if for example you use smart pointers which manage their own memory.
Later if you use libraries you will probably find that you don't want your smart pointers to manage memory using new and delete. For example in SDL2 we have pointers that manage textures by loading them with IMG_LoadTexture and SDL_DestroyTexture. Here is a reference for it:
https://stackoverflow.com/questions/27331315/typedef-a-shared-ptr-type-with-a-static-custom-deleter-similar-to-unique-ptr
All the best.
You need the copy constructor. But if you meant " Which constructor do I need to write? " the answer would be: None.
The special functions.
There are some object functions that C++ treats in a special way, these are:
Fecha::Fecha()
.Fecha::Fecha(const Fecha &)
.Fecha::Fecha(const Fecha &&)
.Fecha::~Fecha()
.Fecha &Fecha::operator =(const Fecha &)
.Fecha &Fecha::operator =(const Fecha &&)
.The special treatment they receive is that if the programmer doesn't write them, the compiler will write them automatically. So, assuming your class
Fecha
looks like this:You can do this without writing additional code:
Notice that as much
mifecha1(mifecha)
as theymifecha2 = mifecha1
use the copy constructor.Control of special functions.
Sometimes it is necessary to control the work that the compiler does " in secret ", either for the programmer to show the intent of his code or to prevent these functions from being generated; for this you can declare special functions like
default
ordelete
:When one of the special functions is marked as
default
indicating that the responsibility of writing that function is delegated to the compiler, marking asdelete
prohibits the compiler from generating that function; so with the code above it is allowed toFecha
be built by default but it is forbidden to copy it with the copy-assign operator.Back to the question:
This would be enough: