在派生类中将基类变量包含在初始化列表中。

huangapple go评论107阅读模式
英文:

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 &quot;DiveInfo.h&quot;
#include &lt;string&gt;

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&amp; diveSiteName, int depth, int duration, double waterTemperature, double visibility, const std::string&amp; notes,
			  const std::string&amp; equipmentCondition, const std::string&amp; equipmentType, const std::string&amp; 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&amp; 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.

huangapple
  • 本文由 发表于 2023年8月4日 21:00:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76836165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定