Member arrays and copy-construction

B

Ben

Hi all,

I know what I need to do if data_ in the code below were a uchar*, but
what about when it's an uchar array? Do I need to specify my own
copy-constructor and assignment operator?



#include <iostream>

typedef unsigned char uchar;

class ArrayTest {
public:
uchar& operator[](size_t index) {
if (index >= 8) {
throw new std::range_error("Index out of bounds");
} else {
return data_[index];
}
}
private:
uchar data_[8];
};

int main(int argc, char* argv[])
{
ArrayTest a1;
a1[3] = 3;
ArrayTest a2(a1); // Problem?

std::cout << static_cast<const int>(a2[3]) << std::endl;

return 0;
}

It appears to do what I want in VC7, but I don't know whether it's UB or
not.

Cheers!

Ben
 
J

John Ratliff

Ben said:
Hi all,

I know what I need to do if data_ in the code below were a uchar*, but
what about when it's an uchar array? Do I need to specify my own
copy-constructor and assignment operator?



#include <iostream>

typedef unsigned char uchar;

class ArrayTest {
public:
uchar& operator[](size_t index) {
if (index >= 8) {
throw new std::range_error("Index out of bounds");
} else {
return data_[index];
}
}
private:
uchar data_[8];
};

int main(int argc, char* argv[])
{
ArrayTest a1;
a1[3] = 3;
ArrayTest a2(a1); // Problem?

std::cout << static_cast<const int>(a2[3]) << std::endl;

return 0;
}

It appears to do what I want in VC7, but I don't know whether it's UB or
not.

Cheers!

Ben

It does what you want because the first object is still present. The
default copy constructor does a shallow copy, not a deep copy. So it
just copies the pointer to the data in a1 to a2. Once a1 is gone, a2
will stop working.

You will need to define a copy constructor and an equals operator if you
want it to work after a1 is destructed.

--John Ratliff
 
K

Karl Heinz Buchegger

Ben said:
Hi all,

I know what I need to do if data_ in the code below were a uchar*, but
what about when it's an uchar array? Do I need to specify my own
copy-constructor and assignment operator?

No. the code is perfectly fine.
The compiler generated copy constructor and assignment operator handle
arrays just the way you want them.
 
K

Karl Heinz Buchegger

John said:
It does what you want because the first object is still present. The
default copy constructor does a shallow copy, not a deep copy. So it
just copies the pointer to the data in a1 to a2. Once a1 is gone, a2
will stop working.

What are you talking about?
There are no pointers and no dynamic memory allocation in the OP's code.
You will need to define a copy constructor and an equals operator if you
want it to work after a1 is destructed.

The code provided is fine. No user defined copy constructor or
assignment operator (note: an 'equals' operator is not an assignment
operator) is needed.
 
J

John Ratliff

I'm sorry. Please disregard what I said earlier. Karl Buchegger is correct.

--John Ratliff
 
B

Ben

Karl said:
No. the code is perfectly fine.
The compiler generated copy constructor and assignment operator handle
arrays just the way you want them.

Nice, thats what I was hoping to hear.

Cheers!

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top