请问,关于构造函数和类型转换
请教,关于构造函数和类型转换。
以下是树上的内容:
// test.h
#include <iostream.h>
class Test {
private:
int num;
public:
~Test();
Test( int );
void print();
};
Test::Test(int n)
{
num = n;
cout << "Intializing" << num << endl;
}
void Test::print()
{
cout << num << endl;
}
Test::~Test()
{
cout << "Destorying" << num << endl;
}
----
#include "test.h"
void main()
{
Test Try(0);
Try = 5;
Try.print();
Try = Test(10);
Try.print();
}
----
书中对输出结果的注释:
Initializing 0 // 建立对象Try
Initializing 5 // 建立隐藏对象
Destroying 5 // 析构第一次建立的对象Try
5 // 输出建立的隐藏对象之数据
Initializing 10 // 由强制类型转换建立数据为10的对象
Destroying 10 // 析构建立的隐藏对象Try(num=5)
10 // 输出由强制类型转换所建立对象之数据
Destroying 10 // 析构强制类型转换所建立的对象Try(num=10)
----
这些注释对吗?
------解决方案--------------------
请看:
隐式类型转换和explicit关键字的作用
包你不再有此类问题。
------解决方案--------------------
楼主,请珍惜生命,扔了那个还让你#include <iostream.h>的垃圾书籍及垃圾编译器。
以下是树上的内容:
// test.h
#include <iostream.h>
class Test {
private:
int num;
public:
~Test();
Test( int );
void print();
};
Test::Test(int n)
{
num = n;
cout << "Intializing" << num << endl;
}
void Test::print()
{
cout << num << endl;
}
Test::~Test()
{
cout << "Destorying" << num << endl;
}
----
#include "test.h"
void main()
{
Test Try(0);
Try = 5;
Try.print();
Try = Test(10);
Try.print();
}
----
书中对输出结果的注释:
Initializing 0 // 建立对象Try
Initializing 5 // 建立隐藏对象
Destroying 5 // 析构第一次建立的对象Try
5 // 输出建立的隐藏对象之数据
Initializing 10 // 由强制类型转换建立数据为10的对象
Destroying 10 // 析构建立的隐藏对象Try(num=5)
10 // 输出由强制类型转换所建立对象之数据
Destroying 10 // 析构强制类型转换所建立的对象Try(num=10)
----
这些注释对吗?
------解决方案--------------------
请看:
隐式类型转换和explicit关键字的作用
包你不再有此类问题。
------解决方案--------------------
楼主,请珍惜生命,扔了那个还让你#include <iostream.h>的垃圾书籍及垃圾编译器。