可以接受混合C / C ++? [英] acceptable to mix C/C++?

查看:48
本文介绍了可以接受混合C / C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C / C ++学习期间一直在反弹,并找到了我喜欢两种语言的部分的b $ b ...我发现指针算术

和C中的数组对我来说非常自然,但我无法通过

事实上C结构甚至不能与C ++类比较(没有

构造函数,没有成员函数,当然没有继承),还有

没有函数重载,而且好的GUI工具(和其他库)

似乎是全部用于C ++。但我真的不想转向C ++和

使用iostream而不是stdio,或者使用向量和字符串

数组和char *'s ,(虽然我确实发现模板/通用

编程很有趣,我已经决定学习它,好像它是另一种语言并且因为使用数组而对待它这个问题可能会让自己失败。


所以我的问题:比如说有一个char *类成员可以接受吗? br />
而不是字符串?使用MyClass *代替向量< MyClass>?

使用从其他结构派生的结构编写C?在一个const中潜入

而不是在C编程中使用#define?

对于大型程序来说,混合两者会是一件坏事吗?

事实是现代的c ++编译器似乎并不关心我是否混合和

匹配,所以它很诱人...

谢谢,有见地的建议将不胜感激

lin

解决方案

" Lindon" <李************ @ yahoo.com>写了...

我在C / C ++学习期间一直在反弹,并且发现我喜欢这两种语言的部分...我发现指针算术

所以我的问题:比如说有一个char *类成员而不是字符串是否可以接受?使用MyClass *而不是矢量< MyClass>?


当然。为什么你认为它不是?

用其他结构衍生的结构写C?


不知道你在这里的意思。

在一个const中而不是#define中潜入一个C编程?


这应该是一个规则。

并且将两者混合对于大型程序来说是一件坏事吗?


不,将两者混合没有任何固有的错误。

事实上现代的c ++编译器似乎并不关心我是否混合使用
匹配所以它很诱人...




我对C99不太熟练,但C90没有什么不可思议的提供

我会说服_me_,我的应用程序的某些部分需要用C语言写成
,对你完全诚实。

只用一种语言处理就简单得多了。当然,如果一个C99大师出现并且

告诉我如何在某些方面C更好(我们有一个编译器

处理它),我' 改变主意。


Victor


你好,


开2003年7月2日星期三12:56:46 -0700,Lindon写道:

我在C / C ++学习期间一直在反弹并找到了
我喜欢这两种语言的一部分...我发现指针算术和C中的数组对我来说非常自然,但是我不能通过这样的事实:C结构甚至都不是比较C ++类(没有
构造函数,没有成员函数,当然没有继承),没有函数重载,而且好的GUI工具(和其他库)似乎是适用于C ++。但我真的不想转向C ++,而是使用iostream而不是stdio,或者使用矢量和字符串代替数组和char *,(尽管我找到了模板/通用的编程很有意思,我已经决定把它当作另一种语言学习它并将其视为这样,因为使用数组和低级别的C级将会自我挫败。


如果你不想搬到iostream,那么根本就不要。您仍然可以使用stdio
,但使用#include< cstdio> (最新的ANSI C ++ - 我猜不是

所有编译器都支持它,所以请注意cstdio标题可能不存在
)。

所以我的问题:比如说有一个char *类成员而不是字符串是否可以接受?使用MyClass *而不是矢量< MyClass>?


使用char *而不是字符串是完全可以接受的。

无论如何,我不会这样做,因为std当我们使用字符串时,:: string类比char *更容易使用

。 char *对于指针

算术运算更有用。就矢量而言,我不能告诉你

关于它的任何事情,因为我不知道那个保留字。

用结构写C来自其他结构?在一个const中而不是在一个C编程的#define中偷偷摸摸?
并且将两者混合对于大型程序来说是一件坏事吗?


当每个人需要时,你应该使用两个类和结构。

需要。 struct可以是类的setDate函数的参数,例如

。我们只是不直接设置,使用普通归因来过滤

一个月的天数和月份数。例如。


它可以同时使用#define和const,但要记住

#defined" constants"将在

编译期间被其实际价值所取代。我的意思是:


#define PI 3.14

if(PI == 3.14)blah


如果相同如下:


if(3.14 == 3.14)blah


编译程序后(你可以把它想象成之后)如果你知道我的意思,那么

转换为汇编。


现在const。 const在内存中的.data部分保留空间(

#defined值也是如此,但字符串也是如此)。无论如何,我通常使用const

当我想做一些像使用char *作为参数而没有

来改变它。谷歌更多关于const,因为我猜这不是
准确的信息。

事实上现代的c ++编译器似乎并不关心我混合和
匹配,所以它很诱人...
谢谢,有见地的建议将不胜感激




问候

(抱歉长文)


2003年7月2日星期三11:56:46 -0700,Lindon写道:
< blockquote class =post_quotes>我在C / C ++学习期间一直在反弹,并且发现我喜欢这两种语言的一部分......




[snip]


C ++在comp.lang.c中是偏离主题的 - 我们这里只讨论纯C。跟进

设定。


-

Freenet发行不可用

我经常后悔我演讲,绝不是我的沉默。

- Publilius Syrus


I''ve been bouncing back between my studies in C/C++ and have found
that I like parts of both languages... I find that pointer arithmetics
and arrays in C come extremely naturally to me, yet I can''t get passed
the fact that C structures don''t even compare to C++ classes (no
constructors, no member functions, of course no inheritance), there''s
no function overloading, and the good GUI tools (and other library)
seem to be all for C++. But I don''t really want to move to C++ and
use the iostreams instead of the stdio, or vectors and strings instead
of arrays and char*''s, (although I do find template/generic
programming interesting, I''ve decided to learn it as if it were
another language and treat it as such because using arrays and low
level C with it would be self defeating).

So my quesiton: is it acceptable to, say, have a char* class member
instead of a string? using MyClass* instead of a vector<MyClass>?
Writing C with structures that derive from other structures? Sneaking
in a const instead of a #define in a C prog?
And would mixing the two be a bad thing for larger programs?
The fact is that modern c++ compilers don''t seem to care if I mix and
match so its rather tempting...
thanks, insightful advice would be appreciated
lin

解决方案

"Lindon" <li************@yahoo.com> wrote...

I''ve been bouncing back between my studies in C/C++ and have found
that I like parts of both languages... I find that pointer arithmetics
and arrays in C come extremely naturally to me, yet I can''t get passed
the fact that C structures don''t even compare to C++ classes (no
constructors, no member functions, of course no inheritance), there''s
no function overloading, and the good GUI tools (and other library)
seem to be all for C++. But I don''t really want to move to C++ and
use the iostreams instead of the stdio, or vectors and strings instead
of arrays and char*''s, (although I do find template/generic
programming interesting, I''ve decided to learn it as if it were
another language and treat it as such because using arrays and low
level C with it would be self defeating).

So my quesiton: is it acceptable to, say, have a char* class member
instead of a string? using MyClass* instead of a vector<MyClass>?
Sure. Why would you think that it''s not?
Writing C with structures that derive from other structures?
Not sure what you mean here.
Sneaking
in a const instead of a #define in a C prog?
That should be a rule.
And would mixing the two be a bad thing for larger programs?
No, there is nothing inherently wrong with mixing the two.
The fact is that modern c++ compilers don''t seem to care if I mix and
match so its rather tempting...



I am not so proficient in C99, but C90 has nothing incredible to offer
that would convince _me_ that some part of my application needs to be
written in C, to be entirely honest with you. It''s much simpler to
deal only with one language. Of course, if a C99 guru comes along and
shows me how in certain aspects C is better (and we have a compiler
that deals with it well), I''d change my mind.

Victor


Hi there,

On Wed, 02 Jul 2003 12:56:46 -0700, Lindon wrote:

I''ve been bouncing back between my studies in C/C++ and have found
that I like parts of both languages... I find that pointer arithmetics
and arrays in C come extremely naturally to me, yet I can''t get passed
the fact that C structures don''t even compare to C++ classes (no
constructors, no member functions, of course no inheritance), there''s
no function overloading, and the good GUI tools (and other library)
seem to be all for C++. But I don''t really want to move to C++ and
use the iostreams instead of the stdio, or vectors and strings instead
of arrays and char*''s, (although I do find template/generic
programming interesting, I''ve decided to learn it as if it were
another language and treat it as such because using arrays and low
level C with it would be self defeating).
If you don''t want to move to iostream, then simply don''t. You can still
use stdio, but use an #include <cstdio> (the latest ANSI C++ - I guess not
all compilers support it yet, so be warned that cstdio header may not
exist).
So my quesiton: is it acceptable to, say, have a char* class member
instead of a string? using MyClass* instead of a vector<MyClass>?
It''s perfectly acceptable to use have a char * instead of a string.
Anyway, I wouldn''t do that, since the std::string class is easier to use
than char * when we''re using strings. char * is more useful for pointer
arithmetic operations. As far as the vector is concerned, I can''t tell you
anything about it, since I didn''t know that reserved word.
Writing C with structures that derive from other structures? Sneaking
in a const instead of a #define in a C prog?
And would mixing the two be a bad thing for larger programs?
Your are supposed to use both classes and structures when each one is
need. A struct can be a parameter to a setDate function of a class, for
example. We just don''t set it directly, using normal attribution to filter
the number of days of a month and the month number for example.

It''s all right to use both #define and const, but keep in mind that
#defined "constants" will be replaced by their real value during
compiling. What I mean is that:

#define PI 3.14
if (PI == 3.14) blah

if the same as:

if (3.14 == 3.14) blah

after the program is compiled (you can think of it as after being
"converted to assembly", if you know what I mean).

Now const. const reserves space in the .data section in memory (the
#defined values too, but so does a string). Anyway, I usually use const
when I want to do something like using a char* as a parameter without
altering it. Google a bit more about const, since I guess this isn''t
accurate info.
The fact is that modern c++ compilers don''t seem to care if I mix and
match so its rather tempting...
thanks, insightful advice would be appreciated
lin



Regards
(and sorry for the long text)


On Wed, 02 Jul 2003 11:56:46 -0700, Lindon wrote:

I''ve been bouncing back between my studies in C/C++ and have found
that I like parts of both languages...



[snip]

C++ is off-topic in comp.lang.c - we only discuss pure C here. Followups
set.

--
Freenet distribution not available
I have often regretted my speech, never my silence.
-- Publilius Syrus


这篇关于可以接受混合C / C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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