By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,218 Members | 1,281 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,218 IT Pros & Developers. It's quick & easy.

Strange behaviour (corrected)

P: n/a
The code:
#include <iostream>
#include <ctime>
#include <vector>
#include <cstddef>
struct SomeClass
{
typedef std::vector<intTypeVector;

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<SomeClassVector;

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?
Oct 2 '08 #1
Share this Question
Share on Google+
17 Replies


P: n/a
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
Oct 2 '08 #2

P: n/a
Victor Bazarov wrote:
Ioannis Vranos wrote:
>[...]
Vector vec(SIZE);

Your class is missing the copy constructor.

Shouldn't I get a diagnostic?
Oct 2 '08 #3

P: n/a
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<intTypeVector;

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<SomeClassVector;

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).
Oct 2 '08 #4

P: n/a
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
Oct 2 '08 #5

P: n/a
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.
Oct 2 '08 #6

P: n/a
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<intTypeVector;
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
Oct 2 '08 #7

P: n/a
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?
Oct 2 '08 #8

P: n/a
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.
Oct 2 '08 #9

P: n/a
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
Oct 2 '08 #10

P: n/a
On Oct 2, 8:14 pm, Jeff Schwab <j...@schwabcenter.comwrote:
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:ja*********@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
Oct 2 '08 #11

P: n/a
James Kanze wrote:
On Oct 2, 8:14 pm, Jeff Schwab <j...@schwabcenter.comwrote:
>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>

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

P: n/a
James Kanze wrote:
On Oct 2, 8:14 pm, Jeff Schwab <j...@schwabcenter.comwrote:
>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.
Oct 2 '08 #13

P: n/a
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
Oct 2 '08 #14

P: n/a
Sana wrote:
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];

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

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

P: n/a
On Oct 2, 4:51*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Sana wrote:
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];
?

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.

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

/s
Oct 2 '08 #16

P: n/a
In article <gc***********@ulysses.noc.ntua.gr>, Ioannis Vranos
<iv*****@no.spam.nospamfreemail.grwrote:
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?
This

vector<SomeClassvec(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?
Oct 2 '08 #17

P: n/a
On Oct 2, 9:41 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
James Kanze wrote:
On Oct 2, 8:14 pm, Jeff Schwab <j...@schwabcenter.comwrote:
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".)

--
James Kanze (GABI Software) email:ja*********@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
Oct 3 '08 #18

This discussion thread is closed

Replies have been disabled for this discussion.