Home > Enterprise >  How do I create an array of objects?
How do I create an array of objects?

Time:02-14

I only know Java, and I am learning how to do c right now. I currently have an object called "node". I want to make an array of those elements in a different class, and I have to perform many operations on this array. Because of this, I am trying to declare a global array variable that gets initialized in my constructor. In Java, this would've been done by

ObjectName[] variableName = new ObjectName[size];

but I am not sure how to do it in c . I've tried declaring it similar to how I declared the other global arrays, with

Node* nodes;

and then in my constructor:

nodes = new Node[size]

but I got a bunch of compiler errors. How am I supposed to do this? This is only my second week of coding in c , so try to keep answers basic.

CodePudding user response:

In C you use vector more often than array. You also distinguish between creating objects on the stack and on the heap (you already mentioned that concept; in C you are more actively involved in thinking about that).

You also may want to pay attention which C Standard you are using. Some concepts are not available in older standards. I tried to mention some in the example code below.

When dealing with arrays in C/C you should understand the notion of pointers, which I believe is the probable cause of your confusion. new creates an object on the heap and returns a pointer. When creating an array, then the returned pointer points to the first element.

Avoid new if you can. In newer C standards there are better concepts of smart pointers (e.g. std::unique_ptr<...>); I will not dive into that since you are just beginning. Be patient with learning C , I am sure you will succeed, it takes time really.

#include <iostream>
#include <array>
#include <vector>

struct Node {
    std::string name = "node";
};


int main() {

    const size_t size = 10;

    // you can create it on the stack
    // will be deleted when leaving the block/scope
    Node nodes1[size];
    nodes1[0].name = "first node1";
    std::cout << nodes1[0].name << std::endl;

    // you can create it on the heap
    // you have to delete the objects yourself then
    Node *nodes2 = new Node[size];
    nodes2[0].name = "first node2";
    std::cout << nodes2[0].name << std::endl;

    // in C   11 and later you can use std::array<...>
    // you have to include the header <array> for that
    std::array<Node, size> nodes3;
    nodes3[0].name = "first node3";
    std::cout << nodes3[0].name << std::endl;

    // in C   you use array "seldom"
    // instead you use the containers quite a lot as far as I have learned
    // e.g. you can include <vector>; can be used like an array
    std::vector<Node> nodes4(size);
    nodes4[0].name = "first node4";
    std::cout << nodes4[0].name << std::endl;

    // you can iterate over a vector like you know it from an array
    for (size_t i = 0; i < nodes4.size();   i) {
        if (i == 0) {
            std::cout << nodes4[i].name << std::endl;
        }
    }

    // in C   you will soon learn about iterators too
    for (auto iter = nodes4.begin(); iter != nodes4.end(); iter  ) {
        if (iter == nodes4.begin()) {
            std::cout << iter->name << std::endl;
        }
    }

    return 0;
}

CodePudding user response:

How do I create an array of objects?

Given a type named ObjectName, you can define an array variable with name variableName and a compile time constant size size like this:

ObjectName variableName[size]{};
  • Related