C++工程实战入门笔记9-面向对象之成员变量和成员函数
·
面向对象简单展示,了解概念
#include<string>
#include <iostream>
using namespace std;
class Npc
{
public:
string name_;//下划线表示成员变量区分于局部变量
int health_=100;
void Atack(int atk)
{
health_ -= atk;
cout << name_ << "was attacked!" << "-" << atk << endl;
}
private:
};
int main()
{
Npc npc1;
npc1.name_ = "class npc1 ";
Npc npc2;
npc2.name_ = "class npc2 ";
npc1.Atack(50);
npc2.Atack(60);
system("pause");
}

成员变量
class MyClass
{
public:
//成员变量
int x{ 0 };//C++11开始可以在声明时赋初值
int y{ 0 };
char name[16]{ "myclass name" };
};
int main()
{
//栈区对象
MyClass mc1;
cout << "MyClass mc1" << endl;
cout << "sizeof(mc1)="<<sizeof(mc1) << " sizeof(MyClass)=" << sizeof(MyClass) << endl;
cout <<"mc1.name = "<< mc1.name << endl;
cout << "mc1.x = " << mc1.x << endl;
cout << endl;
//堆区对象指针
MyClass* mc2=new MyClass;
mc2->x = 100;
cout << "mc2->x = "<< mc2->x << endl;
cout << "(*mc2).x= "<<(*mc2).x << endl;//指针变对象
delete mc2;
cout << endl;
MyClass* mc3 = &mc1;//对象变指针
//自定义类型赋值
MyClass mc4;
mc1.x = 99;
mc4 = mc1;//复制mc1的所有成员变量给mc4
cout <<"复制mc1的所有成员变量给mc4: "<< "mc4.x = " << mc4.x << " mc1.x = " << mc1.x << endl;
system("pause");
}

成员函数(内联函数和非内联函数)
成员函数是类的一部分,可以访问类的私有成员(如数据变量)。
类内定义:直接在类声明中实现函数体。
类外定义:在类声明中声明函数,在类外部实现函数体(需要使用作用域解析运算符 ::)
class MyClass {
public:
void nonInlineFunc(); // 声明非内联成员函数(类外定义)
void inlineFunc() { // 类内定义的成员函数自动成为内联函数
// 函数体
}
};
void MyClass::nonInlineFunc() { // 类外定义的非内联函数
// 函数体
}
内联函数有两种定义方式
类内定义自动内联:在类声明内部直接定义函数体时,编译器自动将其视为内联函数。
class MyClass {
public:
void inlineFunc() { // 自动内联
// 简单操作
}
};
类外定义需显式声明:如果在类外部定义函数,必须使用 inline 关键字显式标记,否则不会内联。但即使使用 inline,编译器可能忽略请求(如果函数过大)
class MyClass {
public:
void inlineFunc(); // 声明
};
inline void MyClass::inlineFunc() { // 显式内联定义
// 函数体
}

练习代码
class MyClass
{
public:
//成员变量
int x{ 0 };//C++11开始可以在声明时赋初值
int y{ 0 };
char name[16]{ "myclass name" };
void TestIn(int v)//内联函数
{
x += v;
y += v;
cout <<"inline: "<< x << ":" << y << endl;
cout << name << endl;
}
void Test(int v);//普通函数,非内联函数,声明和定义是分开的,此处的声明,定义见下方函数 MyClass::Test(int v)
};//以上都是放到.h头文件中
void MyClass::Test(int v)
{
x += v;
y += v;
cout << "no-inline: " << x << ":" << y << endl;
cout << name << endl;
}
int main()
{
//成员函数-内联函数
MyClass mcf;
mcf.TestIn(10);
mcf.Test(10);//大部分我们都是用非内联
system("pause");
}

使用建议
- 优先内联:对于简单、短小的函数(如返回成员变量的函数),使用类内定义或显式
inline。 - 避免内联:对于递归、循环密集或大型函数,使用非内联方式以防止代码膨胀。
this指针
this指针的本质
- 隐含参数:每个非静态成员函数(包括内联和非内联)都隐含一个
this指针参数,指向调用该函数的对象实例。 - 类型规则:
this是ClassName *const类型(常量指针),不可修改指针值,但可通过它修改对象成员。 - 访问方式:通过
this->member访问成员变量/函数,或隐式使用(如直接写member等价于this->member)。
class MyClass {
int value;
public:
void setValue(int v) { // 非内联函数(类外定义时)
this->value = v; // 显式使用this避免命名冲突
}
void print() const; // 声明
};
// 类外定义
void MyClass::print() const {
std::cout << this->value; // 通过this访问成员
}
代码
class MyClass
{
public:
//成员变量
int x{ 0 };//C++11开始可以在声明时赋初值
int y{ 0 };
char name[16]{ "myclass name" };
void TestIn(int v)//内联函数
{
//this指针:当前类对象的地址
cout << "this addr = " << (long long)this << endl;
x += v;
y += v;
cout << "x:y: " << x << ":" << y << endl;
cout << "this->x:y : " << this->x << ":" << this->y << endl;
cout << "this->name: " << this->name << endl;
cout << "name: "<<name << endl;
}
};
int main()
{
MyClass mcf;
mcf.TestIn(10);
cout << "mcf addr = " << (long long)&mcf << endl;
system("pause");
}

更多推荐



所有评论(0)