[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Strange behaviour

Ioannis Vranos

10/2/2008 5:01:00 PM

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;

srand(0);

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

cout<<vec[i]<<"\t";
}

cout<<"\n\n";
}



int main()
{
using namespace std;

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...
1804289383 846930886 1681692777 1714636915
1957747793 424238335 719885386 1649760492
596516649 1189641421

john@john-desktop:~/Projects/Other/anjuta1/src$


(10 values) instead of 100 values. Is it a compiler defect, or am I
missing something?
1 Answer

Jeff Schwab

10/2/2008 5:12:00 PM

0

Ioannis Vranos wrote:
> 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;
>
> srand(0);
>
> for(TypeVector::size_type i= 0; i< vec.size(); ++i)
> {
> vec[i]= rand();
>
> cout<<vec[i]<<"\t";
> }
>
> cout<<"\n\n";
> }
>
>
>
> int main()
> {
> using namespace std;
>
> 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...
> 1804289383 846930886 1681692777 1714636915
> 1957747793 424238335 719885386 1649760492
> 596516649 1189641421
>
> john@john-desktop:~/Projects/Other/anjuta1/src$
>
>
> (10 values) instead of 100 values. Is it a compiler defect, or am I
> missing something?

Only one instance of SomeClass is default-constructed. Most of the
vector elements are copy-constructed.