我注意到,即使构造函数没有参数,前者也会进入我创建的构造函数,而后者仅在需要参数时才进入我创建的构造函数。
赢
// ConsoleApplication11.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
class myRectangle {
int width;
public:
int getWith();
void setWidth(int newWidth) {width = newWidth;};
myRectangle (int);
~myRectangle ();
};
myRectangle::myRectangle (int w) {
width = w;
cout << "myRectangel Constructor\n";
}
myRectangle::~myRectangle() {
cout << "destructor\n";
}
void runObject();
int _tmain(int argc, _TCHAR* argv[])
{
runObject();
int exit; cout << "\n\n";
cin >> exit;
return 0;
}
void runObject()
{
myRectangle mr (5);
}
FAIL // ConsoleApplication11.cpp : 定义控制台应用程序的入口点。//
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
class myRectangle {
int width;
public:
int getWith();
void setWidth(int newWidth) {width = newWidth;};
myRectangle ();
~myRectangle ();
};
myRectangle::myRectangle () {
cout << "myRectangel Constructor\n";
}
myRectangle::~myRectangle() {
cout << "destructor\n";
}
void runObject();
int _tmain(int argc, _TCHAR* argv[])
{
runObject();
int exit; cout << "\n\n";
cin >> exit;
return 0;
}
void runObject()
{
myRectangle mr ();
}