answersLogoWhite

0

C-style arrays can be used as data members, both as fixed-size arrays or as dynamic arrays. The following examples demonstrate how they could be used as one-dimensional data members.

class a {

private:

int b[10] {0}; // in-class initialisation (all elements zero).

public:

int& operator[] (unsigned index) {

if (index>=10) throw std::range_error();

return b[index]; }

};

class c {

public:

c (unsigned sz=10): size(sz), p (size?new int[size]:nullptr) {

memset (p, 0, size); }

c (const s& _s): size(_s.size), p (size?new int[size]:nullptr) {

if (size) memcpy (p, _s.p, size); }

c (s&& _s): size(_s.size), p (_s.p) { _s.p=nullptr; } // swap resources

c& operator= (const s& _s) {

if (p) delete [] p;

size = _s.size;

p = (size?new int[size]:nullptr);

if (size) memcpy (p, _s.p, size); }

c& operator= (s&& _s) {

size = _s.size;

p = _s.p;

_s.p=nullptr; } // swap resources

~c() { if (p) delete [] p; }

int& operator[] (unsigned index) {

if (index>=size) throw std::range_error();

return p[index]; }

private:

unsigned size {0};

int* p {nullptr};

};

A much simpler approach would be to use std::array<int> and std::vector<int> instead of primitive C-style arrays.

User Avatar

Wiki User

10y ago

Still curious? Ask our experts.

Chat with our AI personalities

FranFran
I've made my fair share of mistakes, and if I can help you avoid a few, I'd sure like to try.
Chat with Fran
JudyJudy
Simplicity is my specialty.
Chat with Judy
TaigaTaiga
Every great hero faces trials, and you—yes, YOU—are no exception!
Chat with Taiga

Add your answer:

Earn +20 pts
Q: Array as primitive data members in c plus plus program?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can you store non primitive data type in the array list?

Yes you can store non primitive data type variables in an array. String is a non primitive data type. You can declare a string array as: String a[]=new String[10];


Why arrays are not primitive data type?

An array is a primitive data type. It is the element type that may or may not be primitive.


Should element in an array must be of primitive data type?

No, it can be array, structure or union as well.


Is string arr a non primitive data type?

Depends on the context of the question you were asking from, there are 2 distinct answers: Yes and NO. In the narrowest definition, any array is NOT a primitive data type in C#. Hence a string array is NOT a primitive data type in that context. A string itself, however, is a primitive data type. Some developers would like to extend the definition of "primitives" into the arrays, collections, and Enumeration. Thus, in this context, an array of string IS a primitive data type.


When an array contains values other than primitive data types is it considered a control array?

Only if the non-primitive data types are actually controls, such as an array of label controls, or an array of edit boxes. However, a control array is still an array. The only difference is that the values will likely be resource handles (objects that refer or point to the actual object which will be stored elsewhere in memory) rather than an actual value itself. That is, an array of primitive data types stores the actual value in the array itself.