标签:对象指针 class out sizeof pac turn erb 构造 his
浅拷贝:拷贝后,两个对象会共同指向一个地址,如果有一个对象更改了数据,另一个对象也会受影响,删除对象话,可能会导致释放两次
#include <iostream>
using namespace std;
//.h
class student {
public:
student();
student(string name);
student(const student &stu);
~student();
string getName();
int age;
private:
string m_name;
};
//cpp 这叫定义
student::student() {
m_name="kitty";
m_p=new int[5];
cout<<"gou zao"<<endl;
}
string student::getName(){
return m_name;
}
student::student(string name) {
m_name=name;
}
//拷贝构造函数 (浅拷贝)
student::student(const student &stu) {
// 如果通过 student stu1=stu 这样的方式创建度对象,会执行拷贝构造函数
m_name=stu.m_name; //浅拷贝
m_p=stu.m_p;//浅拷贝 将复制的对象的 指针拷贝给了 另一个对象, 现在两个对象 共同指向了一个 地址
cout<<"qian kaobei"<<endl;
}
student::~student() {
delete []m_p; //这样删除的话,浅拷贝会出问题
cout<<"xi gou"<<endl;
}
void student::printAddr() {
cout<<"dizhi:"<<*m_p<<endl;
}
void test(student s){
}
int main() {
student s;
student s1(s);
s.printAddr();
s1.printAddr();
return 0;
}
#include <iostream>
#include <cstdlib>
using namespace std;
//.h
class student {
public:
student();
student(string name);
student(const student &stu);
~student();
string getName();
void printAddr();
private:
int count;
string m_name;
int *m_p;
};
//cpp 这叫定义
student::student() {
m_name="kitty";
count=5;
m_p=new int[count]; //初始化数组
for (int i = 0; i < 5; i++) {//数组赋值
m_p[i]=i;
cout<<"value:"<<m_p[i]<<endl;
}
cout<<"gou zao"<<endl;
}
string student::getName(){
return m_name;
}
student::student(string name) {
m_name=name;
}
//拷贝构造函数 (深拷贝)
student::student(const student &stu) {
count=stu.count;
//深拷贝 重新开辟一块内存空间
m_p=new int [count];
for (int i = 0; i <count; i++)
{
m_p[i]=stu.m_p[i];
}
cout<<"shen kaobei"<<endl;
}
student::~student() {
delete []m_p;
cout<<"xi gou"<<endl;
}
void student::printAddr() {
cout<<"dizhi:"<<m_p<<endl;
}
void test(student s){
}
int main() {
student s;
student s1(s);
s.printAddr();
s1.printAddr();
return 0;
}
int main() {
student *s=new student();
s->printAddr();
student *s1=new student();
(*s1).printAddr();
return 0;
}
对象成员指针 类似JAVA中的 对象属性
例如:private Bean bean;
指针类型大小为:4
#include <iostream>
#include <cstdlib>
using namespace std;
class Teacher {
public:
Teacher(string name, int age);
~Teacher();
string getName();
int getAge();
private:
string m_Name;
int m_Age;
};
//.h
class Student {
public:
Student(string nameA, int ageA, string nameB , int ageB);
~Student();
void printInfo();
private:
Teacher *m_teacherA;
Teacher *m_teacherB;
};
Teacher::Teacher(string name, int age) {
m_Name = name;
m_Age = age;
cout << "Teacher gou zao name: " << m_Name << " age: " << m_Age << endl;
}
Teacher::~Teacher() {
cout << "Teacher xi gou name: " << m_Name << " age: " << m_Age << endl;
}
int Teacher::getAge() {
return m_Age;
}
string Teacher::getName() {
return m_Name;
}
//cpp 这叫定义
Student::Student(string nameA, int ageA, string nameB , int ageB) {
m_teacherA=new Teacher(nameA,ageA);
m_teacherB=new Teacher(nameB,ageB);
cout << "Student gou zao" << endl;
}
Student::~Student() {
delete m_teacherA;
delete m_teacherB;
cout << "Student xi gou" << endl;
}
void Student::printInfo() {
cout << "printInfo" << endl;
cout << "( A: " <<m_teacherA->getName()<<","<<m_teacherA->getAge()<<")"<< endl;
cout << "( B: " <<m_teacherB->getName()<<","<<m_teacherB->getAge()<<")"<< endl;
}
int main() {
Student *s=new Student("kitty",20,"tony",22);
(*s).printInfo();
cout<< sizeof(s)<<endl;
cout<< sizeof(Student)<<endl;
delete s;
return 0;
}
输出结果:
Teacher gou zao name: kitty age: 20
Teacher gou zao name: tony age: 22
Student gou zao
printInfo
( A: kitty,20)
( B: tony,22)
4
8
Teacher xi gou name: kitty age: 20
Teacher xi gou name: tony age: 22
Student xi gou
和JAVA中的this指针差不多一个意思
标签:对象指针 class out sizeof pac turn erb 构造 his
原文地址:https://www.cnblogs.com/wuyanzu/p/11874308.html