English 中文(简体)
设法在一阵列中储存物体,然后如何使用该物体的方法?
原标题:Trying to store an object in an array but then how to call that object s methods?
  • 时间:2012-01-14 02:00:32
  •  标签:
  • c++

我不是一位经验非常丰富的C++编码员,我 st。 我通过一个物体(在别处被点燃),履行一项职能,我希望能够把该物体储存在某些阵列中,然后通过阵列来,要求该物体发挥功能。 这里有一些伪装:

void AddObject(T& object) {
  object.action(); // this works

  T* objectList = NULL;
  // T gets allocated (not shown here) ...
  T[0] = object;
  T[0].action(); // this doesn t work
}

我知道该物体的通过是正确的,因为第1次要求<编码>目标.action()是应该做的。 但是,当我储存阵列中的物体时,它试图援引<编码>action(>)造成重大故障。

Likely my problem is that I simply tinkered with the . s and * s until it compiled, T[0].action() compliles but crashes at runtime.

最佳回答

回答你的问题的最简单答案是,你必须正确申报其集装箱,而且你必须确定一个适当的舱位操作员。 尽可能从您的榜样中密切合作:

typedef class MyActionableClass T;
T* getGlobalPointer();

void AddInstance(T const& objInstance)
{
    T* arrayFromElsewhere = getGlobalPointer();

//ok, now at this point we have a reference to an object instance
//and a pointer which we assume is at the base of an array of T **objects**
//whose first element we don t mind losing

//**copy** the instance we ve received
    arrayFromElsewhere[0] = objInstance;

//now invoke the action() method on our **copy**
    arrayFromElsewhere[0].action();

iii

注 签字改为const reference<>,其中强调,我们将复制原物体,而不会以任何方式改变原物体。

∗∗∗∗∗ 由于你提供了一份副本,因此与服从行为一样——正在不同情况下援引行动,无论情况如何类似。

虽然你显然感到迷惑不解,但冷却使得这样做的理由不那么明显——例如,你想要维持一系列的退约目标,会更好地证明这种能力“需要”。 如同你一样,使用“T”也是很不好的选择,因为这往往意味着对大多数经验丰富的C++方案制定者的模板使用。

造成你“无法解释”的坠机最有可能的是,派任经营人;如果你不给人定义,汇编者将自动产生一份双向复印件——几乎肯定是而不是——如果你的班子不是收集简单数据类型(POD)。

为使这项工作能够妥善进行一系列复杂的工作,你可能需要确定一个深厚的复印件或参考数;在C++中,让汇编者为你创建任何导师、导师或派任几乎总是很难。

当然,使用标准集装箱,而不是你所举的例子所暗示的简单阵容机制,是一种好的想法。 在这种情况下,由于集装箱和算法的假设,你可能还应界定违约方、虚拟拖车和复印机。

事实上,如果你真的希望(<>不是)在<>原始<<>物体上,但从一阵列中援引行动,那么你就需要有一系列的点。 再次与你原来的榜样密切合作:

typedef class MyActionableClass T;
T** getGlobalPointer();

void AddInstance(T& objInstance)
{
    T** arrayFromElsewhere = getGlobalPointer();

//ok, now at this point we have a reference to an object instance
//and a pointer which we assume is at the base of an array of T **pointers**
//whose first element we don t mind losing

//**reference** the instance we ve received by saving its address
    arrayFromElsewhere[0] = &objInstance;

//now invoke the action() method on **the original instance**
    arrayFromElsewhere[0]->action();

iii

密切地注意到阵列 从Elseland开始,现在有一系列的点,而不是一系列实际物体。

请注意,我在此案中投下了最热烈的词句,因为我不知道行动是否是一种最连贯的方法,而我所假设的名号是......

Note carefully the ampersand (address-of) operator being used in the assignment.

说明还使用了点到操作员来援引(a)方法的新辛迪加。

Finally be advised that using standard containers of pointers is fraught with memory-leak peril, but typically not nearly as dangerous as using naked arrays :-/

问题回答

我感到惊讶的是,它汇编了材料。 页: 1 然后,您指定<代码>T[0] = 标的;。 这不是你想要的东西,你想要的是什么。

T objectList[8];
objectList[0] = object;
objectList[0].action();

T *objectList[8];
objectList[0] = &object;
objectList[0]->action();

Now I m waiting f或 a C++ expert to explain why your code compiled, I m really curious.

您可以将物体置于动态或静态阵列之中:

#include <vector> // dynamic
#include <array>  // static

void AddObject(T const & t)
{
    std::array<T, 12> arr;
    std::vector<T>      v;

    arr[0] = t;
    v.push_back(t);

    arr[0].action();
      v[0].action();
}

虽然如此,你通常在职能之外其他地方界定了你的阵列。





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