例:
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(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
}
值得小心。 m2
在m3
之前,m3
将留有点人,标明任意停车场。
如果您不想将点数推到nullptr
(NUL
或0
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 A
s and that cycle will continue until you run out of memory.
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 ...
I have been searching for sample code creating iterator for my own container, but I haven t really found a good example. I know this been asked before (Creating my own Iterators) but didn t see any ...
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 ...
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?
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->...
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, ...
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 ...
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?