首页 \ 问答 \ 抽象类的C ++工厂方法模式(C++ factory method pattern for abstract class)

抽象类的C ++工厂方法模式(C++ factory method pattern for abstract class)

我必须在C ++中实现工厂方法模式。 由工厂“组装”的类(C)是抽象的,并由其他类(D)继承。 所以我不想C有一个构造函数。 但是为了创建一个D的实例,C必须至少有一个受保护的构造函数。 但是,有人可能从C派生出来,并以这种方式创建C的一个实例。 我不希望发生这种情况。 所以我创建了一个受保护的构造函数,它接受一个指向C对象的指针以供派生类使用,代码如下所示。 问题是这是否是解决这个问题的正确方法。

class C {
private:
 C() {}
protected:
 C(const C* c) {}
friend class CFactory
};

class D: public C
{
private:
 D(const C* c): C(c) {}
friend class CFactory;
};

class CFactory
{
public:
 static C* createC() {
  C* ptr = new C();
  ptr = new D(ptr); // There is garbage collection on the project, so no memory leak.
 }
};

I have to implement a factory method pattern in C++. The class (C) that is to be "assembled" by the factory is abstract and is inherited by some other class (D). So I don't want C to have a constructor. But to create an instance of D, C has to have at least a protected constructor. But then someone might derive from C and create an instance of C in this way. I don't want this to happen. So I created a protected constructor that takes a pointer to a C object for derived classes to use, code is below. The question is whether that is the correct way of dealing with this issue.

class C {
private:
 C() {}
protected:
 C(const C* c) {}
friend class CFactory
};

class D: public C
{
private:
 D(const C* c): C(c) {}
friend class CFactory;
};

class CFactory
{
public:
 static C* createC() {
  C* ptr = new C();
  ptr = new D(ptr); // There is garbage collection on the project, so no memory leak.
 }
};

原文:https://stackoverflow.com/questions/6295248
更新时间:2023-08-17 22:08

最满意答案

一个可能的问题可能是您的GXT版本。 如果您使用的是GXT 3.1.1(这是最新的GPL版本),那么您就无法使用GWT 2.7.0。

看一下版本表:

GXT版本

如您所见,Sencha GXT 3.1.1不支持GWT 2.7.0。 第一个支持GWT 2.7.0的GXT版本是GXT 3.1.2。


One possible problem could be your version of GXT. If you are using GXT 3.1.1 (which was the latest GPL-release), then you can not use GWT 2.7.0.

Take a look at the version table:

GXT versions

As you can see, Sencha GXT 3.1.1 does not support GWT 2.7.0. The first GXT release which will support GWT 2.7.0 is GXT 3.1.2.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。