Hi i have written a code for concatenation of two strings by overloading the string operator. The program works fine but i don't understand the sequence of steps happening.

[Use language = C++]
#include <iostream>
using namespace std;

class String
{
    int len;
    char *p;

    public:
        String ()
        {
            cout << "default constructor " << this << endl;
            len = 0;
            p = NULL;
        }
        String (const char *s)
        {
            cout << "String constructor called " << s << endl;
            len = strlen(s);
            p = new char[len + 1];
            strcpy(p, s);
        }
        String (const String &s)
        {
            cout << "Copy constructor invoked " << s.p << endl;;
            len = s.len;
            p = new char[len + 1];
            strcpy(p, s.p);
        }
        ~String ()
        {
             cout << "destructor invoked\n";
            delete p;
        }
        void display(void)
        {
            cout << p << "\t address of p = " << &p << "\tobject this = " <<  this << endl;
        }
        void operator = (const String &s)
        {
            cout << "assignment operator invoked\n";
            len = s.len;
            p = new char[len + 1];
            strcpy(p, s.p);
        }
        friend String operator + (const String &s, const String &t);
};

String operator + (const String &s, const String &t)
{
    String temp;

    temp.len = s.len + t.len;
    temp.p = new char[temp.len + 1];
    strcpy(temp.p, s.p);
    strcat(temp.p, t.p);

    cout << "temp = "; temp.display();
    return temp;
}

main()
{
    String s1("abcdefg"), s2("hijklmn ");

    String s3 = s2 + s1 ;

    cout << "S1 = "; s1.display();
    cout << "S2 = "; s2.display();
    cout << "S3 = "; s3.display();

    return 0;
}

[/Use]


The output of the above program is as follows.

String constructor called abcdefg
String constructor called hijklmn
default constructor 0xbff1e840
temp = hijklmn adcdefg address of p = 0xbff1e844 object this = 0xbff1e840
S1 = abcdefg address of p = 0xbff1e864 object this = 0xbff1e860
S2 = hijklmn address of p = 0xbff1e854 object this = 0xbff1e850
S3 = hijklmn abcdefg address of p = 0xbff1e844 object this = 0xbff1e840
destructor invoked
destructor invoked
destructor invoked

why is the copy constuctor not called for S3 and also why is the destructor not called for temp?

The C++ standard explicitly allows compilers to eliminate temporary objects if the only way of detecting if those temporary objects exist is to track constructor and destructor calls.

A special case of this is the Return Value Optimisation (RVO), which your compiler is implementing. This optimisation basically means the compiler can avoid multiple copies if a variable within your function (eg temp within operator+()) will only be copied into a variable in the caller (eg S3 in main()).

in the call to the operator+ you use

String temp;

which invokes the default constructor.

Thanx,
Sean

Oh...

Right the compiler can avoid making the copy on return. What he said. Sorry, waas a bit distracted there...

Sean

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.