How do I initialize an array with the address of another array in C++? -
say have class class1
array declared array1
, in class1.h
, have like
class class1{ public: int array1[2]; }
and later, in different class's .cpp
file, have
class1* class = new class1(); int array0[2]; array0 = class->array1;
however, gives me error
invalid array assignment
how can set addresses of arrays equal each other can modify remotely?
you can't refer array array. have either bind reference array of same type, or use array-like type such std::array<int, 2>
.
array reference:
class1 a; int (&array0)[2] = a.array1; // array0 reference a.array1
std::array
reference:
#include <array> class class1{ public: std::array<int, 2> array1; };
then
class1 a; std::array<int, 2>& array0 = a.array1; // array0 reference a.array1
both these approaches ensure full type information of array available via reference. means, example, can use references std::begin
, std::end
or in range-based for-loop.
note class
keyword. should not use naming variables.
Comments
Post a Comment