--- Annamalai Gurusami <[EMAIL PROTECTED]>
wrote:
> Ray Devore wrote:
> > 
> >  > > Samp obj = Samp(12,22) // Explicit Call
> > Calls constructor (not copy constructor) for
> > Samp(12,22) and creates a temp object, then calls
> copy
> > constructor to create and initialize obj with the
> temp
> > object, then calls the destructor to delete the
> temp
> > object so you get two additional function calls
> with
> > the second method.
> 
> I wrote a sample program to check if the copy
> constructor would be 
> called for the above statement.  But no copy
> constructor gets called. 
> So I think there is no difference between the
> following two statements.
> 
> Type obj(p);
> Type obj = p;
> 
> Here is the sample program.
> 
> (begin-code)
> 
> #include <iostream>
> #include <string>
> 
> using namespace std;
> 
> class T
> {
> public:
>     T(const string& n): name(n) {
>        cout << "ctor: " << name << endl;
>     }
>     T(const T& that) {
>        name = that.name;
>        cout << "copy ctor: " << name << endl;
>     }
>     ~T() {
>        cout << "dtor: " << name << endl;
>     }
> private:
>     string name;
> };
> 
> int main()
> {
>     T x("1");
>     T y = T("2");
> }
> 
> (end-code)
> 
I added:

const T& operator=(const T& that) 
{
    name = that.name;
    cout << "operator=: " << name << endl;
    return *this;
}

and the assignment operator is not called either.
It appears that the compiler treats:
    T y = T("2");
as if it was:
    T y("2");

Thanks for the clarification.

Ray


       
____________________________________________________________________________________Luggage?
 GPS? Comic books? 
Check out fitting gifts for grads at Yahoo! Search
http://search.yahoo.com/search?fr=oni_on_mail&p=graduation+gifts&cs=bz

Reply via email to