I don't have the BDS 2007 drive mounted right now, so this answer was
confirmed with BCB 6.
When you declare an instance of class A
A source;
then it will not call the copy constructor for elements in source.X. It
will call the constructor with no or defaulted argument or if none is
supplied, the default constructor for each element in source.X.
If you then do an assignment such as:
A source;
A target;
:
target = source;
The constructor for each element will have been called for each element in
target.X at the point of declaration for target.
When the assignment is executed an assignment operator is called for each
element of target as in {pseudocode}
target.X[n].operator = (source.X[n]);
Run this to see what I mean:
-----------------------------------------------------------
#include <cstdio>
// helper function to handle unprintable chars
const char *CharOut(char c);
class ArrayClass
{
public:
ArrayClass(char cx = tracking_symbol) : c(cx)
{
ShowInit("Constructor", c);
}
ArrayClass(const ArrayClass & x) : c(x.c)
{
ShowInit("Copy Constructor", c);
}
ArrayClass& operator = (const ArrayClass & x)
{
c = x.c;
ShowInit("Assignment Operator", c);
return *this;
}
private:
char c;
static char tracking_symbol;
void ShowInit(const char *why, char x)
{
std::printf(
"%s \'%c\' \'%s\'\n",
why, tracking_symbol, CharOut(x)
);
++tracking_symbol;
}
};
char ArrayClass::tracking_symbol = 'A';
struct ContainerStruct
{
int num_items;
ArrayClass array[5];
};
ArrayClass item1(1);
ArrayClass item2(2);
ArrayClass item3(3);
ArrayClass item4(4);
ArrayClass item5(5);
int main()
{
ContainerStruct source;
std::printf("\nInitializing\n");
source.num_items = 5;
source.array[0] = item1;
source.array[1] = item2;
source.array[2] = item3;
source.array[3] = item4;
source.array[4] = item5;
ContainerStruct target;
std::printf("\nCopying\n");
target = source;
return 0;
}
const char *CharOut(char c)
{
static char result_str[8];
const char *format;
format = ((c >= '!') && (c <= '~')) ? "%c" : "\\x%02X";
std::sprintf(result_str, format, c);
return result_str;
}
-----------------------------------------------------------
. Ed
Post by Vladimirclass A {
float X[5];
// other fields
}
Is it definitely true, that the default copy constructor will copy X
element by element, as if there were
for (int i=0; i<5;i++) { X[i] = other.X[i] } ?