简体   繁体   English

有一个没有构造函数的类有意义吗?

[英]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 classinstance时,首先将从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.

相关问题 如果构造函数引发异常,那么拥有该类的全局对象是否有意义? - If a constructor throws exception, then does it make sense to have a global object of that class? constexpr移动构造函数是否有意义? - Does a constexpr move constructor ever make sense? 为什么没有引用数组是没有意义的,又如何没有指针就拥有多态对象数组呢? - Why does it not make sense to have an array of references, and how can I have an array of polymorphic objects, without pointers? 类Logger可复制和移动是否有意义? - Does it make sense for a class Logger to be copyable and movable? 何时使用抽象类是有意义的 - When does it make sense to use an abstract class 什么时候对operator ==和operator!=进行单独的实现是有意义的? - When does it make sense to have separate implementations for operator== and operator!=? 流的 unique_ptr 有意义吗? - Does it make sense to have unique_ptr for streams? 使用QThread而不调用QThread :: start()是否有意义? - Does it make sense to use QThread without calling QThread::start()? forward_iterator模板类是否有意义? - Does a forward_iterator template class make sense? 从抽象(纯虚拟)class 私有继承是否有意义? - does it make sense to inherit privately from an abstract (pure virtual) class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM