关于静态指针和智能指针吧

haofang666777 2011-11-15 10:36:17
错误很多;
有点无所适从;
#include<iostream>
using namespace std;

class Wilma
{
public:
Wilma();
void f();

};
Wilma::Wilma()
{
cout<<"Wilma Ctor\n";
}

void Wilma::f()
{
cout<<"Wilma used\n";
}

class Fred
{
public:
Fred();
protected:
class Init;
friend Init;
static Wilma* wilma_;
//Wilma* wilma();
};
class Fred::Init
{
public:
Init()
{
if(count_++==0)
wilma_=new Wilma();
}
~Init()
{
if(count_--==0)
delete wilma_;
}
private:
static unsigned count_;
};
static Fred::Init fredInit;
Fred::Fred()
{
cout<<"Fred ctor\n";
wilma_->f();

}

unsigned Fred::Init::count_=0;
Wilma* Fred::wilma_=NULL;
//Wilma Fred::wilma_;
Fred x;



int main()
{
return 0;
}
...全文
174 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xingfeng2510 2011-11-16
  • 打赏
  • 举报
回复
Init为Fred的受保护内部类,static Fred::Init fredInit;这一行编译通不过,
error C2248: “Fred::Init”: 无法访问 protected class(在“Fred”类中声明)
xingfeng2510 2011-11-16
  • 打赏
  • 举报
回复
Wilma类比较简单,没有什么可说的,主要分析后两个类。
在类Fred中,class Init; 这一句是Init类前置声明,声明在类内部,表明Init为Fred的内部类,
friend Init;这一行应该写作friend class Init;声明Init为Fred的友元。
三者之间的关系:Fred依赖Wilma、Init,Init负责Wilma静态实例的初始化、析构等。
angle_sean1 2011-11-16
  • 打赏
  • 举报
回复
你怎么能直接定义protect里面的成员呢。。。。。
lgp88 2011-11-16
  • 打赏
  • 举报
回复
改成 public 就可以了
haofang666777 2011-11-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xingfeng2510 的回复:]

Init为Fred的受保护内部类,static Fred::Init fredInit;这一行编译通不过,
error C2248: “Fred::Init”: 无法访问 protected class(在“Fred”类中声明)
[/Quote]
这是我同学的程序;
他说照书上敲的;(真不知道什么烂书)
但是很多错误,
我也是和你一样给他改的。
含有静态类得指针;
感觉应该是智能指针;
但是和智能指针的格式又差太远了;
真不知道这是什么书
modicum_lf 2011-11-16
  • 打赏
  • 举报
回复


static Fred::Init fredInit;

改为

static class Fred::Init fredInit;


haofang666777 2011-11-15
  • 打赏
  • 举报
回复
我同学问的一程序;
说是树上的应该不会有错;
不过我改了N多地方;
这程序感觉太乱了,什么都不像;
那位高人给解释一下;
这3个类的相互关系

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧