私有构造函数

发布于 2024-09-25 21:40:09 字数 707 浏览 3 评论 0原文

我有一个具有私有构造函数的类对象:

class CL_GUIComponent
{
    // ...
    private:
    CL_SharedPtr<CL_GUIComponent_Impl> impl;
    CL_GUIComponent(CL_GUIComponent &other);
    CL_GUIComponent &operator =(const CL_GUIComponent &other);
    CL_GraphicContext dummy_gc;
};

我有一个类,它有一个指向我之前描述的类型的对象的指针。

class Some
{
   private:
       CL_GUIComponent *obj;
   public:
       CL_GUIComponent getComp() { return *obj; }
}

但是这段代码调用了错误:

In member function ‘CL_GUIComponent Some::getComp()’:
error: ‘CL_GUIComponent::CL_GUIComponent(CL_GUIComponent&)’ is private
error: within this context

我如何存储和获取该对象?

I have an object of class which has private constructor:

class CL_GUIComponent
{
    // ...
    private:
    CL_SharedPtr<CL_GUIComponent_Impl> impl;
    CL_GUIComponent(CL_GUIComponent &other);
    CL_GUIComponent &operator =(const CL_GUIComponent &other);
    CL_GraphicContext dummy_gc;
};

I have a class which has a pointer to the object of the type I described before.

class Some
{
   private:
       CL_GUIComponent *obj;
   public:
       CL_GUIComponent getComp() { return *obj; }
}

But this code calls the error:

In member function ‘CL_GUIComponent Some::getComp()’:
error: ‘CL_GUIComponent::CL_GUIComponent(CL_GUIComponent&)’ is private
error: within this context

How can I store and get that object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

落花随流水 2024-10-02 21:40:09

返回一个引用:

CL_GUIComponent& getComp() { return *obj; } 

和/或

const CL_GUIComponent& getComp() const { return *obj; } 

您拥有的代码试图返回一个副本,但复制构造函数是私有的,因此它无法访问它(因此出现错误)。无论如何,对于重要的对象,返回 const& 几乎总是更好(一般情况下,并非总是如此)。

Return a reference instead:

CL_GUIComponent& getComp() { return *obj; } 

and/or

const CL_GUIComponent& getComp() const { return *obj; } 

The code you have their is trying to return a copy, but the copy constructor is private so it can't access it (hence the error). In any case, for non trivial objects, it's almost always better to return a const& instead (in general, not always).

护你周全 2024-10-02 21:40:09

通过指针或引用。您无法构建新的副本,因此无法像您的 get 尝试那样返回副本。

By pointer or reference. You can't construct a new one and thus can't return copies, as your get attempts to do.

我很OK 2024-10-02 21:40:09

getComp 返回 CL_GUIComponent 的实例。这意味着 getComp 实际上会复制 obj 所指向的实例。如果您希望 getComp 返回 obj 所指向的实例,请返回对 CL_GUIComponent 的引用,如下所示:

CL_GUIComponent &getComp() {return *obj;}

getComp returns an instance of CL_GUIComponent. This means that getComp will actually make a copy of the instance pointed to by obj. If you want getComp to return the instance where obj is pointing to, return a reference to CL_GUIComponent, like this:

CL_GUIComponent &getComp() {return *obj;}
风和你 2024-10-02 21:40:09

这是不可复制习语在行动中。通过指针或引用返回。

This is non-copyable idiom in action. Return by pointer or reference.

夜灵血窟げ 2024-10-02 21:40:09

使用getComp() 初始化引用。

CL_GUIComponent const &mycomp = getComp();

然后该语言不会尝试调用调用函数内的复制构造函数。 (不过,getComp仍然创建并返回一个副本。)

Use getComp() to initialize a reference.

CL_GUIComponent const &mycomp = getComp();

Then the language doesn't try to invoke the copy constructor inside the calling function. (getComp does still create and return a copy, though.)

哀由 2024-10-02 21:40:09

由于构造函数被声明为私有,因此您必须使用公共成员函数来创建使用私有构造函数的类的对象。

class CL_GUIComponent 
{ 
    // ... 
    private: 
    CL_GUIComponent();
    CL_GUIComponent(CL_GUIComponent &other); 
    CL_GUIComponent &operator =(const CL_GUIComponent &other); 
    public:
    CL_GUIComponent* CreateInstance()
         {
           CL_GUIComponent *obj = new CL_GUIComponent();
         }

};
class Some 
{ 
   private: 
       CL_GUIComponent *obj; 
   public: 
       CL_GUIComponent* getComp() { return (obj->CreateInstance()); } 
};

Since the constructor is declared private, you have to use a public member function to create an object of class that uses the private constructor.

class CL_GUIComponent 
{ 
    // ... 
    private: 
    CL_GUIComponent();
    CL_GUIComponent(CL_GUIComponent &other); 
    CL_GUIComponent &operator =(const CL_GUIComponent &other); 
    public:
    CL_GUIComponent* CreateInstance()
         {
           CL_GUIComponent *obj = new CL_GUIComponent();
         }

};
class Some 
{ 
   private: 
       CL_GUIComponent *obj; 
   public: 
       CL_GUIComponent* getComp() { return (obj->CreateInstance()); } 
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文