PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Rule of three example and some questions
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Rule of three example and some questions

Réponse
 
LinkBack Outils de la discussion
Vieux 06/02/2008, 16h21   #1
utab
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Rule of three example and some questions

Dear all,

I made a simple example case to experiment with the rule of three. The
code is below:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Exmpl{
public:
//default constructor
Exmpl(){std::cout << "Default constructor of Exmpl" << std::endl; }
// copy constructor
Exmpl(const Exmpl &);
// assignment operator
Exmpl & operator=(const Exmpl&);
// constructor with parameters
Exmpl(std::string &str,int a, double b);
// destructor
~Exmpl()
{
std::cout << "Destructor for class is used" << std::endl;
}
// getter function for the class variables
void getVals();
private:
std::string *pstring;
int i;
double d;
};

Exmpl::Exmpl(const Exmpl& NN)
{
i=NN.i;
d=NN.d;
pstring=NN.pstring;
std::cout << "Copy constructor is called" << std::endl;
}

Exmpl & Exmpl:perator=(const Exmpl& NN)
{
pstring=NN.pstring;
i=NN.i;
d=NN.d;
std::cout << "Assignment operator is used" << std::endl;
}

Exmpl::Exmpl(std::string &str, int dec, double doub)
{
i=dec;
d=doub;
pstring=&str;
std::cout << "Constructor with parameters is used" << std::endl;
}

void Exmpl::getVals(){

cout << i << '\n'
<< d << '\n'
<< *pstring << '\n';
}

int main(){
string str("try example");
vector<Exmpl> vectorExmpl(5);
Exmpl a(str,0,4.5);
Exmpl b(a);
Exmpl c=b;
b.getVals();
c.getVals();
Exmpl *ptrExmpl=new Exmpl(c);
(*ptrExmpl).getVals();
delete ptrExmpl;
return 0;
}

The output of the compiled code with g++ on debian etch is:

Default constructor of Exmpl **
Copy constructor is called
Copy constructor is called
Copy constructor is called
Copy constructor is called
Copy constructor is called
Destructor for class is used **
Constructor with parameters is used
Copy constructor is called
Copy constructor is called **
0
4.5
try example
0
4.5
try example
Copy constructor is called
0
4.5
try example
Destructor for class is used
Destructor for class is used
Destructor for class is used
Destructor for class is used
Destructor for class is used
Destructor for class is used
Destructor for class is used
Destructor for class is used
Destructor for class is used

My questions are on the two starred lines,

+ If I do not use a default constructor I am getting a compile time
error
+ Why is my assignment operator not working.
+ Why the destructor of the class is run in the middle, is that run
for the vector, if yes, is not vector still in the scope?

Many thanks for the replies and best regards,
  Réponse avec citation
Vieux 06/02/2008, 16h40   #2
Joe Greer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Rule of three example and some questions

utab <umut.tabak@gmail.com> wrote in news:d1b92668-8d77-4cc2-918a-e75fd16f7cc8
@v67g2000hse.googlegroups.com:

>
> My questions are on the two starred lines,
>
> + If I do not use a default constructor I am getting a compile time
> error


std::vector initializes using the default constructor. In this case, it apparently
creates one default initialized object and copies it 5 times to initialize all 5
cells of the vector.

> + Why is my assignment operator not working.


You don't have any assignments in your code. You only have initializers. These
just use the copy constructor. To get the assignment operator you need something
like:

Exmpl c;
c = a;

> + Why the destructor of the class is run in the middle, is that run
> for the vector, if yes, is not vector still in the scope?


That is the destructor for the default constructed object it uses to copy construct
all the cells of the vector.

>
> Many thanks for the replies and best regards,
>


HTH,
joe
  Réponse avec citation
Vieux 06/02/2008, 17h13   #3
utab
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Rule of three example and some questions

On Feb 6, 5:40 pm, Joe Greer <jgr...@doubletake.com> wrote:
> utab <umut.ta...@gmail.com> wrote in news:d1b92668-8d77-4cc2-918a-e75fd16f7cc8
> @v67g2000hse.googlegroups.com:
>
>
>
> > My questions are on the two starred lines,

>
> > + If I do not use a default constructor I am getting a compile time
> > error

>
> std::vector initializes using the default constructor. In this case, it apparently
> creates one default initialized object and copies it 5 times to initialize all 5
> cells of the vector.
>
> > + Why is my assignment operator not working.

>
> You don't have any assignments in your code. You only have initializers. These
> just use the copy constructor. To get the assignment operator you need something
> like:
>
> Exmpl c;
> c = a;
>
> > + Why the destructor of the class is run in the middle, is that run
> > for the vector, if yes, is not vector still in the scope?

>
> That is the destructor for the default constructed object it uses to copy construct
> all the cells of the vector.
>
>
>
> > Many thanks for the replies and best regards,

>
> HTH,
> joe


Thanks for the clarification. The last one was the most probable to
miss :-) at least by me.

Rgds,
  Réponse avec citation
Vieux 06/02/2008, 18h21   #4
Andrey Tarasevich
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Rule of three example and some questions

Joe Greer wrote:
>> My questions are on the two starred lines,
>>
>> + If I do not use a default constructor I am getting a compile time
>> error

>
> std::vector initializes using the default constructor. In this case, it apparently
> creates one default initialized object and copies it 5 times to initialize all 5
> cells of the vector.
> ...


Well, strictly speaking 'std::vector' doesn't use the element's default
constructor anywhere internally. The element type is not required to
have the default constructor at all. The only places when the element's
default constructors might appear is the default arguments for some
member function of 'std::vector'. Yet, once again, 'std::vector' never
relies on these default arguments internally.

This means that it's always up to the user whether to use these default
arguments (and, consequently, the default constructor of the element
type) or not. In OP's case the default argument is used in the
initialization of 'std::vector<Exmpl> vectorExmpl(5)', which is really
just a shorthand for 'std::vector vectorExmpl(5, Exmpl(),
std::allocator<Exmpl>())', taking into account the default arguments.

So, it is not exactly correct to say that "std::vector initializes using
the default constructor". 'std::vector' always initializes its elements
by using the copy constructor. It is really the user who's [implicitly]
causing the default initialization for the filler element in this case.

--
Best regards,
Andrey Tarasevich
  Réponse avec citation
Vieux 07/02/2008, 02h08   #5
Ron Natalie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Rule of three example and some questions

Joe Greer wrote:
> utab <umut.tabak@gmail.com> wrote in news:d1b92668-8d77-4cc2-918a-e75fd16f7cc8
> @v67g2000hse.googlegroups.com:
>
>> My questions are on the two starred lines,
>>
>> + If I do not use a default constructor I am getting a compile time
>> error

>
> std::vector initializes using the default constructor. In this case, it apparently
> creates one default initialized object and copies it 5 times to initialize all 5
> cells of the vector.


It has no choice to do this. Vector's constructor as well as resize
take a single instance of the contained object to "fill" the cells
of the array. It just happens to have a default value of a default
constructed object.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h27.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14790 seconds with 13 queries