验证友元与拷贝构造函数

最近本人想复习下基础知识,于是写了些验证这些知识的例子

#include <fstream>
#include <string.h>
using namespace std;
ofstream out("howmany2.out");

class object;
class howmany2;


class howmany2
{
private:
 enum { bufsize = 30};
 char id[bufsize];
 static int object_count;
 int _id;
public:
    howmany2(const char *ID = 0)
 {
  if ( ID )
  {
   strncpy(id, ID, bufsize);
  }
  else
  {
   *id = 0;
  }
  ++object_count;
  printf("howmany2/n/r");
 }
 howmany2(const howmany2 &h)
 {
  strncpy(id, h.id, bufsize);
  strncat(id, "copy", bufsize - strlen(id));
  ++object_count;
  printf("howmany2 (howmany2 &h)/n/r");
 }
 static void print(char *s)
 {
  printf("%s/n/r", s);
 }
 friend class object;
};

class object
{
 howmany2 &my;
public:
 object ( howmany2 &h):my(h)
 {
  //strncat(h.id, "object", 7);
  //my = h;
  my._id = 1;
  printf("%d/n/r", my._id);
 }
};


int howmany2::object_count = 0;

howmany2 &f(howmany2 &x)
{
 howmany2 y;
 return y;
}

void main()
{
 howmany2 h;
    howmany2 &h2 = f(h);
 object ob(h);
 h2.print("A");
 howmany2::print("hello");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值