Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Rule of three e...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 5 Topic 43131 of 48020
Post > Topic >>

Rule of three example and some questions

by utab <umut.tabak@[EMAIL PROTECTED] > Feb 6, 2008 at 08:21 AM

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::operator=(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,
 




 5 Posts in Topic:
Rule of three example and some questions
utab <umut.tabak@[EMAI  2008-02-06 08:21:22 
Re: Rule of three example and some questions
Joe Greer <jgreer@[EMA  2008-02-06 17:40:19 
Re: Rule of three example and some questions
Andrey Tarasevich <and  2008-02-06 10:21:52 
Re: Rule of three example and some questions
Ron Natalie <ron@[EMAI  2008-02-06 21:08:07 
Re: Rule of three example and some questions
utab <umut.tabak@[EMAI  2008-02-06 09:13:33 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 14 9:05:36 CDT 2008.