英文:
Including base class variables in the initializer list in the derived class
问题
My project is to create a dive logbook.
There is the DiveInfo base class and Equipment derived class.
The equipment may change from dive to dive, so obviously it needs to get the variables for specific dives, according to the given data by the user.
This is the Equipment.h:
#pragma once
#include "DiveInfo.h"
#include <string>
class Equipment : public DiveInfo
//manage and capture information about the diving equip
{
private:
std::string equipmentCondition;
std::string equipmentType;
std::string maskType;
double tankSize;
public:
Equipment(const std::string& diveSiteName, int depth, int duration, double waterTemperature, double visibility, const std::string& notes,
const std::string& equipmentCondition, const std::string& equipmentType, const std::string& maskType, double tankSize);
std::string getEquipmentType() const;
std::string getEquipmentCondition() const;
double getTankSizeI() const;
double getSac() const;
};
I already have a constructor in the DiveInfo for those variables (from diveSiteNames 'til notes).
The question is, do I have to include the DiveInfo's variables in the initializer list for the Equipment.h's constructor?
It may be a bold question as well, but couldn't this be done with some virtual function or with some template? Or would it overcomplicate things completely?
英文:
My project is to create a dive logbook.
There is the DiveInfo base class and Equipment derived class.
The equipment may change from dive to dive, so obviously it needs to get the variables for specific dives, according to the given data by the user.
This is the Equipment.h:
#pragma once
#include "DiveInfo.h"
#include <string>
class Equipment : public DiveInfo
//manage and capture information about the diving equip
{
private:
std::string equipmentCondition;
std::string equipmentType;
std::string maskType;
double tankSize;
public:
Equipment(const std::string& diveSiteName, int depth, int duration, double waterTemperature, double visibility, const std::string& notes,
const std::string& equipmentCondition, const std::string& equipmentType, const std::string& maskType, double tankSize);
std::string getEquipmentType() const;
std::string getEquipmentCondition() const;
double getTankSizeI() const;
double getSac() const;
};
I already have a constructor in the DiveInfo for those variables (from diveSiteNames 'til notes).
The question is, do I have to include the DiveInfo's variables in the initializer list for the Equipment.h's constructor?
It may be a bold question as well, but couldn't this be done with some virtual function or with some template? Or would it overcomplicate things completely?
答案1
得分: 4
以下是翻译好的部分:
每个类应该初始化自己并仅自己。它应该让父类自行初始化。
您可以通过在构造函数初始化列表中的第一步“调用”基类构造函数来实现这一点。
作为一个简单的示例:
struct base
{
int value;
base(int val)
: value{ val }
{
}
};
struct derived : base
{
std::string string;
derived(int val, std::string const& str)
: base{ val }, string{ str }
{
}
};
derived
构造函数初始化列表首先调用 base
类构造函数来进行初始化,然后进行 derived
成员的初始化。
英文:
Each class should initialize itself and only itself. It should let parent classes do their own initialization.
You do this by, first thing in your constructor initializer list, "call" the base class constructor.
As a simple example:
struct base
{
int value;
base(int val)
: value{ val }
{
}
};
struct derived : base
{
std::string string;
derived(int val, std::string const& str)
: base{ val }, string{ str }
{
}
};
The derived
constructor initializer list first invokes the base
class constructor to do its initialization, followed by the derived
member initialization.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论