[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.c++

Strange behaviour (corrected

Ioannis Vranos

10/2/2008 5:08: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;


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?
17 Answers

Victor Bazarov

10/2/2008 5:32:00 PM

0

Ioannis Vranos wrote:
> [...]
> Vector vec(SIZE);
>

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

Ioannis Vranos

10/2/2008 5:54:00 PM

0

Victor Bazarov wrote:
> Ioannis Vranos wrote:
>> [...]
>> Vector vec(SIZE);
>>
>
> Your class is missing the copy constructor.


Shouldn't I get a diagnostic?

Ioannis Vranos

10/2/2008 5:56:00 PM

0

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";
}


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

Victor Bazarov

10/2/2008 5:58:00 PM

0

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?

What diagnostic? For your attentiveness (or lack thereof)? <g>

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Jeff Schwab

10/2/2008 6:14:00 PM

0

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

Obnoxious User

10/2/2008 6:21:00 PM

0

On Thu, 02 Oct 2008 20:56:02 +0300, Ioannis Vranos wrote:

> 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_...

Ioannis Vranos

10/2/2008 6:22:00 PM

0

Jeff Schwab wrote:
> 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. 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?

Jeff Schwab

10/2/2008 6:26:00 PM

0

Ioannis Vranos wrote:
> Jeff Schwab wrote:
>> 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. 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?

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.

Victor Bazarov

10/2/2008 7:11:00 PM

0

Obnoxious User wrote:
> On Thu, 02 Oct 2008 20:56:02 +0300, Ioannis Vranos wrote:
>> (10 values instead of 100).
>
> Why do you expect 100 values to be printed, when
> you only print the ten in the constructor?

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.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

James Kanze

10/2/2008 7:40:00 PM

0

On Oct 2, 8:14 pm, Jeff Schwab <j...@schwabcenter.com> wrote:
> 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.

--
James Kanze (GABI Software) email:james.kanze@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