English 中文(简体)
我如何能够动态地创造相关的类型?
原标题:How can I create related types dynamically?

www.un.org/Depts/DGACM/index_spanish.htm 我如何动态地确定与另一个类别相关的类别?

我提出了一个解决办法,唯一的问题是,我最终必须使用一个定义,必须在所有衍生类别中使用这一定义。

www.un.org/Depts/DGACM/index_spanish.htm 是否有更简单的方式来做到这一点,就不必再加一门课?

值得注意的是:该阶级和相关班级将永远都有各自的基类,不同班级可以分享相关班级,例如,我喜欢控制班级有自己的观点。

这就是我现在的情况。 我必须放弃的唯一事情是开关,但我只想让开关,而我只需要加上背弃义和改变它。

#include <iostream>
#include <string>

class model 
{
public:
  model( int id ) : id(id) {}

  int id;
};

class view 
{
public:
  view( model *m ) {}

  virtual std::string display() 
  {
     return "view";
  }
};

class otherView : public view 
{
public:
  otherView( model *m ) : view(m) {}

  std::string display() 
  {
     return "otherView";
  }
};


class control 
{
public:
  control( model *m ) : m_(m), v_( createRelated() ) {}

  ~control() 
  { 
     delete v_; 
  }
  std::string display() 
  {
     if ( v_ )
        return v_->display();
     return "No view";
  }

  view *createRelated() 
  {
     switch( m_->id )  
     {
     case 0:
       return new view( m_ );
     case 1:
       return new otherView( m_ );
     default:
       return NULL;
     }
  }

  model *m_;
  view  *v_;
};

int main( void ) {
  model m(0);
  model om(1);
  model nm(2);

  control c1( &m );
  control c2( &om );
  control c3( &nm );

  std::cout << c1.display() << std::endl;
  std::cout << c2.display() << std::endl;
  std::cout << c3.display() << std::endl;
}
最佳回答

可能的解决办法是改变模式,以接受设立相关类别的职能,而不是通过:

typedef view* (*make_function)(model*);

class model
{
public:
    model(make_function a_make) : make_(a_make) {}
    view* make() const { return make_(this); }
...
private:
    make_function make_;
};

每一条<代码>view a. 提供静态方法的班级,从而形成一种实例:

class view
{
public:
    static view* make(model* m) {  return new view(m); }
};

class otherView: public view
{
public:
    static view* make(model* m) {  return new otherView(m); }
};

随之而来的是:

view* control::createRelated()
{
    return m_->make();
}

例用:

model m1(&view::make);
model m2(&otherView::make);

希望会有所帮助。

问题回答

http://en.wikipedia.org/wiki/Factory_method_pattern” rel=“nofollow”

你可能需要一些虚拟职能。 即便通过A类的B类物体:

void f(A &objA) {
  objA.f();
}

int main() {
  B objB;
  f(objB);
  return 0;
}

如果A:f()被定义为虚拟,则B:f()将予援引。 因此,你们不需要知道这种 ob。 A ob B. 出席情况

http://en.wikibooks.org/wiki/More_C++_Idioms/Virtual_Constructor”rel=“nofollow” 虚拟信使。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签