| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 580 人关注过本帖
标题:求助一个constructor和deconstructor的问题
只看楼主 加入收藏
winglesswu
Rank: 1
等 级:新手上路
帖 子:92
专家分:0
注 册:2013-1-28
结帖率:71.88%
收藏
已结贴  问题点数:11 回复次数:2 
求助一个constructor和deconstructor的问题
要求写一个constructor和deconstructor,分别是:
1 ISBNPrefix(const char* filename) 这个函数接收一个包含一个文件名的字符串,该constructor仅打开文件
a one-argument constructor that receives a C-style, null-terminated string containing the name of a file that holds the prefix table
 ISBNPrefix(const char* filename);
This constructor opens the file for read only access.
2 deconstructor的要求是:
a destructor that cleans up before the current object goes out of scope
 ~ISBNPrefix();

如果class里的变量是普通变量,我知道怎么写,但是这个变量是指针,我就有点不明白了。我的代码分别是:
1 ISBNPrefix::ISBNPrefix(const char* filename)
    {
        FILE *fp;
        if (fp!=NULL)
            fp=fopen(filename, "r");
        else
            fp=NULL;
    }
2 ISBNPrefix::~ISBNPrefix()
    {
        if (fp!=NULL)
        fclose(fp);
        else
            fp=NULL;
    }
请知道的朋友指点一下,谢谢。
搜索更多相关主题的帖子: 字符串 before current receives file 
2013-06-25 10:51
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:6 
基础知识还是要看点书的。差不多是这样:

struct ISBNPrefix
{
    ISBNPrefix(const char* filename) : file(filename)
    {
    }

    ~ISBNPrefix()
    {
        file.close();
    }

    std::ifstream file;
};
2013-06-25 11:21
wk123586
Rank: 1
等 级:新手上路
帖 子:1
专家分:6
注 册:2013-6-25
收藏
得分:6 
2013-06-25 12:01
快速回复:求助一个constructor和deconstructor的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016567 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved