让C代码看起来像C ++代码 [英] Having C code looking like C++ code

查看:55
本文介绍了让C代码看起来像C ++代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在求职面试中被问到如何使C代码看起来像C ++

代码,老实说我不知道​​是什么回答,因为我从来没有真正完成很多C.现在,我一直在网上搜索关于这个主题的网站

,但是我没有运气。有人能指点我这个主题的网站吗?非常感谢!

Hi,
I''ve been asked in a job interview how to make C code look like C++
code, and honestly I didn''t know what to answer because I have never really
done a lot of C. Now, I''ve been searching around the web about web sites
that talk about this subject, but I''ve had no luck. Can anyone point me to
some web site about this subject? Thanks a lot!

推荐答案

Fronsac写道:
Fronsac wrote:

我有在求职面试中被问到如何使C代码看起来像C ++代码,老实说我不知道​​该回答什么因为我从来没有真正做过很多C.现在,我我一直在网上搜索有关这个主题的网站
网站,但我没有运气。任何人都可以指向我这个主题的网站吗?非常感谢!
Hi,
I''ve been asked in a job interview how to make C code look like C++
code, and honestly I didn''t know what to answer because I have never
really done a lot of C. Now, I''ve been searching around the web about web
sites that talk about this subject, but I''ve had no luck. Can anyone point
me to some web site about this subject? Thanks a lot!




使C代码看起来像C ++通常要求它实际上是C ++。

为什么要尝试制作一种语言只看起来像另一个?没有

优势,除非您正在尝试IOCCC。



Making C code look like C++ generally requires making it actually be C++.
Why would you try to make one language only look like another? There''s no
advantage, unless you''re trying for the IOCCC.


在文章< AL ***** ****************@wagner.videotron.net>,

Fronsac< fr ************* @ hotmail.com>写道:
In article <AL*********************@wagner.videotron.net>,
Fronsac <fr*************@hotmail.com> wrote:

我在求职面试中被问到如何使C代码看起来像C ++代码,老实说我不知道​​该怎么做回答,因为我从来没有真正做过很多C.现在,我一直在网上搜索有关这个主题的网站
,但我没有运气。谁能指点我关于这个主题的一些网站?非常感谢!


答案#1:你为什么要这样?

答案#2:写错C代码

答案#2b :通过编写看起来像坏C ++代码的C代码

答案#3:通过写入C和C ++的公共子集(但参见#1和#2)

回答#4:你真正想要找出什么?


一种可能性是他们想要C ++ - 干净的C代码以便进行可能的升级

路径到C ++。对此的正确解决方案是使用C ++的`extern'C"''

来允许C和C ++代码链接在一起,并且有C(编译为

C)和C ++(编译为C ++)在同一程序中需要旧(C)

代码和新(C ++)代码时链接在一起。所有这一切需要

是使用的头文件是C-clean和C ++ - clean,这比所有代码的一般情况都要简单。
。 />
除非他们在讨论C ++使用的所有尖括号。

你可以使用< ;:和:>为此付出了代价。

dave


-

Dave Vandervies dj ****** @ csclub.uwaterloo.ca [S]直到理查德希思菲尔德不够好,但你能做什么呢,
Hi,
I''ve been asked in a job interview how to make C code look like C++
code, and honestly I didn''t know what to answer because I have never really
done a lot of C. Now, I''ve been searching around the web about web sites
that talk about this subject, but I''ve had no luck. Can anyone point me to
some web site about this subject? Thanks a lot!
Answer #1: Why would you want to?
Answer #2: By writing bad C code
Answer #2b: By writing C code that looks like bad C++ code
Answer #3: By writing in the common subset of C and C++ (but see #1 and #2)
Answer #4: What are you really trying to find out?

One possibility is that they want C++-clean C code for a possible upgrade
path to C++. The correct solution to this is to use C++''s `extern "C"''
to allow C and C++ code to be linked together, and have C (compiled as
C) and C++ (compiled as C++) linked together when they want old (C)
code and new (C++) code in the same program. All that this requires
is that the header files used be both C-clean and C++-clean, which is
rather easier than the general case of all code.
Unless they''re talking about all the angle brackets that C++ uses.
You might be able to get away with using the <: and :> digraphs for that.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca[S]till not good enough for Richard Heathfield, but what can you do, really?



你总是可以写只为聪明的人。在封面上。

- Ben Pfaff和Richard Heathfield在comp.lang.c


You could always write "only for clever people" on the cover.
--Ben Pfaff and Richard Heathfield in comp.lang.c


>使C代码看起来像C ++通常需要使它实际上是C ++。
> Making C code look like C++ generally requires making it actually be C++.
为什么你会尝试使一种语言看起来像另一种?没有
优势,除非您正在尝试IOCCC。
Why would you try to make one language only look like another? There''s no
advantage, unless you''re trying for the IOCCC.




在某些平台上并不总是可以访问C ++(想想游戏机就像

的老游戏男孩可能无法访问C ++编译器),所以你必须使用

C和一些技巧让它像C ++一样运行。从我收集的内容来看,远远没有用C语法来实现继承,尽管你可以用它来假装它。例如:


struct Foo {

int var1;

}


struct Bar {

struct Foo base;

int var2;

}


Bar * createBar (){

返回malloc(sizeof(Bar));

}


void foo_incVar1(Foo * foo){

foo-> var1 ++;

}


void bar_incVar2(Bar * bar){

bar-> var2 ++;

/ *虽然,你意识到如果我想访问var1,我必须做

bar-> base.var1,或者(Foo *)bar) - > var1)。

最让我烦恼的是你可以封装你的
数据。 * /

}


void main(){

Foo * foo =(Foo *)createBar();

foo_incVar1(foo); / *增量var1 * /

bar_incVar2((Bar *)foo); / *增量var2 * /

}


你可以使用函数指针模拟你的

"对象的虚拟方法;。



C++ is not always accessible on some platforms (think game consoles like the
old game boy might not have access to a C++ compiler), so you''ll have to use
C and some tricks to have it behave like C++. From what I''ve gathered so
far, it''s impossible with C syntax to achieve inheritence, though you can
fake it. For example :

struct Foo {
int var1;
}

struct Bar {
struct Foo base;
int var2;
}

Bar* createBar() {
return malloc( sizeof(Bar) );
}

void foo_incVar1( Foo* foo ) {
foo->var1++;
}

void bar_incVar2( Bar* bar ) {
bar->var2++;
/* though, you realize that if I want to access var1, I have to do
bar->base.var1, or ((Foo*)bar)->var1).
The thing that bugs me most though is that you can encapsulate your
data. */
}

void main() {
Foo* foo = (Foo*) createBar();
foo_incVar1( foo ); /* increments var1 */
bar_incVar2( (Bar*) foo ); /* increments var2 */
}

And you can use function pointers to simulate virtual methods for your
"objects".


这篇关于让C代码看起来像C ++代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆