关于构造函数和析构函数的一个小问题

gallanthunter 2009-02-11 11:13:53

#include <string>
#include <iostream>
using namespace std;
class CStudent
{
public:
student(int n,string nam,char s)
{
num = n;
name = nam;
sex = s;
cout << "Constructor called." << endl;
}

~student() //调用析构函数
{
cout << "Constructor called." << endl;
}
void display()
{
cout << "num:" << num << endl;
cout << "name:" << name << endl;
cout << "sex:" << sex << endl;
}
private:
int num;
string name;
//char name[10];
char sex;
};

int main()
{
CStudent student1(100, "zhangzhijun", 'f');
student1.display();
return 0;
}

请问各位,上面的例子中析构函数和构造函数这样使用是否正确?
另外类对象建立的时候能否直接对其赋值,如CStudent student1(100, "zhangzhijun", 'f');



...全文
227 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
gallanthunter 2009-02-12
  • 打赏
  • 举报
回复
为什么我在编译的时候会提示下面的错误呢?
Compiling...
Cpp1.cpp
c:\documents and settings\administrator\cpp1.cpp(13) : warning C4183: 'student': member function definition looks like a ctor, but name does not match enclosing class
c:\documents and settings\administrator\cpp1.cpp(15) : error C2523: 'CStudent::~student' : destructor tag mismatch
c:\documents and settings\administrator\cpp1.cpp(34) : error C2661: 'CStudent::CStudent' : no overloaded function takes 3 parameters
Error executing cl.exe.
gallanthunter 2009-02-12
  • 打赏
  • 举报
回复
谢谢各位了!
rabocheng 2009-02-12
  • 打赏
  • 举报
回复
class CStudent
{
public:
CStudent(int n,string nam,char s):num(n),name(nam),sex(s)
{
cout << "Constructor called." << endl;
}

~CStudent() //调用析构函数
{
cout << "Constructor called." << endl;
}
void display()
{
cout << "num:" << num << endl;
cout << "name:" << name << endl;
cout << "sex:" << sex << endl;
}
private:
int num;
string name;
//char name[10];
char sex;
};

应该改成这样:)呵呵
wlk 2009-02-12
  • 打赏
  • 举报
回复
楼主写的显然不是构造函数和析构函数.

构造函数名=类名
析构函数名=~类名
scott_2009 2009-02-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 baihacker 的回复:]
用初始化列表好些...

初始化列表是初始化的表现

从无到有

你赋值是改变的表现

从有到有
[/Quote]
up
初始化列表初始化
构造
赋值初始化
构造+赋值
l274516903 2009-02-12
  • 打赏
  • 举报
回复
构造函数和析构函数与类名应该相同吧
ajtomato 2009-02-12
  • 打赏
  • 举报
回复
class CStudent
{
public:
student(int n,string nam,char s)
{
num = n;
name = nam;
sex = s;
cout << "Constructor called." << endl;
}

~student() //调用析构函数
{
cout << "Constructor called." << endl;
}
类名:CStudent
构造和析构:student

typo : )
yangguan0104 2009-02-11
  • 打赏
  • 举报
回复
这样没有什么问题
Chevin 2009-02-11
  • 打赏
  • 举报
回复
记得好像C++ Primer里面有说的,你可以查阅一下相关章节
初始化分为直接初始化和复制初始化。
两者是有稍微不同的。
直接初始化效率更高:CStudent student1(100, "zhangzhijun", 'f');
waizqfor 2009-02-11
  • 打赏
  • 举报
回复
[Quote=引用楼主 gallanthunter 的帖子:]
C/C++ code
#include <string>
#include <iostream>
using namespace std;
class CStudent
{
public:
student(int n,string nam,char s)
{
num = n;
name = nam;
sex = s;
cout << "Constructor called." << endl;
}

~student() //调用析构函数
{
cout << "Constructor called." << endl;
}
void display()
{

[/Quote]
感觉用构造函数初始化成员 逻辑上看着比较好

chin_chen 2009-02-11
  • 打赏
  • 举报
回复
显然可以了啊。
baihacker 2009-02-11
  • 打赏
  • 举报
回复
用初始化列表好些...

初始化列表是初始化的表现

从无到有

你赋值是改变的表现

从有到有

65,211

社区成员

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

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