简体   繁体   English

两个默认构造函数,一个是公共的,一个是私有的

[英]two default Constructor, one in public and one in private

My question is about constructors in OOP(C++).我的问题是关于 OOP(C++) 中的构造函数。 When I define default constructor in a class as private and when I initialize an object of that class in main as default then error occurs that default constructor is inaccessible.当我将类中的默认构造函数定义为 private 并且当我在 main 中将该类的对象初始化为默认值时,就会发生默认构造函数不可访问的错误。 It's fine.没关系。 But then I also make default argument constructor in Public section and when I initialize object in main again then ambiguous call to function overloading occurs.但是随后我也在 Public 部分创建了默认参数构造函数,当我再次在 main 中初始化对象时,就会发生对函数重载的模棱两可的调用。 So my question is that if private constructor is not accessible from main then compiler should call the constructor in public section which is default argument constructor.所以我的问题是,如果不能从 main 访问私有构造函数,那么编译器应该调用公共部分中的构造函数,这是默认参数构造函数。 Kindly answer why is this happening.请回答为什么会发生这种情况。

Whether or not some scope of your program is allowed to access functions and/or instantiate class types is decided by the compiler after overload resolution has been performed.是否允许程序的某个范围访问函数和/或实例化类类型由编译器执行重载解析决定。 That means available constructors are not "filtered" by their private or public visibility.这意味着可用的构造函数不会被它们的privatepublic可见性“过滤”。

In your scenario, that might not make immediate sense when looking at the main function, from which you seem to instantiate objects of the class in question.在您的场景中,这在查看main函数时可能没有直接意义,您似乎从中实例化了相关类的对象。 But imagine you create an instance of the class from with a member function of that class: here, both private and public members are visible, and the compiler won't be able to decide which one it should take.但是想象一下,您使用该类的成员函数创建了该类的一个实例:在这里, private成员和public成员都是可见的,编译器将无法决定应该采用哪一个。

As a side note, if you don't want your class to be created by a default ctor, prefer to = delete it.作为旁注,如果您不希望您的类由默认构造= delete创建,请选择= delete它。 Also, a default constructor and one with a defaulted single argument could certainly be refactored into two constructors, eg using in-class initializers.此外,一个默认构造函数和一个带有默认单个参数的构造函数当然可以重构为两个构造函数,例如使用类内初始化器。

For Example take a class例如上课

#include "iostream"

class Type
{
  private:
    Type()
    {
        std::cout<<"Private Default";
    }
  public:
    Type()
    {
        std::cout<<"Public Default";   
    }
};

int main()
{
    Type obj;
}

Here both default constructor is in scope Type::Type() You can't overload like this ie there is no private scope or public scope all are in Type scope So you can't overload this according to c++ overloading rules.这里两个默认构造函数都在Type::Type()范围内你不能像这样重载,即没有私有范围或公共范围都在Type范围内所以你不能根据 c++ 重载规则重载它。

Output for above code:以上代码的输出:

main.cpp:11:5: error: ‘Type::Type()’ cannot be overloaded
     Type()
     ^~~~
main.cpp:6:5: error: with ‘Type::Type()’
     Type()
     ^~~~
main.cpp: In function ‘int main()’:
main.cpp:19:10: error: ‘Type::Type()’ is private within this context
     Type obj;
          ^~~
main.cpp:6:5: note: declared private here
     Type()
     ^~~~

And if you comfort with c++11 you can delete constructor like如果您对c++11感到满意,则可以删除构造函数,例如

class Type
{
  public:
    Type() = delete;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM