抽象类的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
最满意答案
一个可能的问题可能是您的GXT版本。 如果您使用的是GXT 3.1.1(这是最新的GPL版本),那么您就无法使用GWT 2.7.0。
看一下版本表:
如您所见,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:
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.
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
一个可能的问题可能是您的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-rele ...
-
GWT Maven多模块(GWT Maven Multi Module)[2021-08-25]
一个gwt库应该有.java和.class文件。 所以我们需要打包这两个文件(也可以有.css,.png,.html等文件)。 你可以使用下面的pom.xml配置来包含它们。${webappDirectory}/WEB-INF/classes src/main/resources ... -
gwt编译期间的GWT验证错误 - 没有可用的源代码(GWT validation error during gwt compile - No source code available)[2023-10-30]
GWT是针对Hibernate Validator 4.1.0-Final构建的,并依赖于它的一些内部( PathImpl , NodeImpl ),显然在Hibernate Validator 4.2.0中已经发生了变化。 使用4.1.0-Final(或可能是样本中的4.0.2.GA)而不是4.2.0。 可以在GWT Tracker中跟踪问题 ,我们应该至少明确记录不兼容性。 GWT is built against Hibernate Validator 4.1.0-Final and relies o ... -
我遇到了同样的问题。 事实证明我有一些参考2.4版本的gwt-dev.jar。 确保你没有,然后再试一次。 I experienced the same issue. it turns out i had some references to the 2.4 version of gwt-dev.jar. make sure you don't have any and try again.
-
尝试删除详细的Soyc参数。 我也遇到过这个问题; 我生成SOYC没问题,然后它开始失败,所以我调整了我的设置,直到我得到了SOYC。 启用了json soyc并禁用了详细的Soyc,我仍然可以获得大量详细信息,告诉我编译大小的来源。 这是我目前正在使用的: -XjsInteropMode JS -XmethodNameDisplayMode NONE -Xnamespace NONE -style OBFUSCATED -sourceLevel 1.8 -compileReport -Xnocompile ...
-
从lib中删除了gwt-dev.jar,它工作得很好。 我完全不知道它的东西也可能造成问题。 REMOVED THE gwt-dev.jar from the lib and it worked totally fine. Something i was totally unaware of it could also create problem.
-
查看CellBasedWidgetImplStandard.java的源代码,我可以确切地看到它失败的原因。 它传递给DOM.dispatchEvent()的第二个参数是: Lcom/google/gwt/user/client/Element; 在最新版本的GWT中,该方法的第二个参数是: Lcom/google/gwt/dom/client/Element; 请注意两者之间的包名称的差异。 这是GWT 2.6中的一个变化(如果我没记错的话),所以我认为这里的解决方案是找到DnD库的更新版本。 也许这 ...
-
因为GWT的JRE仿真不支持编译器抱怨的类。 您应该仅在服务器端使用它们。 有关更多信息,请参阅GWT JRE仿真参考 。 Because the classes which are compiler is complaining are unsupported by GWT's JRE emulation. You should use them on the server-side only. See GWT JRE Emulation Reference for more information.