English 中文(简体)
如何将奶制品列入多图,其中一类是抽象的?
原标题:How: Insert pair into multimap, with one of the types being abstract?

例如:

abstract class goal
class priority
class childgoal

multimap<priority, goal> mm;
mm.insert(make_pair(priority(), childgoal());

我发现错误:

cannot declare field  std::pair<priority, goal>::second 
to be of abstract type  goal 

如果我正确记住,Pair利用违约建筑商在分配之前创造第二位,这是造成痛苦的原因。 我可能不正确,但会解释错误。

我怎么做呢? 在哪一种类型是抽象的时,我怎么能够把这插入一个多图(或可能是地图)。

最佳回答

你们可以绘制一个要点地图:

#include <memory>
#include <map>

class Base { };
class Derived1 : public Base { };
class Derived2 : public Base { };

typedef std::shared_ptr<Base> MyPtr;
typedef std::multimap<Key, MyPtr> MyMap;

int main()
{
  MyMap m { { Key(1), MyPtr(new Derived1) }, { Key(2), MyPtr(new Derived2) } };
}

统一初始化需要C++11支持。 在旧版本中,你可以改为:

m.insert(MyMap::value_type(Key(1), MyPtr(new Derived1)));

如果没有人需要被绘制的物体,那么你可能会拿走<条码>:unique_ptr<Base>。 一个简单的问题,是改变类型。

问题回答

你们显然想要使用多变(因为你想要把一个衍生的阶层只算一个基级)。

但是,每当你想使用多吗).时,你就应当拥有使用点对基的自动化(如果我正确回顾,参考对基准也是有益的)。 假设孩子们公开继承目标,在发言之后是:

  • childgoal is-a goal
  • a pointer to goal can point to goal or to childgoal (or to any derived class)
  • but a goal instance is-NOT-a childgoal

因此,如果你能够把你的童子团划入一个目标(,因为你试图传出一个抽象的“”),那么你就可以把在童年的所有非成员rid掉,从而使你们的原始目标破灭。 我认为这不是你的意图。

If you are afraid about the memory management issue that would arise with classic pointers, you could consider using shared_pointers here :

abstract class goal;
class priority;
class childgoal : public goal;

typedef std::tr1::shared_ptr<goal> goalPtr; //sadly I think std::tr1 is not a multiplatform namespace 

multimap<priority, goalPtr> mm;
goalPtr tmpChildGoal(new childgoal()); //always use named shared pointer !
mm.insert(make_pair(priority(), tmpChildGoal);

Very simply said: abstract classes can only be used as pointers or references. If you need a container of a polymorphic instance of your class you need to store a pointer or, better, a smart pointer, possibly a unique_ptr.





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

热门标签