English 中文(简体)
• 私人成员类别?
原标题:class types as private members, MVP?
  • 时间:2011-10-13 11:57:06
  •  标签:
  • c++

由于四舍五入,如果你在一类班头上有一个私人成员定义,这种定义就是:

box newBox;

盒子里有一个构造者,不接受任何论点,这是否意味着你必须把它当作点子或参考物?

你们如何处理这个问题? 似乎难以以这种方式宣布某些课堂,而不是接受任何论点的人。 或者我是否误解?

我的理解是,如果它不接受任何论点,那么这不仅是一个定义,也是初步的。

class whatever
{
private:
    box newBox; //is this not allowed because of the most vexing parse? because it s a
                //header file and because of the MVP this is initialisation as well? or
                //am I getting confused about something?
};

class box
{
public:
    box();
}
最佳回答

以下著作只是罚款:

struct Foo {
}

struct Bar {
    Foo foo;
}

无地雷影响调查,没有模糊之处。 一切都属于罚款。 在一类申报中<代码> Foo foo; is a member-declaration. 不可能在该地进行初始化,在构造中(此处为<条码>Bar)进行初步化(明示或默示)。

问题回答

If the only constructor accepts no arguments, this is the canonical constructor that s called every time you construct an instance of the class which has newBox as an element. It s still always a new box object, though it ll always look the same.


You can test it this way:
class foo {
 public:
  foo() {
    std::cout << "Constructed a foo.
";
  }
};

class bar{
  foo afoo;
 public:
  bar() {}
};

int main() {
  std::cout << "Request new bar: ";
  bar bar1;
  std::cout << "Request another new bar: ";
  bar bar2;
  return 0;
}

产出:

Request new bar: Constructed a foo.
Request another new bar: Constructed a foo.

页: 1 除需要将<代码><> 方框>/代码”的定义移至<代码>whatever的类别外,还可在该栏中采用<代码>。 当你创建<代码>whatever时,newBox将与<代码>方框级的违约构造者一起启动。

如果你想明确初始化或以某种方式定制,就必须在<代码>上添加一个构件。 班级。 例如:

class whatever {
private:
    box newBox;
public:
    whatever() : newBox() {
       // ...
    }
};

这是最令人 v慕的教区的一个例子:

box newBox();

这并不是:

box newBox;




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

热门标签