1

I have done some Java programming before where you can pass an array through the constructor of a class to initialize it.

public class Student {
    int age;
    String name;
    int[] classIds;

    public Student(int age, String name, int[] classIds) {
        this.age = age;
        this.name = name;
        this.classIds = classIds;
    }
}

int[] classIds = {1, 2, 3};
Student student = new Student(5, "Claudia", classIds);

But I can't figure out how to pass an array through the constructor in C++

class Student
{

public:
    int age;
    string name;
    int classIds[];
    
    Student()
    {
    }

    Student(int age, string name, int classIds[])
    {
        this->age = age;
        this->name = name;
        this->classIds = classIds;
    }
};

How would I do this? It's giving the lint error incomplete type is not allowed in VSCode

1
  • The array argument decays to a pointer so, unless you also pass its size, you can't copy the data - just the address of its first element. However, you can use the std::array container from the STL - that will be copyable. Or std::vector, if you have variable-sized arrays. Commented Jul 17, 2021 at 21:05

2 Answers 2

1

So I think your question actually has a deeper answer in that, C++ does not have arrays as first class objects. This means that an array is a series of memory addresses of some size and type, that either exists on the stack, on the heap, or in the data segment of an executable. So in your Java code:

int[] classIds = {1, 2, 3};
Student student = new Student(5, "Claudia", classIds);

The classIds array in Java would be managed by the garbage collector. In C++ though, the array would live on the stack. This means as soon as the current scope completes, such as ending an if block or a function returns, that array you just passed in has been destroyed.

In C++, there are two ways to transfer resources like an array. Passing a pointer, or copying. Passing a pointer means that, you must use new and delete[] in order to dynamically allocate and deallocate memory for that area, and the pointer serves as the reference to it. Thus, your code would actually look like this:

class Student
{

public:
    int age;
    string name;
    int* classIds;
    
    Student()
    {
    }

    Student(int age, string name, int* classIds)
    {
        this->age = age;
        this->name = name;
        this->classIds = classIds;
    }
};

When you initialize student, you would instead need to do

int* classIds = new int[3];
classIds[0] = 55;
Student s(3, "hello", classIds);

This way, your class stores a pointer to the classIds, which live in memory on the heap. At some point in your program you will need to call delete[] on the memory you allocated.

Sign up to request clarification or add additional context in comments.

Comments

1

Use std::vector for your arrays.

class Student
{

public:
    int age;
    string name;
    std::vector<int> classIds;

    Student(int age, string name, std::vector<int> classIds)
    {
        this->age = age;
        this->name = name;
        this->classIds = classIds;
    }
};

This code passes the arrays around using copy (i.e. pass by value). This is different from what Java does (pass by reference), but it's simpler and should have mostly the same performance for small arrays.

If you use this idea (pass by value), you should never need to use new.

Student s(55, "person", {1, 22, 8});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.