English 中文(简体)
如何在C++没有动态分配(如新)的物体内向点人提出反对?
原标题:How to initialize an object to a pointer inside an object without dynamic allocation(ie NEW) in C++?

例:

class A{

int b;
A *a1;
A *a2;

A(int c):b(c), a1(c), a2(c)  {}

}

I thought this was the way, but it doesn t compile. Is there a way to do this, or is it necessary to always use dynamic allocation? thanks

最佳回答

你可以简单地把要点推向无效。

class A{

   int b;
   A *a1;
   A *a2;

   A(int c):b(c), a1(NULL), a2(NULL)  {}

}

(注,如果它抱怨未界定的NCL,#include <cstddef>)

Or not initialize them at all.

class A{

   int b;
   A *a1;
   A *a2;

   A(int c):b(c)  {}

}
问题回答

It s not clear what you re trying to do, but you certainly can do it:

class A{

int b;
A *a1;
A *a2;

A(int c, A* pa1, A* pa2):b(c), a1(pa1), a2(pa2)  {}

}

你可以这样说:

A m1(1, NULL, NULL);
A m2(2, NULL, NULL);
if(something())
{
  A m3(3, &m1, &m2);
  // do stuff with m3
}

值得小心。 m1或m2m3之前,m3将留有点人,标明任意停车场。

如果您不想将点数推到nullptr(NUL0 pre-C++11)上,那么:

class A {
    A(int c) : b(c), a1(), b1() { }

    int b;
    A* a1, *a2;
};

then either the object you make the pointer point to has to exist in some outer scope so that it will live at least as long as the object that contains the pointer, or you have to use new.

You could accept an A* to set the pointers to:

class A {
    A(int c, A* aptr) : b(c), a1(aptr), a2(aptr) { }

    ... 
};

(因为这样将形成一个<代码>) A a demand for an A, an possible conditions to hold. 你们可以再造一个接受参考的建筑商。

In your example, you are trying to use an int to initialise a pointer which won t work. Also, if you create an object in your example (via new or whatever), you will run out of memory because each A you allocate will allocate two more As and that cycle will continue until you run out of memory.





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

热门标签