简体   繁体   English

我想在组合/整体 class 的成员初始化程序列表中初始化一个“部分”数组 class

[英]I want to initialize an array of "part" class in member initializer list of composed/whole class

Please help me on this.请帮助我。 I want to initialize an array of "part" class in member initializer list of composed/whole class.我想在组合/整个 class 的成员初始化程序列表中初始化“部分”class 的数组。 Here Class B is composed from class A. Now in class B constructor how I will initialze the array in member initialzer list.这里 Class B 由 class A 组成。现在在 class B 构造函数中,我将如何初始化成员初始化器列表中的数组。 I know how to initalize single object of "part" class in constructor of whole class but how an array of part class can be initialized in constructor of whole class. I know how to initalize single object of "part" class in constructor of whole class but how an array of part class can be initialized in constructor of whole class. Also if i dont initialize array of part class in member initializer list will the code work?另外,如果我不在成员初始化程序列表中初始化部分 class 的数组,代码会起作用吗? Thanks in advance.提前致谢。

#include <iostream>
using namespace std ;

class A  //part class
{
  public:
    A( int value = 0)
    { a = value ; }
    
    void printA()
    {
        cout << "\nPrinting A members : " << a << endl ;
    }
 
    void setA( int value)
    { a = value ; }

    protected:
      int a ;

 };


 class B  //whole class
 {
   public:
     B( int value = 5 ) : aM(0) //member initializer list
     { b = value ; }
  
     void printB()
     {
       cout << "\nPrinting B members : " << b << endl ;
       aM.printA() ;
       for (int i = 0 ; i < 5 ; i++)
         cout << arr[i] << " , " ;
     }

   private:
     int b ;
     A aM ; //composition
     A arr[5] ;

};


int main()
{
  B objB ;
  objB.printB() ;


  return 0 ;
}

You initialize the array of a member almost the same as you initialize an array otherwise, using a curly-brace enclosed list of elements.使用花括号括起来的元素列表,您初始化成员的数组几乎与初始化数组一样。 Or nothing if you want all elements default constructed:如果您希望所有元素都默认构造,或者什么都没有:

B( int value = 5 ) : b{ value }, aM{ }, arr{ }
{ }

On another note,另一个注意事项,

cout << arr[i] << " , " ;

is invalid since you don't have an overloaded << operator for A .无效,因为您没有A的重载<<运算符。 Perhaps you want this instead:也许你想要这个:

arr[i].printA();

If I have understood correctly what you need is the following如果我理解正确,您需要的是以下内容

B( int value = 5 ) : b( value ), aM(0), arr{}
{
}

However it is enough to write不过写就够了

B( int value = 5 ) : b( value )
{
}

because the default constructor of the class A will initialize data members aM and arr with zeroes.因为 class A 的默认构造函数将用零初始化数据成员 aM 和 arr。

If you want to use initializers of the array other than zero then you can write for example如果你想使用数组的初始化器而不是零,那么你可以编写例如

B( int value = 5 ) : b( value ), aM(0), arr{ 1, 2, 3, 4, 5}
{
}

And the member function和成员function

 void printB()
 {
   cout << "\nPrinting B members : " << b << endl ;
   aM.printA() ;
   for (int i = 0 ; i < 5 ; i++)
     cout << arr[i] << " , " ;
 }

is incorrect because there is no defined operator << for objects of the type A.不正确,因为没有为 A 类型的对象定义运算符 <<。

To make this statement valid使此声明有效

     cout << arr[i] << " , " ;

define the operator << as a friend function of the class A. For example将运算符 << 定义为 class A 的朋友 function。例如

class A  //part class
{
  public:
    A( int value = 0)
    { a = value ; }
    
    void printA()
    {
        cout << "\nPrinting A members : " << a << endl ;
    }
 
    void setA( int value)
    { a = value ; }

    friend std::ostream & operator <<( std::ostream &os, const A &a )
    {
        return os << a.a;
    }
    protected:
      int a ;

 };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在类成员初始化器列表中初始化向量的元组 - Initialize a tuple of vectors in a class member initializer list 在类的成员初始化器列表中初始化std :: tuple - Initialize std::tuple in member initializer list of a class 如何在成员初始值设定项列表之外初始化类成员 - How to initialize class member outside the member initializer list 如何在C ++类的初始化列表中初始化member-struct? - How to initialize member-struct in initializer list of C++ class? 如何在构造函数初始值设定项列表中初始化std :: unique_ptr对象的类成员std :: vector? - How do I initialize a class member `std::vector` of `std::unique_ptr` object in the constructor initializer list? 如何在初始化列表中初始化普通数组成员变量? - How can I initialize normal array member variable in initializer list? 如何使用 initializer_list 初始化成员数组? - How do I initialize a member array with an initializer_list? 类成员初始化程序使用错误检查初始化ifstream? - Class member initializer to initialize ifstream with error check? 如何让我的数组 class 实例使用初始化列表来初始化数组 - How can I get my array class instance to use initializer list to initialize the array 如何在成员初始值设定项列表中初始化数组 - How to initialize an array in the member initializer list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM