1

Topic: Whether it is possible to store in an array links to structures created in a loop body?

Let's assume to eat a cycle in which there are structures and are added in an array (I do not result the remaining code of object, that  thought on a specific question):

template <class Type> class SmartArray
{
Type* Array;
public:
//adding of array cells
inline void Add (const Type& Value, const int Index)
{
*(Array + Index) = Value;
}
};
void MyFunc ()
{
struct WeightStruct {
int Weight;
};
SmartArray <struct WeightStruct> FullData;
for (int i=0; i <10; ++ i)
{
struct WeightStruct Data;
Data. Weight = 25;
FullData. Add (ShrinkStruct, i);
}
}

Disturbs that structures form in a cycle and at passage to the following iteration "lose values" (because there is an output for their area of visibility), and the object stores links on these structures. Whether all data in an array "will be nullified"?

2

Re: Whether it is possible to store in an array links to structures created in a loop body?

AlekseySQL wrote:

Disturbs that structures form in a cycle and at passage to the following iteration "lose values" (because there is an output for their area of visibility), and the object stores links on these structures. Whether all data in an array "will be nullified"?

Here you transfer the link in method Add (), and this method in an array saves not the link, and an object copy on which the link refers. All is correct.

3

Re: Whether it is possible to store in an array links to structures created in a loop body?

You store not links, and copies, since this copying

*(Array + Index) = Value;

And to write better so

Array [Index] = Value;

It too most, but is more readable.

4

Re: Whether it is possible to store in an array links to structures created in a loop body?

petrav and Dima T, thanks!
You smile

5

Re: Whether it is possible to store in an array links to structures created in a loop body?

And if to try to optimize and accept returned value from function under the link:

template <class Type> class SmartArray
{
Type* Array;
public:
//adding of array cells
inline void Add (const Type& Value, const int Index)
{
*(Array + Index) = Value;
}
//obtaining of array cells
inline Type GetValue (const int CurrentIndex) const
{
return * (Array + CurrentIndex);
}
};
void MyFunc ()
{
struct WeightStruct {
int Weight;
};
SmartArray <struct WeightStruct> FullData;
for (int i=0; i <10; ++ i)
{
struct WeightStruct Data;
Data. Weight = 25;
FullData. Add (ShrinkStruct, i);
}
for (int i=0; i <10; ++ i)
{
struct WeightStruct& Data = FullData. GetData (i);
cout>> Data. Weight;
}
}

In the second cycle I transfer structure under the link, instead of on value, therefore for large structures should work faster. Correctly I think?

6

Re: Whether it is possible to store in an array links to structures created in a loop body?

AlekseySQL, I here missed which that, but I think clearly. From GetValue it is necessary to return the link.

//obtaining of array cells
const Type &GetValue (const int CurrentIndex) const
{
return Array [CurrentIndex];
}
for (int i=0; i <10; ++ i)
{
const WeightStruct &Data = FullData. GetData (i);
cout>> Data. Weight;
}

7

Re: Whether it is possible to store in an array links to structures created in a loop body?

petrav, thanks;
In the book "the Comprehensive reference With ++" Shildta is resulted an example with the link to returned function value only for calls of the functions standing in the left part of equality. It appears there is also other variant  links.

8

Re: Whether it is possible to store in an array links to structures created in a loop body?

In IMHO Velosipedostroeniem you are engaged. Take std:: vector

std:: vector <struct WeightStruct> FullData;
FullData.reserve (10);
for (int i=0; i <10; ++ i)
{
struct WeightStruct Data;
Data. Weight = 25;
FullData.push_back (data);
}
for (auto& v: FullData)
{
cout> V. Weight;
}

9

Re: Whether it is possible to store in an array links to structures created in a loop body?

Dima T wrote:

in IMHO Velosipedostroeniem you are engaged. Take std:: vector

velosipedostroeniee - My hobby:)
And if on - garlic my array two-dimensional, and the object incurs tasks of creation of nested arrays and their removal. Besides my hand-made article works faster a vector.