Initialization of an Array of Classes

Initialization of an Array of Classes

A common practice in C is to do something like this to initialize an array of structs:

typedef struct {float x,y,z;} Vector3;Vector3 VecList[] = {{1,0,0},{0,1,0},{0,0,1}};// Now VecList[0] = {1,0,0};//     VecList[1] = {0,1,0}; etc...

Now, I would like to do the same for a class in C++:

class Vector3 {private:float _x,_y,_z;public:// ConstructorsVector3() : _x (0), _y(0), _z(0) {};Vector3( const float X, const float Y,         const float Z ) {        _x = X; _y = Y; _z = Z;};~Vector3();};

But this won’t work…

Vector3 VecList[] = {{1,0,0},{0,1,0},{0,0,1}};

I assume there’s a way… so how can I do this?

This question exemplifies the importance of declaring a default constructor in a class. Please note that a default constructor is one that can be called without any arguments; it isn’t necessarily a constructor that takes no arguments. Thus, even if the class’s constructor takes one or more parameters, it’s advisable to give these parameters default values:

class Coord{public:  Coord(int x = 0, int y = 0);};

The default parameter values enable you to use the class in a context that requires a default constructor, for example, when declaring arrays:

Coord carr[10]; // okCoord *p = new Coord[10]; // ok

However, even if the class doesn’t define a default constructor, as does the following class:

class A{public:   A(int n); //no default constructor};

you can still create arrays, although you need to use an explicit initialization list this time:

A a[3] = { A(0), A(0), A(0)};

Note that in the declaration of the array a, every element must be explicitly initialized. This is tedious, especially if you create a large array. Worse yet, you cannot create dynamic arrays of objects of a class that lacks a default constructor:

A * pa = new A[2]; //error; A has no default ctor
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing