c++采用new建立对象数组

推荐用vector。

参考此文:https://stackoverflow.com/questions/255612/dynamically-allocating-an-array-of-objects

object *p = new object[3];

p[0] = 

p[1] = 

上述情况是否需要定义构造函数?答案是不需要(有默认构造函数就足够了)。如果对象有用户自定义的构造函数,new可以同时对对象初始化。但是,不能用在对象数组上。
An optional initializer field is included in the grammar for the new operator. This allows new objects to be initialized with user-defined constructors.No explicit per-element initialization can be done when allocating arrays using the new operator; only the default constructor, if present, is called.
https://docs.microsoft.com/en-us/cpp/cpp/new-operator-cpp?view=vs-2017

// expre_Initializing_Objects_Allocated_with_new.cpp
class Acct
{
public:
    // Define default constructor and a constructor that accepts
    //  an initial balance.
    Acct() { balance = 0.0; }
    Acct( double init_balance ) { balance = init_balance; }
private:
    double balance;
};

int main()
{
    Acct *CheckingAcct = new Acct;
    Acct *SavingsAcct = new Acct ( 34.98 );
    double *HowMuch = new double ( 43.0 );
    // ...
}

new object不对对象进行初始化。

new object(parameters)显示调用构造函数初始化对象。

如果没有定义默认构造函数,也可以用new动态定义对象数组,参考https://www.cnblogs.com/SimonKly/p/7819147.html。

如果类

class A

{

        B * pointer_toB;

pointer_toB,默认构造函数将其初始化成啥?

你可能感兴趣的:(编译)