Зачем нужен список инициализации в конструкторе?
Зачем нужен список инициализации в конструкторе?
Ship::Ship() : name{ nullptr }, type{ nullptr }, displ{ 0 }
Ship.h
#pragma once
class Ship
{
char* name;
char* type;
int displ;
public:
Ship();
Ship(const char* Name, const char* Type, int Displ);
~Ship();
};
Ship.cpp:
#include "Ship.h"
#include <iostream>
#include <cstring>
using namespace std;
Ship::Ship() : name{ nullptr }, type{ nullptr }, displ{ 0 }
{
cout << "Вызван конструктор" << endl;
}
Ship::Ship(const char* Name, const char* Type, int Displ)
{
name = new char[strlen(Name) + 1];
strcpy(name, Name);
type = new char[strlen(Name) + 1];
strcpy(type, Type);
displ = Displ;
cout << "Вызван конструктор с параметрами" << endl;
}
Ship::~Ship()
{
delete[] name;
delete[] type;
cout << "Вызван деструктор" << endl;
}
Source.cpp:
#include "Ship.h"
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "ru");
Ship ship;
}