当前位置: 代码迷 >> 综合 >> C++,类的对象,其地址,栈上还是堆上?
  详细解决方案

C++,类的对象,其地址,栈上还是堆上?

热度:87   发布时间:2024-01-19 15:36:27.0

 地址,存贮空间,指针自身在哪里,指针指向的存储空间的地址是? 大小是?

#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
// 存
void setname(string s){	name = s;}
void setage(int y){age = y;	}
void setaddress(string add){address = add;}    
// 取
string getname(){return name;}
int getage(){return age;}
string getaddress(){return address;}
//-- 引入构造函数:初始化成员,与类同名,没有返回值
Student(string name,int age,string address)
{
this->name = name; this->age = age; this->address = address;      
}
~Student(){ 
}
protected:
private:
string name;
int    age;
string address;
};
void main()
{
// x1在栈上,存储空间 36字节
Student x1("刘莉莉x1",22,"东风路369号"); 
cout<<"sizeof x1 = "<<sizeof x1<<"   &x1地址:"<<&x1<<endl; //36个字节
// p1自身在栈上:
  相关解决方案