简介: CSDN博客专家,专注Android/Linux系统,分享多mic语音方案、音视频、编解码等技术,与大家一起成长!
优质专栏:Audio工程师进阶系列【原创干货持续更新中……】🚀
人生格言: 人生从来没有捷径,只有行动才是治疗恐惧和懒惰的唯一良药.

1.前言
本篇目的:理解C++之函数中实现类与调用实例用法
2.在函数中实现类的作用
在C++中在函数中实现类可以具有多种作用,包括但不限于:
-  封装性和局部性:在函数中实现类可以将类的定义限制在函数的作用域内,从而保持类的封装性和局部性。这样可以避免在整个程序中暴露类的细节,使得类的实现对于其他部分来说是不可见的,从而保持代码的模块化和封装。 
-  临时对象:在某些情况下,如果一个类只在一个函数中使用,将该类定义在函数中可以减少全局名称空间的污染,同时避免在整个项目中引入不必要的类定义。 
-  内部实现细节隐藏:在函数中实现类可以将类的具体实现细节隐藏起来,只暴露对外部代码有意义的接口,从而提高代码的安全性和可维护性。 
-  临时辅助类:有时候我们可能需要临时定义一些辅助类或者数据结构,在函数中定义这些类可以避免在整个项目中引入不必要的类定义。 
-  模板函数内部类:在模板函数内部定义类可以提供更灵活的方式来处理模板函数的特化。 
-  简化代码:在特定函数中定义类可以使代码更加紧凑和可读,因为相关的类定义直接与使用它们的代码放在一起。 
虽然在函数中实现类可以带来一些好处,但需要注意的是这样做也可能会导致一些问题,比如难以维护、可读性下降、类的复用性下降等。因此,在实际开发中需要根据具体情况权衡利弊。
3.代码实例
1.在函数中定义类并创建类的实例:
#include <iostream>
using namespace std;
void useClassInFunction1() {
    class Person {
    public:
        string name;
        int age;
    };
    Person person1;
    person1.name = "Alice";
    person1.age = 30;
    cout << "Name: " << person1.name << ", Age: " << person1.age << endl;
}
int main() {
    useClassInFunction1();
    return 0;
}
2. 在函数中定义类并创建多个类的实例:
#include <iostream>
using namespace std;
void useClassInFunction2() {
    class Car {
    public:
        string brand;
        string model;
    };
    Car car1, car2;
    car1.brand = "Toyota";
    car1.model = "Corolla";
    car2.brand = "Honda";
    car2.model = "Civic";
    cout << car1.brand << " " << car1.model << endl;
    cout << car2.brand << " " << car2.model << endl;
}
int main() {
    useClassInFunction2();
    return 0;
}
3. 在函数中定义类并使用构造函数初始化类的实例:
#include <iostream>
using namespace std;
void useClassInFunction3() {
    class Rectangle {
    public:
        int width;
        int height;
        Rectangle(int w, int h) : width(w), height(h) {}
    };
    Rectangle rect(10, 5);
    cout << "Width: " << rect.width << ", Height: " << rect.height << endl;
}
int main() {
    useClassInFunction3();
    return 0;
}
4. 在函数中定义类并使用成员函数:
#include <iostream>
using namespace std;
void useClassInFunction4() {
    class Circle {
    public:
        double radius;
        double area() {
            return 3.14 * radius * radius;
        }
    };
    Circle circle;
    circle.radius = 5.0;
    cout << "Area of the circle: " << circle.area() << endl;
}
int main() {
    useClassInFunction4();
    return 0;
}
5. 在函数中定义类并使用静态成员:
#include <iostream>
using namespace std;
void useClassInFunction5() {
    class BankAccount {
    public:
        static double interestRate;
    };
    BankAccount::interestRate = 0.05;
    cout << "Interest rate: " << BankAccount::interestRate << endl;
}
int main() {
    useClassInFunction5();
    return 0;
}
6. 在函数中定义类并使用友元函数:
#include <iostream>
using namespace std;
void useClassInFunction6() {
    class Rectangle {
    private:
        int width, height;
    public:
        Rectangle(int w, int h) : width(w), height(h) {}
        friend void printArea(Rectangle rect);
    };
    void printArea(Rectangle rect) {
        cout << "Area: " << rect.width * rect.height << endl;
    }
    Rectangle rectangle(4, 5);
    printArea(rectangle);
}
int main() {
    useClassInFunction6();
    return 0;
}
7. 在函数中定义类并使用模板:
#include <iostream>
using namespace std;
template <class T>
void useClassInFunction7() {
    class Box {
    public:
        T data;
    };
    Box<int> intBox;
    intBox.data = 10;
    cout << "Data in intBox: " << intBox.data << endl;
    Box<string> strBox;
    strBox.data = "Hello";
    cout << "Data in strBox: " << strBox.data << endl;
}
int main() {
    useClassInFunction7();
    return 0;
}
8.在函数中定义静态成员函数
#include <iostream>
using namespace std;
void load_system_driver() {
  class MatchFile {
  public:
    static std::string find() {
      const char* const searchPaths[] = {
#if defined(__LP64__)
	"/vendor/lib64/egl",
	"/system/lib64/egl"
#else
	"/vendor/lib/egl",
	"/system/lib/egl"
#endif
      };
      std::string retbuf;
      for (auto dir : searchPaths) {
	printf("xxx--------------->%s, %s(), line = %d, dir = %s\n",__FILE__,__FUNCTION__,__LINE__,dir);
	retbuf.assign(dir);
      }
      return retbuf;
    }
  };
  std::string absolutePath = MatchFile::find();
  printf("xxx--------------->%s, %s(), line = %d, abs_path = %s\n",__FILE__,__FUNCTION__,__LINE__,absolutePath.c_str());
}
int main(){
  load_system_driver();
}


















