C++ struct :: namespace

:: 类作用域操作符。“::”指明了成员函数所属的类

// c++使用struct 声明变量可以不用struct关键字  
//:: 类作用域操作符。“::”指明了成员函数所属的类
#include<iostream>
using namespace std;
namespace hk
{
	void print()
	{
		cout<<"hk"<<endl;
	}
}
struct hhk
{
	int a;
	void fun() //可以放函数成员
	{
		cout<<"kah"<<endl;
	}
};
//using namespace hk;
int main()
{
char a='k';
float num1,num2;
cout<<"hello hankai"<<endl;
cout<<a<<'\n';
hk::print();//
cin>>num1>>num2;
cout<<num1<<num2<<'\n';
hhk b;// c++使用struct 声明变量可以不用struct关键字
b.fun();
system("pause");
return 0;
}

C 结构体中加函数----使用指针

#include<stdio.h>
struct node
{
int a;
void (*p)();
};
void fun()
{
printf("printf");
}
void main()
{
	struct node a;
	a.p=fun;
	a.p();
	system("pause");
}
发布了35 篇原创文章 · 获赞 2 · 访问量 921

猜你喜欢

转载自blog.csdn.net/hk2121/article/details/103882242