<legend id='92GP8'><style id='92GP8'><dir id='92GP8'><q id='92GP8'></q></dir></style></legend>
    1. <i id='92GP8'><tr id='92GP8'><dt id='92GP8'><q id='92GP8'><span id='92GP8'><b id='92GP8'><form id='92GP8'><ins id='92GP8'></ins><ul id='92GP8'></ul><sub id='92GP8'></sub></form><legend id='92GP8'></legend><bdo id='92GP8'><pre id='92GP8'><center id='92GP8'></center></pre></bdo></b><th id='92GP8'></th></span></q></dt></tr></i><div id='92GP8'><tfoot id='92GP8'></tfoot><dl id='92GP8'><fieldset id='92GP8'></fieldset></dl></div>
    2. <small id='92GP8'></small><noframes id='92GP8'>

        <tfoot id='92GP8'></tfoot>

        • <bdo id='92GP8'></bdo><ul id='92GP8'></ul>
      1. 如何在构造函数中初始化 C++ 对象成员变量?

        时间:2023-09-27
        <i id='MTP1O'><tr id='MTP1O'><dt id='MTP1O'><q id='MTP1O'><span id='MTP1O'><b id='MTP1O'><form id='MTP1O'><ins id='MTP1O'></ins><ul id='MTP1O'></ul><sub id='MTP1O'></sub></form><legend id='MTP1O'></legend><bdo id='MTP1O'><pre id='MTP1O'><center id='MTP1O'></center></pre></bdo></b><th id='MTP1O'></th></span></q></dt></tr></i><div id='MTP1O'><tfoot id='MTP1O'></tfoot><dl id='MTP1O'><fieldset id='MTP1O'></fieldset></dl></div>

        <small id='MTP1O'></small><noframes id='MTP1O'>

        <tfoot id='MTP1O'></tfoot>

        <legend id='MTP1O'><style id='MTP1O'><dir id='MTP1O'><q id='MTP1O'></q></dir></style></legend>

                  <tbody id='MTP1O'></tbody>
                • <bdo id='MTP1O'></bdo><ul id='MTP1O'></ul>
                • 本文介绍了如何在构造函数中初始化 C++ 对象成员变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个类,它有几个对象作为成员变量.我不希望在声明时调用这些成员的构造函数,所以我试图明确地挂在指向该对象的指针上.我不知道我在做什么.

                  I've got a class that has a couple of objects as member variables. I don't want the constructors for these members to be called when declared, so I'm trying to hang onto a pointer to the object explicitly. I have no idea what I'm doing.

                  我想也许我可以执行以下操作,在初始化对象成员变量时立即调用构造函数:

                  I thought maybe I could do the following, where the constructor is called immediately when initializing the object member variable:

                  class MyClass {
                      public:
                          MyClass(int n);
                      private:
                          AnotherClass another(100); // Construct AnotherClass right away!
                  };
                  

                  但我希望 MyClass 构造函数调用 AnotherClass 构造函数.这是我的代码的样子:

                  But I want the MyClass constructor to call the AnotherClass constructor. Here's what my code looks like:

                  #include "ThingOne.h"
                  #include "ThingTwo.h"
                  
                  class BigMommaClass {
                  
                          public:
                                  BigMommaClass(int numba1, int numba2);
                  
                          private:
                                  ThingOne* ThingOne;
                                  ThingTwo* ThingTwo;
                  };
                  

                  文件 BigMommaClass.cpp

                  #include "BigMommaClass.h"
                  
                  BigMommaClass::BigMommaClass(int numba1, int numba2) {
                          this->ThingOne = ThingOne(100);
                          this->ThingTwo = ThingTwo(numba1, numba2);
                  }
                  

                  这是我尝试编译时遇到的错误:

                  Here's the error I'm getting when I try to compile:

                  g++ -Wall -c -Iclasses -o objects/BigMommaClass.o classes/BigMommaClass.cpp
                  In file included from classes/BigMommaClass.cpp:1:0:
                  classes/BigMommaClass.h:12:8: error: declaration of ThingTwo* BigMommaClass::ThingTwo
                  classes/ThingTwo.h:1:11: error: changes meaning of ThingTwo from class ThingTwo
                  classes/BigMommaClass.cpp: In constructor BigMommaClass::BigMommaClass(int, int):
                  classes/BigMommaClass.cpp:4:30: error: cannot convert ThingOne to ThingOne* in assignment
                  classes/BigMommaClass.cpp:5:37: error: ((BigMommaClass*)this)->BigMommaClass::ThingTwo cannot be used as a function
                  make: *** [BigMommaClass.o] Error 1
                  

                  我是否使用了正确的方法,但使用了错误的语法?或者我应该从不同的方向来解决这个问题?

                  Am I using the right approach, but the wrong syntax? Or should I be coming at this from a different direction?

                  推荐答案

                  可以在成员初始化列表中指定如何初始化成员:

                  You can specify how to initialize members in the member initializer list:

                  BigMommaClass {
                      BigMommaClass(int, int);
                  
                  private:
                      ThingOne thingOne;
                      ThingTwo thingTwo;
                  };
                  
                  BigMommaClass::BigMommaClass(int numba1, int numba2)
                      : thingOne(numba1 + numba2), thingTwo(numba1, numba2) {}
                  

                  这篇关于如何在构造函数中初始化 C++ 对象成员变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:什么是 C++ 中的非平凡构造函数? 下一篇:什么是 C++ 中的复制构造函数?

                  相关文章

                  <i id='88BfD'><tr id='88BfD'><dt id='88BfD'><q id='88BfD'><span id='88BfD'><b id='88BfD'><form id='88BfD'><ins id='88BfD'></ins><ul id='88BfD'></ul><sub id='88BfD'></sub></form><legend id='88BfD'></legend><bdo id='88BfD'><pre id='88BfD'><center id='88BfD'></center></pre></bdo></b><th id='88BfD'></th></span></q></dt></tr></i><div id='88BfD'><tfoot id='88BfD'></tfoot><dl id='88BfD'><fieldset id='88BfD'></fieldset></dl></div>
                • <small id='88BfD'></small><noframes id='88BfD'>

                    • <bdo id='88BfD'></bdo><ul id='88BfD'></ul>
                  1. <tfoot id='88BfD'></tfoot>

                    1. <legend id='88BfD'><style id='88BfD'><dir id='88BfD'><q id='88BfD'></q></dir></style></legend>