English 中文(简体)
无声望者类型/类别病媒
原标题:vector of type/class for typecasting null pointer

根据以下方案,我目前正在使用无标识病媒的类型:

(说明该法典是“原则”,我已经删除了最不必要的内容。)

for (i=0;i<NObjects;i++)
{
    switch (ObjectTypes[i])
    {
    case 1:
        ((File_GUI*) (NullVector[i]))->function();
        break;

    case 2:
        ((Point_GUI*) (NullVector[i]))->function();
        break;

    case 3....etc
    }
}

是否有办法用简单阵列取代大量案件1,案件2等,用于进行播种? 因此,该守则将研究类似情况(当然,必须尽早建立类型CastVector,并包含每个指数的数据类型,即:

for (i=0;i<NObjects;i++)
{
    ((typeCastVector[i]*) (NullVector[i]))->function();
 }

If possible, this would save me tons of lines of codes.

问题回答

I agree with Kerrek SB, a redesign is probably wanted so as to use a virtual function call and a base class for this purpose. If for some reason you really don t want to give File_GUI, Point_GUI, etc a base class you could do this...

struct BaseStorer
{
    virtual void CallFunction() = 0;
};

template<typename T>
struct TypeStorer : public BaseStorer
{
    TypeStorer(T* _obj) : obj(_obj) {}
    virtual void CallFunction() { obj->function(); }

    T* obj;
};

储存一系列基地Storer* ,这些编号为<编码>新类型Storer<File_GUI>new file_GUI(......)。 你们的朋友们希望......

for (i=0;i<NObjects;i++)
{
    baseStorerVector[i]->CallFunction();
}

我的看法与评论中提到的相同。 如今,您的<代码>ObjectTypes[]似乎是一个阵列(或vector of ints>)。 您不妨将其更改为一系列共同基点(<>条码>>>>。 例如,

GUI *ObjectTypes[NObjects];

www.un.org/spanish/ecosoc 等级类似

class GUI {  // abstract base class
public:
  virtual void function () = 0; // make it an abstract method
};

class File_GUI : public GUI {
  void function () { ... }
};
...

现在,你可以以一行代替你的要求。

for (i=0;i<NObjects;i++)
{
    ObjectTypes[i]->function();
}




相关问题
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?

热门标签