C++ 练习2:一般的构造,拷贝构造,赋值操作,析构,static_cast

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cassert>
#define BUF_SIZE  80

using namespace std;

class NODE_BUF
{
    public:
      explicit NODE_BUF(void)//构造函数 NODE_BUF a ;
      {
          _buf=new char[BUF_SIZE];
          if (_buf != NULL)
          memset(_buf, 'u', sizeof(char)*BUF_SIZE);
          cout<<"construct : _buf's address="<<(static_cast<const void *>(_buf))<<endl;
      }
      NODE_BUF(const NODE_BUF &node)//拷贝构造,支持 NODE_BUF c(a)   或 NODE_BUF  c=a ;这两种方式效果是一样的。
      {
         _buf = new char[BUF_SIZE];//深拷贝
         if (_buf != NULL)
         memcpy(_buf, node._buf, sizeof(char)*BUF_SIZE);
         cout<<"copy construct: _buf's address="<<(static_cast<const void *>(_buf))<<endl;
      }

      NODE_BUF & operator =(const NODE_BUF &node)//赋值:支持对象间赋值操作。
      {
         if (this->_buf == node._buf)
         return *this;
   
         memcpy(this->_buf, node._buf,  sizeof(char)*BUF_SIZE);  
         cout<<"assignment =: addr buf start="<<(static_cast<const void *>(this->_buf))<<endl;   
         return *this;
      }  

      void * get_buf_start(void)
      {  
         return (void *)_buf;   
      }  
      void * get_buf_end(void)
      {
        return (void *)(_buf+sizeof(char)*BUF_SIZE);
      }
  
      void set_buf(char *src, int size)
      {  
        assert(src and size>0 and size < BUF_SIZE);
        memcpy(this->_buf, src, size);
      }
 
      ~NODE_BUF(void)//析构
      {
          delete []_buf;
          cout<<"destory Ok: "<<(static_cast<const void *>(_buf))<<endl;
      }
    private:
      char *_buf;
}; 
int main(void)
{
  NODE_BUF a; //定义对象a,调用构造函数explicit NODE_BUF(void)
  cout<<"a buf="<<static_cast<char *>(a.get_buf_start())<<endl;//打印a对象所指的缓冲区,在构造函数中被初始化为80个'u'.
  NODE_BUF *pNew =new NODE_BUF;//也可以这样构造对象,但在main函数结束之前,需要手动去删除pNew指针,否则会导致内存泄露
  NODE_BUF *pa =&a; //定义指向对象a 的一个指针。
  cout<<"pa ="<<pa<<",start="<<pa->get_buf_start()<<",end="<<pa->get_buf_end()<<endl; //分析此指针指向对象的buf开始和结束地址
  cout<<"first char="<<*((static_cast<char *>(pa->get_buf_start())))<<endl;// 对象缓冲区内的第一个字符
  cout<<"Last char=" <<*((static_cast<char *>(pa->get_buf_end()-1)))<<endl;//对象缓冲区内的最后一个字符
 
 
  NODE_BUF b;//定义对象b,调用构造函数explicit NODE_BUF(void)
  b=a; //调用类的赋值操作函数 NODE_BUF & operator =(const NODE_BUF &node)
 
  char src[]="1234567890qwertyuiopasdfghjklzxcvbnm";
  a.set_buf(src, sizeof(src));
  cout<<"a buf="<<static_cast<char *>(a.get_buf_start())<<endl;//打印被修改过后的a对象缓冲区。

  NODE_BUF c =a; //使用拷贝构造函数  NODE_BUF(const NODE_BUF &node) ,用a来赋值c
  cout<<"c buf="<<static_cast<char *>(c.get_buf_start())<<endl;//打印C对象缓冲区。
  cout<<"Obj c's buf_start addr="<<c.get_buf_start()<<",Obj c's buf_end="<<c.get_buf_end()<<endl;
  cout<<endl<<endl;
  delete pNew;
  return 0;
}


转载于:https://my.oschina.net/mingfu/blog/527512

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值