.cpp vs .c [英] .cpp vs .c
问题描述
我有2个不同的文件,仅在文件类型上有所不同。一个是Text1.c,
其他是Text1.cpp。如果我有这个功能:
int * get_an_int(无效)
{
int t;
int * pt;
pt =(int *)malloc(sizeof * pt);
t = 41;
pt [0 ] = t;
返回pt;
}
然后一个将编译,而如果我删除演员,另一个赢了'' t。
包含一个C模块如果我要踩到它就不会有任何好处
有不同的规则。我怎么能绕过这个? furunculus
你的叔叔写道:我有2个不同的文件,只有文件类型不同。一个是Text1.c,
另一个是Text1.cpp。如果在其中我有这个功能:
int * get_an_int(void)
{
int t;
int * pt;
pt =(int * )malloc(sizeof * pt);
t = 41;
pt [0] = t;
返回pt;
}
然后一个会编译,而如果我删除了演员阵容,另一个人不会。
如果我要用不同的规则踩踏它,那么包含一个C模块并没有任何好处。我怎么能绕过这个? furunculus
你到底想要什么?该文件的名称是
无关紧要 - 重要的是调用哪个编译器(C或C ++,大概是
)。大多数编译器会从文件名中推断语言
,但也会提供一些机制来覆盖它。 (例如,使用gcc
你可以使用-x标志。)
你的叔叔写道:< blockquote class =post_quotes>我有两个不同的文件,只有文件类型不同。一个是Text1.c,
另一个是Text1.cpp。如果在其中我有这个功能:
int * get_an_int(void)
{
int t;
int * pt;
pt =(int * )malloc(sizeof * pt);
t = 41;
pt [0] = t;
返回pt;
}
然后一个会编译,而如果我删除了演员,另一个不会。
这句话对我来说有点令人费解。
移除演员并期待其他编译的动力是什么?我可能是错误的,但是如果内存服务的话,csat是必要的。
ma740988写道:你的叔叔写道:我有2个不同的文件,只有文件类型不同。一个是Text1.c,
另一个是Text1.cpp。如果在其中我有这个功能:
int * get_an_int(void)
{
int t;
int * pt;
pt =(int * )malloc(sizeof * pt);
t = 41;
pt [0] = t;
返回pt;
}
然后一个会编译,而如果我删除了演员,另一个不会。
这句话对我来说有点令人费解。
移除演员并期待其他编译的动力是什么?我可能会错误但是如果内存服务csat是必要的。
在C ++中,类型不允许来自void指针的隐式强制转换
安全。在C中,允许隐式演员。
干杯! --M
I have 2 different files that differ only in filetype. One is Text1.c , the
other is Text1.cpp . If therein I have this function:
int * get_an_int(void)
{
int t;
int * pt;
pt = (int *)malloc (sizeof*pt);
t = 41;
pt[0] = t;
return pt;
}
Then one will compile, whereas if I remove the cast, the other one won''t.
It doesn''t do any good to include a C module if I''m gonna stomp all over it
with different rules. How can I get around this? furunculus
Your Uncle wrote:I have 2 different files that differ only in filetype. One is Text1.c , the
other is Text1.cpp . If therein I have this function:
int * get_an_int(void)
{
int t;
int * pt;
pt = (int *)malloc (sizeof*pt);
t = 41;
pt[0] = t;
return pt;
}
Then one will compile, whereas if I remove the cast, the other one won''t.
It doesn''t do any good to include a C module if I''m gonna stomp all over it
with different rules. How can I get around this? furunculus
What exactly do you want to get around? The name of the file is
irrelevant-- what matters is which compiler is being invoked (C or C++,
presumably). Most compilers will infer the language from the filename
but will also offer some mechanism to override this. (E.g., with gcc
you can use the -x flag.)
Your Uncle wrote:I have 2 different files that differ only in filetype. One is Text1.c , the
other is Text1.cpp . If therein I have this function:
int * get_an_int(void)
{
int t;
int * pt;
pt = (int *)malloc (sizeof*pt);
t = 41;
pt[0] = t;
return pt;
}
Then one will compile, whereas if I remove the cast, the other one won''t.
This statement is somewhat puzzling to me. What''s the impetus behind
removing the cast and expecting the ''other'' to compile? I could be
mistaken but if memory serves the csat is necessary.
ma740988 wrote:Your Uncle wrote:I have 2 different files that differ only in filetype. One is Text1.c , the
other is Text1.cpp . If therein I have this function:
int * get_an_int(void)
{
int t;
int * pt;
pt = (int *)malloc (sizeof*pt);
t = 41;
pt[0] = t;
return pt;
}
Then one will compile, whereas if I remove the cast, the other one won''t.
This statement is somewhat puzzling to me. What''s the impetus behind
removing the cast and expecting the ''other'' to compile? I could be
mistaken but if memory serves the csat is necessary.
In C++, implicit casts from void pointers are not allowed for type
safety. In C, the implicit cast is allowed.
Cheers! --M
这篇关于.cpp vs .c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!