why C++ cannot let you initialize a static array of objects that have at least one reference value ?

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
feli_lecchini
Posts: 15
Joined: November 7th, 2022, 12:58 am

why C++ cannot let you initialize a static array of objects that have at least one reference value ?

Post by feli_lecchini » January 5th, 2023, 5:42 pm

Say I have a class A defined as:

#include "Graphics.h"

class A
{
public:
A(Graphics& gfx)
:
gfx(gfx)
{
}

A() = delete;
A(const A&) = delete;
A& operator = (const A&) = delete

private:
Graphics& gfx;
}

...and then say I want to create a static array of objects of class A in another class B:

class B
{
static constexpr int N = 10;
A myArray[N];
}

the compiler complains about not able to initialize the elements of the array. And no matter how I try to implement different kinds of constructors for class B there's no way the compiler will pass the parameter "gfx" to every instance of the class A in the array. Any solution to this ?

John73 John
Posts: 14
Joined: August 6th, 2020, 7:16 pm

Re: why C++ cannot let you initialize a static array of objects that have at least one reference value ?

Post by John73 John » January 5th, 2023, 11:24 pm

As with a lot of things in C++, it's complicated. Basically when you create and array, you create all the items in the array at that point. Creating an array of A's tries to call the default constructor, which is "A()" but that's been deleted. There's no way (that I know of) to create an array of A's and then pass parameters to them later. Since A needs a reference to gfx, you can't create it without passing that in.

How far are you along in Chili's tutorials? I think a vector is your best bet here, if you've reached that point. (I don't mean like "Vector2Float", I mean the C++ structure "std::vector<Type>".) A vector is a lot like an array, but a lot easier to use and more flexible. You can create it empty, and then fill it with things later on.

What you can do is something like:

Code: Select all

#include <vector>

class B:
{
	B(Graphics& gfx);
	
	std::vector<A> myVectOfA = {}; // creates an empty vector that will later hold a bunch of A's
}
and then in the constructor:

Code: Select all

B::B(Graphics& gfx)
{
	// Loop creates 10 A's and adds them to the vector
	for (int i = 0; i < 10; i++)
	{
		myVectOfA.push_back(A(gfx));
	}
}

feli_lecchini
Posts: 15
Joined: November 7th, 2022, 12:58 am

Re: why C++ cannot let you initialize a static array of objects that have at least one reference value ?

Post by feli_lecchini » January 6th, 2023, 12:27 am

I suspected it could not be done this way. I actually finished the beginner series. I knew about std::vector, is just that this question arose while trying some coding. So, thanks again for the reply !.

Ohh, I need some of your wisdom here, i have a code of block and i want to replace all appearances of the expressions "position.x" and "position.y" with "X_POS" and "Y_POS". so I used VS regex (Ctrl+H) and wrote "position.([xy])" and replace with "\u$1_POS". But it doesn't seem to work, the "turn to uppercase" \u does not responds. It replaces position.x with \ux_POS and position.y with \uy_POS. What am I doind wrong ?

John73 John
Posts: 14
Joined: August 6th, 2020, 7:16 pm

Re: why C++ cannot let you initialize a static array of objects that have at least one reference value ?

Post by John73 John » January 6th, 2023, 3:15 am

I'm afraid I can't help with that second question. Since there are only 2 things you're replacing why not just do them one at a time? Use ctrl+H to replace "position.x" with "X_POS" and then do the same for Y?

feli_lecchini
Posts: 15
Joined: November 7th, 2022, 12:58 am

Re: why C++ cannot let you initialize a static array of objects that have at least one reference value ?

Post by feli_lecchini » January 6th, 2023, 3:22 am

I did that, but I was wondering if there was a more clever way of doing it. :)

John73 John
Posts: 14
Joined: August 6th, 2020, 7:16 pm

Re: why C++ cannot let you initialize a static array of objects that have at least one reference value ?

Post by John73 John » January 6th, 2023, 3:38 am

Ah yes, the classic programmer trap. "This will take me like 20 seconds, 25 tops. Let me put 4 hours into figuring out if there's a way to cut that down to 10."

feli_lecchini
Posts: 15
Joined: November 7th, 2022, 12:58 am

Re: why C++ cannot let you initialize a static array of objects that have at least one reference value ?

Post by feli_lecchini » January 6th, 2023, 4:37 am

...story of my life right there

Post Reply