亲宝软件园·资讯

展开

c++类成员函数如何做函数参数

程序鸡 人气:0

类成员函数做函数参数

类内部的typedef函数声明,属于类成员,在类外声明时必须加类声明作用域(Test::FUNC),且赋值的函数必须为类成员(&Test::Func1)

下面的类中,Func1和Func2的返回值类型和参数列表都一样,定义的FUNC类似委托类型

Test.h

#pragma once
#include<iostream>
using namespace std;
class Test
{
public:
	typedef void (Test::*FUNC)(int);
	Test();
	~Test();
	void Func(FUNC f, int x);
	void Func1(int x);
	void Func2(int x);
};

Test.cpp

#include "Test.h"


Test::Test()
{
}


Test::~Test()
{
}

void Test::Func(FUNC f, int x)
{
	(this->*f)(x);
}

void Test::Func1(int x)
{
	cout << "Func1:" << x<<endl;
}

void Test::Func2(int x)
{
	cout << "Func2:" << x << endl;;
}

源.cpp

#include"Test.h"
int main()
{
	Test test;
	test.Func(&Test::Func1, 1);
	test.Func(&Test::Func2, 2);

	system("pause");
}

运行结果

在这里插入图片描述

C++ string类常见成员函数

在使用C++string相关操作时必须包含头文件#include<string>

1.string构造函数

例:

2.遍历访问

通过操作符 s[i] 以及at(i)可直接访问第i个字符,区别时at会检查越界问题。

3.容量大小相关操作

4.赋值操作

5.字符串拼接

字符串+字符串,或字符+字符串,或字符串+字符都可以直接拼接。

6.字符串比较

7.字符串查找函数

8.替换函数

9.插入函数

10.删除函数

11.子串

12.字符串交换

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论