楼主: xiaoye

java心得,2016年1月20日10:42:23再次修正,

[复制链接]
 楼主| 发表于 2016-1-17 00:45:45 | 显示全部楼层
Antergone 发表于 2016-1-16 22:26
额,楼主看了你的代码,我要以一个程序员的角度来告诉你几个问题。
1.C语言 不需要方法申明也可以实现。但 ...

结构体里面不能写函数,通过函数内嵌结构体,这样相当将上面的两个变量变相定义了,求思路
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-1-17 00:46:17 | 显示全部楼层
xiaoye 发表于 2016-1-17 00:33
楼上也说了,可以通过结构体实现类,我去,早知道用结构体写好了,泪奔。这样更加接近类
...

这方法貌似行不通
回复 支持 反对

使用道具 举报

发表于 2016-1-17 01:17:40 | 显示全部楼层
[AppleScript] 纯文本查看 复制代码
#include "stdafx.h"

struct OPPClass{

	int a;
	int b;
	int rootB(int a, int b);
};

int OPPClass::rootB(int a, int b)
{
	return a + b;
}

int _tmain(int argc, _TCHAR* argv[])
{
	OPPClass *pObj = new OPPClass;
	printf("result = %d\n", pObj->rootB(1, 2));
	delete pObj;
	return 0;
}

点评

你这个不是C吧,这么写是C++的写法了  详情 回复 发表于 2016-1-17 03:58
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-1-17 01:30:17 | 显示全部楼层
冰琥珀 发表于 2016-1-17 01:17
[mw_shl_code=applescript,true]#include "stdafx.h"

struct OPPClass{

明天试试。明天我改改

点评

函数指针也能做到啊 [mw_shl_code=c,true]#include "stdio.h" struct TEST { int x,y; int (*func)(int,int); }; int add2(int x,int y) { return x+y; } int main(int argc, char const *argv[]) { struct TE  详情 回复 发表于 2016-1-17 03:53
回复 支持 反对

使用道具 举报

发表于 2016-1-17 03:50:56 | 显示全部楼层
冰琥珀 发表于 2016-1-17 01:17
[mw_shl_code=applescript,true]#include "stdafx.h"

struct OPPClass{

这是 vc吧? _tmain 我记得是个宏
回复 支持 反对

使用道具 举报

发表于 2016-1-17 03:53:35 | 显示全部楼层
本帖最后由 Antergone 于 2016-1-17 03:58 编辑
xiaoye 发表于 2016-1-17 01:30
明天试试。明天我改改


函数指针也能做到啊
[C] 纯文本查看 复制代码
#include "stdio.h"
struct TEST
{
	int x,y;
	int (*func)(int,int); 
};
int add(int x,int y)
{
	return x+y;
}
int main(int argc, char const *argv[])
{
	struct TEST test;
	test.func=&add;
	printf("func(1,2)=%d",test.func(1,2));
	return 0;
}
回复 支持 反对

使用道具 举报

发表于 2016-1-17 03:58:04 | 显示全部楼层
冰琥珀 发表于 2016-1-17 01:17
[mw_shl_code=applescript,true]#include "stdafx.h"

struct OPPClass{

你这个不是C吧,这么写是C++的写法了

点评

只是用了个new符号而已,你可以改为OPPClass obj,然后下面的调用改为obj.rootB()。当然了,函数指针也是一种方式  详情 回复 发表于 2016-1-17 10:23
回复 支持 反对

使用道具 举报

发表于 2016-1-17 10:23:39 | 显示全部楼层
本帖最后由 冰琥珀 于 2016-1-17 10:38 编辑
Antergone 发表于 2016-1-17 03:58
你这个不是C吧,这么写是C++的写法了


在VS下编译的,应该那玩意兼容C++了,在linux下用gcc编译不通过,不过函数指针是可行的
选区_022.png
选区_023.png
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-1-17 11:09:13 | 显示全部楼层
Antergone 发表于 2016-1-17 03:58
你这个不是C吧,这么写是C++的写法了

c++的
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-1-17 11:10:48 | 显示全部楼层
Antergone 发表于 2016-1-17 03:53
函数指针也能做到啊
[mw_shl_code=c,true]#include "stdio.h"
struct TEST

厉害厉害,涨知识了
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

指导单位

江苏省公安厅

江苏省通信管理局

浙江省台州刑侦支队

DEFCON GROUP 86025

旗下站点

邮箱系统

应急响应中心

红盟安全

联系我们

官方QQ群:112851260

官方邮箱:security#ihonker.org(#改成@)

官方核心成员

Archiver|手机版|小黑屋| ( 沪ICP备2021026908号 )

GMT+8, 2025-3-7 02:53 , Processed in 0.028332 second(s), 10 queries , Gzip On, MemCache On.

Powered by ihonker.com

Copyright © 2015-现在.

  • 返回顶部