/*<span style="font-size:32px;color:#FF0000;">在写一个简单的多态例子的时候总是出错,后来发现是
在构造函数上面有问题 少写了new,结果依旧运行,那
么构造函数返回了什么呢</span>*/
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
class person {
protected:
string name;
public:
virtual void showinfo();
person(string name) :name(name) { cout << "this is person 构造函数" << endl; }
person(person& rhs) { cout << "this is person拷贝构造函数" << endl; }
};
void person::showinfo() {
cout << name<<endl;
}
class teacher :public person{
protected:
string prefession;
public:
void showinfo();
teacher(teacher& rhs):person(rhs) { cout << "this is teacher 拷贝构造函数" << endl; }
teacher(string name, string prefession) :person(name), prefession(prefession) { cout << "this is teacher 构造函数" << endl; }
};
void teacher::showinfo() {
cout << name << " " << prefession << endl;
}
class student :public person {
protected:
int s_number;
public:
void showinfo();
student(string name, int nu) :person(name), s_number(nu) { cout << "this is student 构造函数"<<endl; }
};
void student::showinfo() {
cout << name << " " << s_number << endl;
}
int main()
{
person d = teacher("luyu", "jiaoshi");//构造函数这样写
d.showinfo();
person* b = new teacher("luyu", "jiaoshi");//或者这么写
b->showinfo();
delete b;
return 0;
}
运行结果是这样的,多调用了一个拷贝构造函数,那么构造函数返回的应该是this指针
对汇编无感,知乎上面的朋友给力
版权意识要浓厚
构造函数返回了一个this指针。。。。