Fork me on GitHub

C++ 成员变量

4. 成员变量

4.1 Fields, parameters, local variables

  • All three kinds of variable are able to store a value that is appropriate to their defined type.

4.1.1 Fields

  • Fields are defined outside constructors and methods.

  • Fields are used to store data that persists throughout the life of an object. As such, they maintain the current state of an object. They have a lifetime that lasts as long as their object lasts.

  • Fields have class scope: their accessibility extends throughout the whole class, and so they can be used within any of the constructors or methods of the class in which they are defined.

  • a.h

#ifndef _A_H_
#define _A_H_

void f();

extern int global;

class A {
private:
    int i;
public:
    void f();
};

#endif
  • a.cpp
#include <iostream>

#include "a.h"

void A::f() {
    // j 为本地变量,仅在 f() 中有效
    int j = 10;
    // i 为成员变量,在类 A 的所有函数中可以使用
    // 在创建 A 类的对象后,成员变量才有效
    i = 20;
    std::cout << "i = " << i << ", j = " << j << std::endl;
}

int main()
{
    A a;
    a.f();
    return 0;
}

4.1.2 本地变量(local variable)

  • Local variables are defined inside a method, have a scope limited to the method to which they belong.

  • 深入理解 a.cpp

#include <iostream>

class A
{
public:
    // 对变量 i 的声明
    int i;
    void f();
};

void A::f()
{
    std::cout << "before i = " << i << std::endl;
    i = 20;
    std::cout << "after i = " << i << std::endl;
}

struct B
{
    int i;
};

void f(struct B* p)
{
    p->i = 20;
    std::cout << "struct B: " << p->i << std::endl;
}


int main()
{
    A a;
    A aa;
    B b;
    a.i = 10;
    std::cout << "before main i = " << a.i << std::endl;
    // f 函数属于 类A,不是属于 对象a
    // a.f() 是让 对象a 去做 类A的函数 f 中的动作
    // 等同于 A::f(&a) 
    a.f();
    std::cout << "after main i = " << a.i << std::endl;
    std::cout << "==== aa ====" << std::endl;
    aa.i = 100;
    aa.f();
    f(&b);
    return 0;
}

4.2 Call functions in a class

  • There is a relationship with the function be called and the variable calls it.
  • The function itself knows it is doing something with the variable.
  • 通过打印地址,深入理解 a.cpp
  • 以 32 位进行编译:g++ -m32 a.cpp
#include <stdio.h>

class A
{
public:
    // 对变量 i 的声明
    int i;
    void f();
};

void A::f()
{
    i = 20;
    // 输出 i 的地址
    printf("A::f() -- &i = %p\n", &i);
    // 打印 this 的地址
    printf("this = %p\n", this);
}

struct B
{
    int i;
};

void f(struct B* p)
{
    p->i = 20;
}


int main()
{
    A a;
    A aa;
    a.i = 10;
    printf("&a = %p\n", &a);
    printf("&a.i = %p\n", &(a.i));
    a.f();
    printf("&aa = %p\n", &aa);
    aa.f();
    return 0;
}
  • 运行结果:

4.2.1 this: the hidden parameter

  • this is a hidden parameter for all member functions, with the type of the class
  • a.cpp
// 定义函数 f()

void A::f() {
    // 省略...
}

// 等同于

void A::f(A *p) {
    // 省略...
}
  • To call the function, you must specify a variable;
  • main.cpp
// 通过对象,调用函数 f()

A a;
a.f();

// 等同于

A::f(&a);

参考资料:

posted @ 2022-05-11 16:45  小a的软件思考  阅读(151)  评论(0编辑  收藏  举报