zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C++之4种实例化对象方式(一百一三十一)

C++实例对象 方式 三十一 一百
2023-09-14 09:09:57 时间

1.程序例子

#include <iostream>
using namespace std;
 
class Student{
public:
  Student(){
    printf("%s(), line = %d\n",__FUNCTION__,__LINE__);
  }

  Student(int cc){
    this->data = cc;
    printf("%s(), line = %d, data = %d\n",__FUNCTION__,__LINE__,this->data);
  }
  int data;
};

int main(){
  //1.隐式创建(栈上创建)
  Student st1;

  //2.显示创建(栈上创建)
  Student st2 = Student();

  //3.new堆上创建
  Student *st3 = new Student;

  //4.new实例化后直接赋值给st4对象
  Student *st4(new Student());
  
  //Student *st5(new Student(44));
	       
  return 0;
}