复制构造函数与等号的操作符重载

//头文件
#include <iostream>
#ifndef STRNGBAD_H_
#define STRNGBAD_H_
class StringBad
{
private:
    
char * str;
    size_t len;
    
static int num_strings;
public:
    StringBad(
const char * s);

    
//定义了复制构造函数,可以在通过已有对象创建一个复制对象时使用,如果不提供此构造函数编译器会自动提供一个,但是默认的只提供浅拷贝,则会使两个对象的指针指向同一块内存,析构两次将出错
    StringBad(const StringBad & st);
    
    StringBad();
    
~StringBad();
    friend std::ostream 
& operator << (std::ostream & os,
        
const StringBad & st);

    
//在使一个对象等于另一个对象时,使用这个操作符重载,同复制构造函数相同,如果不提供编译器将提供默认的,但是只作浅拷贝
    StringBad & operator = (const StringBad & st);
}
;
#endif
//cpp文件
#include <cstring>
#include 
"stringbad.h"

using std::cout;

int StringBad::num_strings=0;

StringBad::StringBad(
const char * s)
{
    len
=std::strlen(s);
    str
=new char[len+1];
    std::strcpy(str,s);
    num_strings
++;
    cout
<<num_strings<<": \""<<str<<"\" object created\n";
}


StringBad::StringBad(
const StringBad & st)
{
    num_strings
++;
    len
=st.len;
    str
=new char[len+1];
    std::strcpy(str,st.str);
    cout
<<num_strings<<":\""<<str
        <<"\"object created\n";
}


StringBad::StringBad()
{
    len
=4;
    str
=new char[4];
    std::strcpy(str,
"C++");
    num_strings
++;
    cout
<<num_strings<<": \""<<str<<"\"default object created\n";
}


StringBad::
~StringBad()
{
    cout
<<"\""<<str<<"\"object deleted, ";
    
--num_strings;
    cout
<<num_strings<<" left\n";
    delete[] str;
    str
=NULL;
}


std::ostream 
& operator << (std::ostream & os,
                            
const StringBad & st)
{
    os
<<st.str;
    
return os;
}


StringBad 
& StringBad::operator = (const StringBad & st)
{
    
if (this==&st)
        
return *this;
    delete[] str;
    len
=st.len;
    str
=new char[len+1];
    std::strcpy(str,st.str);
    
return *this;
}

//调用测试
// dd.cpp : 定义控制台应用程序的入口点。
//

#include 
"stdafx.h"
#include 
<iostream>
#include 
<cmath>
#include 
<string>
#include 
<fstream>

#include 
<iostream>
using std::cout;
#include 
"stringbad.h"

void callme1(StringBad &);
void callme2(StringBad);

int _tmain(int argc, _TCHAR* argv[])
{
    
using std::endl;
    StringBad headline1(
"Celery Stalks at midnight");
    StringBad headline2(
"lettuce Prey");
    StringBad sports(
"Spinach Leaves Bow1 for Dollars");
    cout
<<"headline1:"<<headline1<<endl;
    cout
<<"Headline2:"<<headline2<<endl;
    cout
<<"sports:"<<sports<<endl;
    callme1(headline1);
    cout
<<"headline1:"<<headline1<<endl;
    callme2(headline2);
    cout
<<"headline2:"<<headline2<<endl;
    cout
<<"Initialize one ojbect to another:\n";
    StringBad sailor
=sports;
    cout
<<"sailor:"<<sailor<<endl;
    cout
<<"Assign one object to another:\n";
    StringBad knot;
    knot 
=headline1;
    cout
<<"knot:"<<knot<<endl;
    cout
<<"End of main()\n";
    
return 0;
}


void callme1(StringBad & rsb)
{
    cout
<<"String passed by refence:\n";
    cout
<<"    \""<<rsb<<"\"\n";
}


void callme2(StringBad sb)
{
    cout
<<"String passed by value:\n";
    cout
<<"    \""<<sb<<"\"\n";
}
posted @ 2008-01-15 14:56  吴东雷  阅读(1361)  评论(1编辑  收藏  举报