Question about copy constructors / distructors

Discussion in 'C++' started by ennio, Aug 16, 2006.

  1. ennio

    ennio Guest

    Hi, i have a doubt i can't solve right now.
    Your help will be appreciated.

    I am studying constructors/copy constructors/destructors.
    I created this a little class (see below).
    If i comment the destructor (~Mine), i get the following lines as i
    execute main:
    "Constructor
    Copy Constructor"
    , as i expected (in the main function i initialize a variable).
    But, if i uncomment the destructor, i can read
    "Constructor"
    only.

    Can you tell me why or give me an hint?
    Thank you very much

    p.s. I am using VC6

    ----------------CUT-------------------
    #include <iostream>
    #include <conio.h>

    using namespace std;

    class Mine {
    public:
    char *s;
    Mine(char *s) {
    cout << "Constructor" << endl;
    this->s = new char[strlen(s) + 1];
    strcpy(this->s, s);
    }
    Mine(const Mine& object) {
    cout << "Copy Constructor" << endl;
    this->s = new char[strlen(object.s) + 1];
    strcpy(this->s, object.s);
    }
    ~Mine() {
    cout << "Destructor" << endl;
    delete [] s;
    }
    };

    void main() {
    Mine mine = Mine("Try1");
    while(!kbhit());
    }
    ----------------CUT-------------------
     
    ennio, Aug 16, 2006
    #1
    1. Advertising

  2. ennio

    Rolf Magnus Guest

    ennio wrote:

    > Hi, i have a doubt i can't solve right now.
    > Your help will be appreciated.
    >
    > I am studying constructors/copy constructors/destructors.
    > I created this a little class (see below).
    > If i comment the destructor (~Mine), i get the following lines as i
    > execute main:
    > "Constructor
    > Copy Constructor"
    > , as i expected (in the main function i initialize a variable).
    > But, if i uncomment the destructor, i can read
    > "Constructor"
    > only.
    >
    > Can you tell me why or give me an hint?


    Well, your main function basically first creates a nameless temporary Mine
    object, then copy-construcs mine from it. However, the compiler is allowed
    to optimize the temporary away, so that mine is directly initialized with
    your string. It sounds strange that this would depend on whether you have a
    user-defined destructor or not, but it would be permitted.

    > Thank you very much
    >
    > p.s. I am using VC6


    It would be good to upgrade that. It's very old and not really
    standard-compliant.

    >
    > ----------------CUT-------------------
    > #include <iostream>
    > #include <conio.h>
    >
    > using namespace std;
    >
    > class Mine {
    > public:
    > char *s;
    > Mine(char *s) {


    You should prefer the std::string class for strings. If you can't for some
    reason, make that:

    Mine(const char* s) {

    > cout << "Constructor" << endl;
    > this->s = new char[strlen(s) + 1];
    > strcpy(this->s, s);
    > }
    > Mine(const Mine& object) {
    > cout << "Copy Constructor" << endl;
    > this->s = new char[strlen(object.s) + 1];
    > strcpy(this->s, object.s);
    > }


    Don't forget to add an operator=.

    > ~Mine() {
    > cout << "Destructor" << endl;
    > delete [] s;
    > }
    > };
    >
    > void main() {


    main() must always return int.

    > Mine mine = Mine("Try1");
    > while(!kbhit());
    > }
     
    Rolf Magnus, Aug 16, 2006
    #2
    1. Advertising

  3. ennio wrote:
    > [..]
    > If i comment the destructor (~Mine), i get the following lines as i
    > execute main:
    > "Constructor
    > Copy Constructor"
    > , as i expected (in the main function i initialize a variable).
    > But, if i uncomment the destructor, i can read
    > "Constructor"
    > only.
    >
    > Can you tell me why or give me an hint?


    Apparently, the compiler produces different code based on the presence
    of the destructor. It is allowed to.

    > Thank you very much
    >
    > p.s. I am using VC6


    That's very unfortunate. I strongly recommend you to upgrade to 2003
    or 2005.

    >
    > ----------------CUT-------------------
    > #include <iostream>
    > #include <conio.h>


    This is a non-standard header. It's customary to provide its contents
    or not use it at all when posting here.

    >
    > using namespace std;
    >
    > class Mine {
    > public:
    > char *s;
    > Mine(char *s) {
    > cout << "Constructor" << endl;
    > this->s = new char[strlen(s) + 1];
    > strcpy(this->s, s);
    > }
    > Mine(const Mine& object) {
    > cout << "Copy Constructor" << endl;
    > this->s = new char[strlen(object.s) + 1];
    > strcpy(this->s, object.s);
    > }
    > ~Mine() {
    > cout << "Destructor" << endl;
    > delete [] s;
    > }
    > };
    >
    > void main() {


    C++ does not have "void main". 'main' returns 'int'.

    > Mine mine = Mine("Try1");


    The line above is called "copy-initialisation". The compiler is allowed
    not to create the temporary, but instead initialise the object directly.
    It is allowed, of course, not to optimise.

    > while(!kbhit());


    There is no function 'kbhit' in C++. Try to avoid posting non-standard
    code when asking questions in comp.lang.c++.

    > }
    > ----------------CUT-------------------


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 16, 2006
    #3
  4. ennio

    mlimber Guest

    ennio wrote:
    > Hi, i have a doubt i can't solve right now.
    > Your help will be appreciated.
    >
    > I am studying constructors/copy constructors/destructors.
    > I created this a little class (see below).
    > If i comment the destructor (~Mine), i get the following lines as i
    > execute main:
    > "Constructor
    > Copy Constructor"
    > , as i expected (in the main function i initialize a variable).
    > But, if i uncomment the destructor, i can read
    > "Constructor"
    > only.
    >
    > Can you tell me why or give me an hint?
    > Thank you very much
    >
    > p.s. I am using VC6
    >
    > ----------------CUT-------------------
    > #include <iostream>
    > #include <conio.h>


    Non-standard header.

    >
    > using namespace std;
    >
    > class Mine {
    > public:
    > char *s;


    Prefer std::string to managing memory on your own. See
    http://www.parashift.com/c -faq-lite/containers.html#faq-34.1.

    > Mine(char *s) {
    > cout << "Constructor" << endl;
    > this->s = new char[strlen(s) + 1];
    > strcpy(this->s, s);
    > }
    > Mine(const Mine& object) {
    > cout << "Copy Constructor" << endl;
    > this->s = new char[strlen(object.s) + 1];
    > strcpy(this->s, object.s);
    > }


    It's not necessary to qualify members with "this->".

    > ~Mine() {
    > cout << "Destructor" << endl;
    > delete [] s;
    > }
    > };
    >
    > void main() {


    int main() { // http://parashift.com/c -faq-lite/newbie.html#faq-29.3

    > Mine mine = Mine("Try1");
    > while(!kbhit());
    > }
    > ----------------CUT-------------------


    Are you sure? When I run it on VC6 (sp6) or g++ 3.4.4, I get:

    Constructor
    Destructor

    Cheers! --M
     
    mlimber, Aug 16, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeremy Smith
    Replies:
    2
    Views:
    603
    Jeremy Smith
    Aug 3, 2006
  2. Jess
    Replies:
    5
    Views:
    615
    Ron Natalie
    Jun 7, 2007
  3. Shraddha

    pure virtual constructors and distructors...

    Shraddha, Jun 25, 2007, in forum: C Programming
    Replies:
    4
    Views:
    386
    Johan Bengtsson
    Jul 6, 2007
  4. Shraddha
    Replies:
    8
    Views:
    558
    Diego Martins
    Jun 29, 2007
  5. srp113
    Replies:
    3
    Views:
    482
Loading...

Share This Page