[英]Does it make sense to have a class without constructor?
I am wondering whether it makes sense to have a class without constructor if the derived classes have one. 我想知道如果派生类有一个没有构造函数的类是否有意义。 If not, is there a way of doing what I want differently?
如果没有,有没有办法做我想做的事情?
My case is the following: I want the users to only use the derived classes, but those have data members and methods in common. 我的情况如下:我希望用户仅使用派生类,但这些用户具有共同的数据成员和方法。
EDIT: as default constructors exist for classes, it means that the user can always create an instance of Instrument directly, then how can I do what I want? 编辑:由于存在类的默认构造函数,这意味着用户始终可以直接创建Instrument的实例,那么我该怎么做呢?
class instrument{
public:
double get_price();
std::string get_udl();
private:
double m_price;
std::string m_udl;
};
class stock : public instrument{
public:
double get_dividend();
private:
double m_dividend;
};
class option : public instrument{
public:
double get_strike();
private:
double m_strike;
};
I want the users to only use the derived classes, but those have data members and methods in common
我希望用户仅使用派生类,但这些用户具有共同的数据成员和方法
Have your default (or any other) c'tor, but declare it with protected
access. 使用默认值(或其他任何默认值),但使用
protected
访问权限对其进行声明。 Now only derived classes may invoke it. 现在,只有派生类可以调用它。 Effectively making it usable only when inherited.
有效地使其仅在继承时才可用。
class instrument{
public:
double get_price();
std::string get_udl();
protected:
instrument() = default;
private:
double m_price;
std::string m_udl;
};
This also avoids the overhead associated with making a class abstract. 这也避免了与使类抽象相关的开销。 If you don't need polymorphism, you shouldn't use it just to make a class usable only as a base class.
如果您不需要多态性,则不应仅使用它来使一个类只能用作基类。
Have thought of Abstract class ? 有没有想到抽象课?
An abstract class is a class that is declared abstract—it may or may not include abstract methods.
抽象类是被声明为抽象的类-它可能包含也可能不包含抽象方法。 Abstract classes cannot be instantiated, but they can be subclassed.
抽象类不能实例化,但是可以被子类化。
Isn't this what you are looking for ? 这不是您要的东西吗?
When there's no default constructor defined, the compiler will automatically generate one for you, doing nothing for members of basic types and calls the default constructor for members of struct/class types. 如果没有定义默认构造函数,则编译器将自动为您生成一个,对基本类型的成员不执行任何操作,并为struct / class类型的成员调用默认构造函数。
So the conclusion is, it makes no sense to have a class without any constructor because whenever you define an instance of that class you have to call at least one (more may be called with deligate construction). 因此得出的结论是,没有任何构造函数的类是没有意义的,因为每当定义该类的实例时,您都必须调用至少一个 (如果使用deligate构造,则可能会调用更多实例)。
Yes! 是! Definitely it make sense.
绝对有道理。 Because when user create the
instance
of derived
class
, first of all parent
class's constructor
will be called from within the derived
class
constructor
. 因为当用户创建
derived
class
的instance
时,首先将从derived
class
constructor
内部调用parent
类的constructor
constructor
。 Even without you explicitly call it. 即使没有您显式调用它。 And
parent
class's default
constructor
will be called even if you didn't create one. 即使您没有创建
parent
类的default
constructor
也会被调用。 By default 默认
public constructor with no parameter is present in every class
if it has no overridden constructor.
And yes all the public members of parent class will be present in derived class
after creating derived class instance
. 是的,创建
derived class instance
后,父类的所有公共成员都将出现在derived class
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.