#include <iostream>
#include <ctime>
#include <vector>
#include <cstddef>
struct SomeClass
{
typedef std::vector<int> TypeVector;
TypeVector vec;
enum { VectorSize= 10 };
public:
SomeClass();
};
SomeClass::SomeClass():vec(VectorSize)
{
using namespace std;
for(TypeVector::size_type i= 0; i< vec.size(); ++i)
{
vec[i]= rand();
cout<<vec[i]<<" ";
}
cout<<"\n\n";
}
int main()
{
using namespace std;
srand(time(0));
const size_t SIZE=10;
typedef vector<SomeClass> Vector;
cout<< "\nCreating vector with "<< SIZE<< " SomeClass objects..."<<
endl;
Vector vec(SIZE);
}
in my system produces:
john@john-desktop:~/Projects/Other/anjuta1/src$ ./foobar_cpp
Creating vector with 10 SomeClass objects...
73703028 1208935909 602693459 1501639085 1773871829 541492682
713359358 1018154590 823363404 280191048
john@john-desktop:~/Projects/Other/anjuta1/src$
(10 values) instead of 100 values. Is it a compiler defect, or am I
missing something?
Your class is missing the copy constructor.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Shouldn't I get a diagnostic?
The code:
#include <iostream>
#include <ctime>
#include <vector>
#include <cstddef>
struct SomeClass
{
typedef std::vector<int> TypeVector;
TypeVector vec;
enum { VectorSize= 10 };
public:
SomeClass();
SomeClass(const SomeClass &);
};
SomeClass::SomeClass():vec(VectorSize)
{
using namespace std;
for(TypeVector::size_type i= 0; i< vec.size(); ++i)
{
vec[i]= rand();
cout<<vec[i]<<" ";
}
cout<<"\n\n";
}
SomeClass::SomeClass(const SomeClass &arg):vec(arg.vec) {}
int main()
{
using namespace std;
srand(time(0));
const size_t SIZE=10;
typedef vector<SomeClass> Vector;
cout<< "\nCreating vector with "<< SIZE<< " SomeClass objects..."<<
endl;
Vector vec(SIZE);
}
still produces:
john@john-desktop:~/Projects/Other/anjuta1/src$ ./foobar_cpp
Creating vector with 10 SomeClass objects...
1843119438 547869594 571665984 1629263681 1309619246 1217447082
488471487 1269191257 74219401 991479353
john@john-desktop:~/Projects/Other/anjuta1/src$
(10 values instead of 100).
What diagnostic? For your attentiveness (or lack thereof)? <g>
There's no diagnostic because your class actually does have a trivial
copy constructor, provided for you by the compiler. Implicitly defined
constructors are convenient, but do occasionally (as in this case) cause
some confusion.
> Victor Bazarov wrote:
>> Ioannis Vranos wrote:
>>> [...]
>>> Vector vec(SIZE);
>>>
>>>
>> Your class is missing the copy constructor.
>
>
>
> The code:
>
>
> #include <iostream>
> #include <ctime>
> #include <vector>
> #include <cstddef>
>
>
> struct SomeClass
> {
> typedef std::vector<int> TypeVector;
> TypeVector vec;
> enum { VectorSize= 10 };
> public:
> SomeClass();
> SomeClass(const SomeClass &);
> };
>
> SomeClass::SomeClass():vec(VectorSize) {
> using namespace std;
> for(TypeVector::size_type i= 0; i< vec.size(); ++i) {
> vec[i]= rand();
> cout<<vec[i]<<" ";
> }
> cout<<"\n\n";
> }
You print the values in the constructor, but...
>
>
> SomeClass::SomeClass(const SomeClass &arg):vec(arg.vec) {}
>
...you don't do it in the copy constructor.
[snip]
>
>
> still produces:
>
> john@john-desktop:~/Projects/Other/anjuta1/src$ ./foobar_cpp
>
> Creating vector with 10 SomeClass objects... 1843119438 547869594
> 571665984 1629263681 1309619246 1217447082 488471487 1269191257
> 74219401 991479353
>
> john@john-desktop:~/Projects/Other/anjuta1/src$
>
>
> (10 values instead of 100).
Why do you expect 100 values to be printed, when
you only print the ten in the constructor?
--
OU
Remember 18th of June 2008, Democracy died that afternoon.
http://frapedia.se/wiki/Information_in_English
This default copy constructor shouldn't copy the vector data member
implicitly?
It copied the prototype instance of SomeClass.
1) The prototype was constructed. std::rand() was called ten times, and
the values were inserted into std::cout.
2) The prototype was copied SIZE (or SIZE-1) times. Each copy had the
same values as the prototype, which is pretty much what you'd expect.
No new values or output were generated.
Because Ioannis expected the default constructor to be called 10 times
when a vector of 10 elements is defined:
Vector vec(SIZE); // 'SIZE' is a constant with value 10
Some might think that there would be total of SIZE elements (correct),
*all* default-constructed (incorrect). Only one - a temporary - is
default-constructed, the elements of the vector are actually
copy-constructed from that temporary.
> >> Your class is missing the copy constructor.
> > Shouldn't I get a diagnostic?
> There's no diagnostic because your class actually does have a
> trivial copy constructor, provided for you by the compiler.
The copy constructor is provided by the compiler, but it's not
trivial.
--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
"Simplistic" would be the term I'd use <g>
True! The vector member of SomeClass has a non-trivial
copy-constructor, so the implicit copy-constructor for SomeClass is also
non-trivial. My mistake.
> Vector vec(SIZE);
>
Shouldn't it be:
Vector vec[SIZE];
?
/sana
No, if you use the brackets, you'd construct an array of SIZE vectors,
each of zero size. Ioannis' goal was to construct a *single* vector
with SIZE elements.
Duh! Correct.
/s
This
vector<SomeClass> vec(SIZE);
is roughly equivalent to
SomeClass temp;
SomeClass vec [SIZE] = { temp, temp, temp ... temp };
See how the default constructor is only called once, and the copy
constructor is called SIZE times?
> >>>> Your class is missing the copy constructor.
> >>> Shouldn't I get a diagnostic?
> >> There's no diagnostic because your class actually does have a
> >> trivial copy constructor, provided for you by the compiler.
> > The copy constructor is provided by the compiler, but it's not
> > trivial.
> "Simplistic" would be the term I'd use <g>
The standard uses the term "implicit". I'd go with that as
being what most people would understand. In general, a special
member function is either implicit or user defined; implicit
special member functions can be trivial or not. User defined
functions are never considered "trivial", even if they require
no generated code. (The definition of "trivial" is
intentionally designed so that it corresponds to "no code needs
to be generated".)