简体   繁体   English

指向对象和初始化成员值的指针数组

[英]Array of Pointers to class objects and initializing member values

I'm attempting to allocate memory for each entry of the array and initialize the members part1 and part2 to 0. 我正在尝试为数组的每个条目分配内存,并将成员part1和part2初始化为0。

#include <iostream>
using namespace std;

class Two {
public:
    int part1;
    int part2;
};


int main() {

    Two * dp[10]; //array of 10 pointers to objects of class Two

    part1 = 0;
    part2 = 0;

    for (int i = 0; i < 10; i++) { 
        dp[i] = 0;                        

    }
    return 0; 
}

Any help is appreciated. 任何帮助表示赞赏。 I'm new to c++ and I'm trying to understand the basic concepts. 我是c ++的新手,我正在努力理解基本概念。 Thank you in advance! 先感谢您!

Here's a really basic version of that code that uses new to allocate memory: 这是使用new来分配内存的代码的真正基本版本:

#include <iostream>

class Two {
public:
  Two() : part1(0), part2(0) { };
  int part1;
  int part2;
};


int main() {
  const size_t count = 10;
  Two *dp = new Two[count];

  // Do stuff?

  for (size_t i = 0; i < count; ++i) {
    std::cout << dp[i].part1 << "/" << dp[i].part2 << std::endl;
  }

  delete[] dp;

  return 0; 
}

Note that for a multitude of reasons this is a bad idea, but if you need to side-step the Standard Library because teacher then this is where you go. 请注意,由于多种原因,这是一个坏主意,但如果您需要支持标准库, 因为老师那么这就是您要去的地方。

In c++ for your example is preferable to have a default constructor that initialize your data and a std::vector to hold objects of your custom class. 在c ++中,您的示例最好有一个初始化数据的默认构造函数和一个std::vector来保存自定义类的对象。 This will save you a lot of memory handling problems. 这将为您节省大量内存处理问题。

class Two {
public:
  Two() : part1(0), part2(0) { };
  int part1;
  int part2;
};


int main() {
    std::vector<Two> twoVec(10);
    return 0; 
}

You created 10 pointers to instances of Two , but those pointers are not initalized and doesn't point to any instances. 你创造了10个指针的情况下Two ,但这些指针不initalized并没有指向任何实例。

Here is one way to create those instances, store pointers to them in your array, and then set their part1 member to 1 and the part2 member to 2; 下面是创建这些实例的一种方法,在数组中存储指向它们的指针,然后将它们的part1成员设置为1,将part2成员设置为2;

for (int i = 0; i < 10; i++) { 
    dp[i] = new Two();
    dp[i]->part1= 1;              
    dp[i]->part2= 2;              

}

You need to allocate objects of the type class Two pointed to by the elements of the array. 您需要分配由数组元素指向的class Two对象。 Otherwise the program will have undefined behavior. 否则程序将具有未定义的行为。

You can do it either using standard algorithm std::generate like this 您可以使用标准算法std::generate这样做

#include <iterator>
#include <algorithm>

//...

Two * dp[10]; //array of 10 pointers to objects of class Two

std::generate( std::begin( dp ), std::end( dp ), 
               [] { return new Two { 0, 0 };} );    

Or you can use the range based for loop. 或者您可以使用基于范围的循环。 For example 例如

Two * dp[10]; //array of 10 pointers to objects of class Two

for ( Two * &p : dp ) p = new Two { 0, 0 }; 

Here is a demonstrative program 这是一个示范计划

#include <iostream>

using namespace std;

class Two {
public:
    int part1;
    int part2;
};


int main() 
{

    Two * dp[10]; //array of 10 pointers to objects of class Two

    for ( Two * &p : dp ) p = new Two { 0, 0 }; 

    // processing of the array

    for ( Two * &p : dp ) 
    {
        delete p;
        p = nullptr;
    }       

    return 0; 
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM