copy constructor

This is a discussion on copy constructor within the C++ Programming forums, part of the General Programming Boards category; Hi... just making the move from C to C++. Would appreciate an answer to the following question... I want to ...

  1. #1
    Unregistered
    Guest

    Post copy constructor

    Hi... just making the move from C to C++. Would appreciate an answer to the following question...

    I want to overload an operator so that it will join strings and assign the result to another object. I can get these to work, but only if my assignment looks like this:

    strtype obj5 = ob1 + ob2; // automatically calls copy constructor

    What I really want to do is this:
    strtype ob1, ob2, ob5;
    ...
    ...
    ...
    obj5 = obj1 + obj2; // crashes; prob. does't call copy constructor.

    Here's my code... please take a look. Thanks. I use Dev C++.


    #include <iostream>
    #include <cstdlib>
    #include <cstring>

    using namespace std;

    class strtype {
    private:
    char *s;
    public:
    strtype( const strtype & ); // copy constructor
    strtype() { s = new char; s = '\0'; }
    strtype( char * );
    ~strtype() { cout << "destructing...\n"; }
    friend strtype operator+( strtype &, strtype & );
    void print_string() { cout << "string: " << s << "\n"; }
    };

    strtype::strtype( const strtype &obj ) // copy constructor
    {

    int len;

    len = strlen(obj.s);

    s = new char [len];

    strcpy(s, obj.s);

    }



    strtype::strtype( char *string )
    {

    int len;

    len = strlen(string);
    s = new char [len];

    if( !s ) {
    cout << "Allocation Error\n";
    exit(1);
    }

    strcpy(s, string);

    }

    strtype operator+( strtype &string1, strtype &string2 )
    {

    int len1, len2;
    strtype temp;

    len1 = strlen(string1.s);
    len2 = strlen(string2.s);

    temp.s = new char [len1 + len2];

    if( !temp.s )
    exit(1);

    temp.s = strcat(string1.s, string2.s);

    return temp;



    }

    int main()
    {

    strtype ob1("string1");
    strtype ob2("zebra2");

    strtype ob5 = ob1 + ob2; // invokes copy constructor

    ob5.print_string();


    system("PAUSE");
    return 0;

    }

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    What I really want to do is this:
    strtype ob1, ob2, ob5;
    ...
    ...
    ...
    obj5 = obj1 + obj2; // crashes; prob. does't call copy constructor.
    Then you'll have to overload the assignment operator. The code will be similar to your copy constructor but you'll have delete the memory allocated(if any) in the strtype being assigned to before allocating memory to store the assigned string as with the copy constructor.

    Also before deleting any memory you'll have to check for self-assignment (obj1=obj1; ) otherwise you'll remove the string stored in obj1.
    Last edited by zen; 09-26-2001 at 05:13 PM.
    zen

  3. #3
    Unregistered
    Guest
    OK Zen... thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy constructor
    By dude543 in forum C++ Programming
    Replies: 26
    Last Post: 01-26-2006, 04:35 PM
  2. illegal copy constructor required?
    By ichijoji in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2005, 05:27 PM
  3. Linked list copy constructor issue
    By Craptastic! in forum C++ Programming
    Replies: 1
    Last Post: 08-03-2003, 08:30 PM
  4. copy constructor
    By Eber Kain in forum C++ Programming
    Replies: 1
    Last Post: 09-30-2002, 05:03 PM
  5. Using strings with the copy constructor
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-29-2001, 03:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21