Strange behaviour (corrected)

I

Ioannis Vranos

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::SomeClass():vec(VectorSize)
{
using namespace std;


for(TypeVector::size_type i= 0; i< vec.size(); ++i)
{
vec= rand();

cout<<vec<<" ";
}

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?
 
I

Ioannis Vranos

Victor said:
Ioannis said:
[...]
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= rand();

cout<<vec<<" ";
}

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).
 
I

Ioannis Vranos

Jeff said:
Ioannis said:
Victor said:
Ioannis Vranos wrote:
[...]
Vector vec(SIZE);


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. Implicitly defined
constructors are convenient, but do occasionally (as in this case) cause
some confusion.


This default copy constructor shouldn't copy the vector data member
implicitly?
 
J

James Kanze

James said:
Ioannis Vranos wrote:
Victor Bazarov wrote:
Ioannis Vranos wrote:
[...]
Vector vec(SIZE);
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".)
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top