Is it possible for a class to declare an array which can be overridden with an array of a different length in a derived class? I know I could just use std::vector but it is for a performance critical stage of a game engine and since the lengths are known at compile time it seems like it should be possible to do it statically.
I am aiming for something like this, but without the impossible requirement of a member variable which is both virtual and static:
struct F {
virtual static const size_t n;
Signal[n] inputs;
Signal getInput(size_t i)
{
if(i<n)
return inputs[i];
}
};
struct Binary : F {
static const size_t n=2;
};
>Solution :
When you say compile time think, template or constexpr.
Like this :
#include <cassert>
#include <array>
#include <iostream>
//-----------------------------------------------------------------------------
struct Signal
{
std::size_t id = ++s_id;
static std::size_t s_id;
};
std::size_t Signal::s_id{ 0 };
//-----------------------------------------------------------------------------
struct FBase
{
virtual std::size_t size() const = 0;
virtual ~FBase() = default;
protected:
FBase() = default;
};
//-----------------------------------------------------------------------------
template<std::size_t N>
struct F :
public FBase
{
Signal inputs[N]{};
Signal getInput(size_t i)
{
assert(i < N);
return inputs[i];
}
std::size_t size() const override
{
return N;
}
};
//-----------------------------------------------------------------------------
struct Binary :
public F<2ul>
{
};
struct Trinary :
public F<3ul>
{
};
//-----------------------------------------------------------------------------
int main()
{
Binary bin;
Trinary tri;
auto signal = bin.getInput(1ul);
std::cout << signal.id << "\n";
std::cout << tri.size();
return 0;
}