Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Strange behaviour (corrected)

0 views
Skip to first unread message

Ioannis Vranos

unread,
Oct 2, 2008, 1:08:18 PM10/2/08
to
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?

Victor Bazarov

unread,
Oct 2, 2008, 1:32:11 PM10/2/08
to
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

unread,
Oct 2, 2008, 1:54:01 PM10/2/08
to
Victor Bazarov wrote:
> Ioannis Vranos wrote:
>> [...]
>> Vector vec(SIZE);
>>
>
> Your class is missing the copy constructor.


Shouldn't I get a diagnostic?

Ioannis Vranos

unread,
Oct 2, 2008, 1:56:02 PM10/2/08
to
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

unread,
Oct 2, 2008, 1:58:07 PM10/2/08
to

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

Jeff Schwab

unread,
Oct 2, 2008, 2:14:11 PM10/2/08
to

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

unread,
Oct 2, 2008, 2:20:31 PM10/2/08
to
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_in_English

Ioannis Vranos

unread,
Oct 2, 2008, 2:21:32 PM10/2/08
to


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

Jeff Schwab

unread,
Oct 2, 2008, 2:26:14 PM10/2/08
to

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

unread,
Oct 2, 2008, 3:10:44 PM10/2/08
to

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.

James Kanze

unread,
Oct 2, 2008, 3:39:39 PM10/2/08
to
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...@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

Victor Bazarov

unread,
Oct 2, 2008, 3:41:30 PM10/2/08
to
James Kanze wrote:
> 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.

"Simplistic" would be the term I'd use <g>

Jeff Schwab

unread,
Oct 2, 2008, 3:58:00 PM10/2/08
to
James Kanze wrote:
> 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.

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.

Sana

unread,
Oct 2, 2008, 4:43:02 PM10/2/08
to
On Oct 2, 1:08 pm, Ioannis Vranos <ivra...@no.spam.nospamfreemail.gr>
wrote:
> The code:
>

>      Vector vec(SIZE);
>

Shouldn't it be:

Vector vec[SIZE];

?

/sana

Victor Bazarov

unread,
Oct 2, 2008, 4:51:37 PM10/2/08
to

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.

Sana

unread,
Oct 2, 2008, 4:56:27 PM10/2/08
to

Duh! Correct.

/s

blargg

unread,
Oct 2, 2008, 7:25:06 PM10/2/08
to
In article <gc33fc$2qvt$1...@ulysses.noc.ntua.gr>, Ioannis Vranos
<ivr...@no.spam.nospamfreemail.gr> wrote:

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?

James Kanze

unread,
Oct 3, 2008, 4:14:48 AM10/3/08
to
On Oct 2, 9:41 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> James Kanze wrote:
> > 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.

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

0 new messages