C++ 类的函数访问方式两种:

1).函数名(类的实例化名.函数名)

#include <iostream>
using namespace std;


class SimpleStat
{

public:
static void pri(int* aa){
    int i;
    for(i=0;i<*aa;i++) cout << "hello!\n";
}

};

int main() {
	cout  << "test\n";
	printf("ddsfds\n");
	// ii[12] = 'sdadas';
	SimpleStat statHelper;
	int ii=20;
	statHelper.pri(&ii);
	return 0;
}



2)::函数名(类::函数名)

::访问必须函数带static修饰(不带调用报without object错误);另外也不用单独调用类起别名

#include <iostream>
using namespace std;


class SimpleStat
{

public:
static void pri(int* aa){
    int i;
    for(i=0;i<*aa;i++) cout << "hello!\n";
}

};

int main() {
	cout  << "test\n";
	printf("ddsfds\n");
	// ii[12] = 'sdadas';
	int ii=20;
	SimpleStat::pri(&ii);
	return 0;
}





3)->函数名

#include <iostream>
using namespace std;


class SimpleStat
{

public:
static void pri(int* aa){
    int i;
    for(i=0;i<*aa;i++) cout << "hello!\n";
}

};

int main() {
	cout  << "test\n";
	printf("ddsfds\n");
	// ii[12] = 'sdadas';
	SimpleStat statHelper;
	SimpleStat *pst=&statHelper;
	int ii=20;
	pst->pri(&ii);
	return 0;
}




在这里插入图片描述

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐