Re: constructor and destructor when initializing sized vector

From: jjleto (jjleto_at_laposte.net)
Date: 11/12/04


Date: Fri, 12 Nov 2004 22:15:15 +0100

Ron Natalie a écrit :
> jjleto wrote:
>
>> When I run this simple program:
>>
>> #include <vector>
>> #include <iostream>
>> using namespace std;
>>
>> class A {
>> public:
>> A() { cout << "[ctor]" << endl; }
>> ~A() { cout << "[dtor]" << endl; }
>
> Add:
> A(const A&) { cout << "[copy ctor]" << endl; }
> A& operator=(const A&) { cout << "[op=]" <<endl; return *this; }
>
>> int main()
>> {
>> vector<A> v(3)
>
>
> The constructor you are using above has a second defaulted argument
> which is the value to fill the vector with. It is defaulted to a default
> constructoed A object.
>
> So you get:
> ctor - default constructed object for second constructor parameter
> copy ctor - v[0] being filled
> copy ctor - v[1] being filled
> copy ctor - v[2] being filled.

Thanks. It is clear now.



Relevant Pages

  • Re: basic_ios::init: multiple call standard conform?
    ... >> standard conform, what code version is standard conform? ... > constructed object are perforce uninitialized, ... constructor for ostream also calls ios::init. ...
    (microsoft.public.vc.language)
  • Re: ctor, operator =
    ... > already constructed object and in general case the implementation of the ... > assignment operator will rely on that fact. ... > depends on your class implementation details. ... copy constructor with operator = is valid?" ...
    (comp.lang.cpp)
  • Re: Terminology? Object structure definition, not a class?
    ... comparison between a constructor and a constructed object. ... Now you're in danger of causing confusion. ...
    (comp.lang.javascript)
  • Re: Problem with linker
    ... but to have actually written a default constructor. ... such as overloading on int and pointer types. ... conversion of 0 to CString requires a user-defined conversion, ... acceleration operator *(distance d, time_squared t2); ...
    (microsoft.public.vc.mfc)
  • FAQ Suggestions
    ... It can only be applied to reference type variables converting to ... The .NET runtime can't guarantee that parameterless constructors will be ... performing by not having to call constructor code. ... number like casting an int to short) are always explicit. ...
    (microsoft.public.dotnet.languages.csharp)