看到过这样一段代码,请教高人,这样的代码有什么好处,又有什么弊端?

smallfool 2003-07-15 01:31:19
int n = 0;
struct A
{
int *pColor;
A()
{
pColor = &n;
}
};
我最近在浏览别人的代码,他的代码有大量类似的代码。我看不出来这样的代码有什么好处。请问高人给予指点一二:
...全文
45 63 打赏 收藏 转发到动态 举报
写回复
用AI写文章
63 条回复
切换为时间正序
请发表友善的回复…
发表回复
wbh0360 2003-08-24
  • 打赏
  • 举报
回复
linux原代码中也有这些
smallfool 2003-08-24
  • 打赏
  • 举报
回复
TO sevencat:惭愧,有好几年的程序代码历史了。
sevencat 2003-08-18
  • 打赏
  • 举报
回复
咕咙...不知道写过多少代码了,才有资格来评别人的代码。
smallfool 2003-08-18
  • 打赏
  • 举报
回复
呵呵,好几天不来,还是有人有话要说的,只可惜似乎也是公说公有理;婆说婆有道。

个人认为这样的代码不提倡,没什么价值。
ahao 2003-07-30
  • 打赏
  • 举报
回复
我个人认为,这样的代码极烂,这么写有多大好处??效率有多大提高??带来的问题却多得多,严重的多.
yakai 2003-07-30
  • 打赏
  • 举报
回复
struct A
{
static int n = 0;


int *pColor;
A()
{
pColor = &n;
}
};

应该说这样肯定比它的要好一些,全局变量前面带了一个静态成员变量

全局变量既然要用,就不如用自己而不用指向它的指针,除非很特殊的原因(看相关的代码,察觉其意图才可以),反正不是这个构造函数所看得出来的了。
hookuy 2003-07-30
  • 打赏
  • 举报
回复
如果用C++写的话,尽量用class体现封装性
用C的话,用struct体现数据结构清晰
smallfool 2003-07-30
  • 打赏
  • 举报
回复
好像很多人都离题了。
alan118 2003-07-28
  • 打赏
  • 举报
回复
to:466632586089(六月初六)
有什么不可以,在c++里面,struct 和class是一样的概念,只不过它的默认借口是public的
gernal_dn 2003-07-28
  • 打赏
  • 举报
回复
我觉得:如果程序中有大量类似的代码,目的应该与反跟踪有关,有可能是加密工具修改出来的代码。
sevencat 2003-07-28
  • 打赏
  • 举报
回复

static int nodata; /* if data points to this, then the element was actually deleted */


#ifdef USE_CHECK_ALLOC
extern t_list * list_create_real(char const * fn, unsigned int ln)
#else
extern t_list * list_create(void)
#endif
{
t_list * new;

#ifdef USE_CHECK_ALLOC
if (!(new = check_malloc_real(sizeof(t_list),fn,ln)))
#else
if (!(new = malloc(sizeof(t_list))))
#endif
{
eventlog(eventlog_level_error, __FUNCTION__, "could not allocate memory for new");
return NULL;
}

new->head = NULL;
new->tail = NULL;
new->len = 0;

return new;
}


extern int list_destroy(t_list * list)
{
if (!list)
{
eventlog(eventlog_level_error, __FUNCTION__, "got NULL list");
return -1;
}

list_purge(list);
if (list->head)
eventlog(eventlog_level_error, __FUNCTION__, "got non-empty list");

free(list);

return 0;
}


extern int list_purge(t_list * list)
{
t_elem * curr;
t_elem * head;
t_elem * tail;
t_elem * next;
t_elem * * change;

if (!list)
{
eventlog(eventlog_level_error, __FUNCTION__, "got NULL list");
return -1;
}

#ifdef LIST_DEBUG
list_check(list);
#endif

head = NULL;
tail = NULL;
change = NULL;
for (curr=list->head; curr; curr=next)
{
next = curr->next;
if (curr->data==&nodata)
{
if (change)
*change = next;
free(curr);
}
else
{
tail = curr;
if (!head)
head = curr;
change = &curr->next;
}
}

list->head = head;
list->tail = tail;

return 0;
}


#ifdef USE_CHECK_ALLOC
extern int list_append_data_real(t_list * list, void * data, char const * fn, unsigned int ln)
#else
extern int list_append_data(t_list * list, void * data)
#endif
{
t_elem * elem;

if (!list)
{
eventlog(eventlog_level_error, __FUNCTION__, "got NULL list");
return -1;
}

#ifdef USE_CHECK_ALLOC
if (!(elem = check_malloc_real(sizeof(t_elem),fn,ln)))
#else
if (!(elem = malloc(sizeof(t_elem))))
#endif
{
eventlog(eventlog_level_error, __FUNCTION__, "could not allocate memory for elem");
return -1;
}
elem->data = data;

elem->next = NULL;
if (!list->head)
list->head = elem;
if (list->tail)
list->tail->next = elem;
list->tail = elem;
list->len++;

return 0;
}


extern t_elem * list_get_elem_by_data(t_list const * list, void const * data)
{
t_elem * curr;

if (!list)
{
eventlog(eventlog_level_error, __FUNCTION__, "got NULL list");
return NULL;
}

LIST_TRAVERSE(list,curr)
if (curr->data==data)
return curr;

return NULL;
}


extern int list_remove_elem(t_list * list, t_elem * elem)
{
if (!list)
{
eventlog(eventlog_level_error, __FUNCTION__, "got NULL list");
return -1;
}
if (!elem)
{
eventlog(eventlog_level_error, __FUNCTION__, "got NULL elem");
return -1;
}
if (elem->data==&nodata)
{
eventlog(eventlog_level_error, __FUNCTION__, "got deleted elem");
return -1;
}

elem->data = &nodata;
list->len--;

return 0;
}


extern int list_remove_data(t_list * list, void const * data)
{
t_elem * elem;

if (!list)
{
eventlog(eventlog_level_error, __FUNCTION__, "got NULL list");
return -1;
}

if (!(elem = list_get_elem_by_data(list,data)))
return -1;

return list_remove_elem(list,elem);
}


extern int elem_set_data(t_elem * elem, void * data)
{
if (!elem)
{
eventlog(eventlog_level_error, __FUNCTION__, "got NULL elem");
return -1;
}
if (elem->data==&nodata)
{
eventlog(eventlog_level_error, __FUNCTION__, "got deleted elem");
return -1;
}
if (data==&nodata)
{
eventlog(eventlog_level_error, __FUNCTION__, "got bad data");
return -1;
}

elem->data = data;

return 0;
}


extern void * elem_get_data(t_elem const * elem)
{
if (!elem)
{
eventlog(eventlog_level_error, __FUNCTION__, "got NULL elem");
return NULL;
}
if (elem->data==&nodata)
{
eventlog(eventlog_level_error, __FUNCTION__, "got deleted elem");
return NULL;
}

return elem->data;
}


extern void * list_get_data_by_pos(t_list const * list, unsigned int pos)
{
t_elem const * curr;
unsigned int len;

len = 0;
LIST_TRAVERSE_CONST(list,curr)
if (len++==pos)
return curr->data;

eventlog(eventlog_level_error, __FUNCTION__, "requested position %u but len=%u",pos,len);
return NULL;
}


#ifdef LIST_DEBUG
extern t_elem * list_get_first_real(t_list const * list, char const * fn, unsigned int ln)
#else
extern t_elem * list_get_first(t_list const * list)
#endif
{
t_elem * curr;

if (!list)
{
#ifdef LIST_DEBUG
eventlog(eventlog_level_error, __FUNCTION__, "got NULL list from %s:%u",fn,ln);
#else
eventlog(eventlog_level_error, __FUNCTION__, "got NULL list");
#endif
return NULL;
}

for (curr=list->head; curr; curr=curr->next)
if (curr->data!=&nodata)
return curr;

return curr;
}


#ifdef LIST_DEBUG
extern t_elem const * list_get_first_const_real(t_list const * list, char const * fn, unsigned int ln)
#else
extern t_elem const * list_get_first_const(t_list const * list)
#endif
{
t_elem const * curr;

if (!list)
{
#ifdef LIST_DEBUG
eventlog(eventlog_level_error, __FUNCTION__, "got NULL list from %s:%u",fn,ln);
#else
eventlog(eventlog_level_error, __FUNCTION__, "got NULL list");
#endif
return NULL;
}

for (curr=list->head; curr; curr=curr->next)
if (curr->data!=&nodata)
return curr;

return curr;
}


extern t_elem * elem_get_next(t_elem const * elem)
{
t_elem * curr;

if (!elem)
{
eventlog(eventlog_level_error, __FUNCTION__, "got NULL elem");
return NULL;
}

for (curr=elem->next; curr; curr=curr->next)
if (curr->data!=&nodata)
return curr;

return curr;
}


extern t_elem const * elem_get_next_const(t_elem const * elem)
{
t_elem const * curr;

if (!elem)
{
eventlog(eventlog_level_error, __FUNCTION__, "got NULL elem");
return NULL;
}

for (curr=elem->next; curr; curr=curr->next)
if (curr->data!=&nodata)
return curr;

return curr;
}
smallfool 2003-07-25
  • 打赏
  • 举报
回复
2 TopCat:呵呵,是啊,我所抄来的代码就是出自这样一个自认为也是高人的高人的程序。
TopCat 2003-07-24
  • 打赏
  • 举报
回复
我也比较反对使用全局变量。

上面那个例子完全可以用类封装起来。

但是一些比较早入行的人可能比较习惯这种用法,我们可以不用,但是要习惯别人的用法。除非你有足够的时间去改写它。呵呵。
smallfool 2003-07-24
  • 打赏
  • 举报
回复
To RedSun:你的观点很不错。不过我感觉在我所浏览的代码中似乎不存在着大量重复对象的初始化问题。结论嘛,我也就不多说了。

To nilm:呵呵,你的火气似乎太大。呵呵,垃圾不垃圾,也是公说公有理,婆说婆有道。没有办法的事情。很多时候都是跟个人的风格有很大关系的。

另外我再给大家介绍一种风格,请大家给出看法、
static CArray<HWND, HWND> g_arrBars;
void AddWnd(HWND hToAdd)
{
g_arrBars.Add(hToAdd);
}

void Delete(int nIndex)
{
HWND hWnd = (HWND)g_arrBars[nIndex];
ASSERT(::IsWindow(hWnd));
DestoryWindow(hWnd);

}

....可能还有更多的对这个全局数组的操作函数。
首先说明一下:我这是演示,可能有些函数语句未必就正确。我想询问的是:这种以全局函数对全局变量的操作,这样的编程风格,大家有何看法。

我自己是不太欣赏。我对全局变量的使用是比较敏感的,尽量不使用。这样的方式更是不大喜欢。
carambo 2003-07-23
  • 打赏
  • 举报
回复
应该是在c++结构体中声明了一个私有构造函数来进行pColor的初始化,这个在《Thingking in c++》中有详细介绍。
nilm 2003-07-23
  • 打赏
  • 举报
回复
这样的代码值得讨论吗?简直是垃圾。
这么写用意何在?
为什麽要用全局变量?????!!!!
用静态就比它好。

局部性的原则时刻要牢记!
zijida 2003-07-23
  • 打赏
  • 举报
回复
赞同RedSunRS(RedSun) & njtu(天地不容)两位仁兄的说法.
int n 是一个全局变量.
struct A则定义了对这个全局变量的引用.这种做法本身来说有些多余.
可能原作者的本意是希望在调用n的时候有一个更清楚的表达.
因为n的名称是含义模糊的,不是一种很好的命名方式.
同时更重要的是,在上一个目的的基础上,还想方便的修改这个全局变量的所有引用.
与优化的关系....好像不太明显.

我也不太赞同这种方式的编码.要么用类,要么给n一个清晰的名字,直接调用.
njtu 2003-07-23
  • 打赏
  • 举报
回复
我认为 RedSunRS(RedSun)说的有道理:

好处在于这个了。
int n = 0;
struct A
{
int *pColor;
A()
{
pColor = &n;
}
};
n是一个全局变量。
如果此程序中有多个 struct A那么构造函数将其初始化为对全局变量的引用。你是否能做到牵一发而动全身呢?能的。如果程序中用到10个struct A,然而你在程序运行时修改了n值,那么所友struct *pColor指向值全部改变。也许不明白为什么在struct A中声明那个pColor?直接用全局变量,不是更好?那不符合OO的思想,而且如果改动了全局变量,那你可有的改了。另外,代码十分庞大以后,维护,调试,将因为这个全局变量变得困难。

作者也许想让所有的A用同一个pColor,又想符合OO的思想。
这样会不会更好一点:

struct A
{
static int color;
...
};
smallfool 2003-07-23
  • 打赏
  • 举报
回复
TO ckacka:我赞成你的话。冗余有时候是必须的。这种带有一些技巧性的东西我不太欣赏。
smallfool 2003-07-22
  • 打赏
  • 举报
回复
关注???
加载更多回复(43)

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧