I know that C++ has the concept of objects but C doesn't. I also know that pretty much all there is to know about C fits into K & R but the C++ library is vastly more complex. There have got to be other big differences though.
我知道c++有对象的概念,但C没有。我也知道,几乎所有关于C的知识都适合于K & R,但是c++库要复杂得多。但也有其他巨大的差异。
What are the major differences between C and C++?
C和c++的主要区别是什么?
11 个解决方案
#1
23
Check out Stroustrup's FAQ here, specifically:
查看Stroustrup的FAQ,具体如下:
What is the difference between C and C++?
C和c++的区别是什么?
C++ is a direct descendant of C that retains almost all of C as a subset. C++ provides stronger type checking than C and directly supports a wider range of programming styles than C. C++ is "a better C" in the sense that it supports the styles of programming done using C with better type checking and more notational support (without loss of efficiency). In the same sense, ANSI C is a better C than K&R C. In addition, C++ supports data abstraction, object-oriented programming, and generic programming (see The C++ Programming Language (3rd Edition)"; Appendix B discussing compatibility issues is available for downloading).
c++是C的直系后代,它保留了几乎所有C作为子集。c++提供了比C更强的类型检查,并且直接支持比C c++更广泛的编程风格,因为它支持使用C编程的编程风格,更好的类型检查和更多的notational支持(不损失效率)。在同样的意义上,ANSI C比K&R C更好,c++支持数据抽象、面向对象编程和泛型编程(参见c++编程语言(第3版));附录B讨论兼容性问题可供下载。
#2
24
The C++ language says they are the same:
c++语言说它们是一样的:
int C = 0;
assert(C++ == C);
#3
15
I think you answered your own question: Classes
我想你回答了你自己的问题:课堂。
Its a completely different design paradigm. I think the confusion comes because many people develop C++ programs that are basically C programs and don't even realize it.
这是一个完全不同的设计模式。我认为这种困惑是因为很多人开发了c++程序,这些程序基本上都是C程序,甚至都没有意识到。
Which allows, in the word of its creator, Stroustrup:
这允许,在它的创造者的话语中,Stroustrup:
- data abstraction
- 数据抽象
- object-oriented programming
- 面向对象编程
- generic programming.
- 泛型编程。
Edit: added some other fun stuff
编辑:添加一些其他有趣的东西。
#4
7
In short, C aspires to be a "portable assembler language". It keeps things simple, lets you do things that map almost directly to the underlying hardware, and doesn't present a lot of high level abstractions (you've got functions and.... that's about it)
简而言之,C立志成为一种“便携式汇编语言”。它使事情变得简单,让你几乎直接映射到底层硬件的事情,并且不存在大量的高水平的抽象(你有功能和....这是关于它的)
C++ tries to be everything. A high level language, a low level language, an object oriented language, a multi-paradigm language, a systems programming language, an embedded programming language, and an application development language.
c++试图成为一切。高级语言、低级语言、面向对象语言、多范式语言、系统编程语言、嵌入式编程语言和应用程序开发语言。
The two language really don't have much in common, other than some shared syntax. A C program might compile as C++ with only minor changes, but it'll have nothing in common with "proper" C++ code written for the language.
这两种语言实际上没有多少共同之处,除了一些共享的语法。一个C程序可以编译成c++,只会有一些细微的变化,但是它与为语言编写的“适当的”c++代码没有任何共同之处。
#5
7
Here's a website showing the "incompatibilities" between c and c++:
这是一个显示c和c++之间“不兼容”的网站:
http://david.tribble.com/text/cdiffs.htm#C++-vs-C
http://david.tribble.com/text/cdiffs.htm c++ -vs-C
There are actually quite a few areas where c and c++ diverge (in addition to the classes, template, exceptions, etc).
实际上,在一些领域中,c和c++存在差异(除了类、模板、异常等)。
As far as major differences, here's a list which covers it well:
至于主要的区别,这里列出了一个很好的清单:
- anonymous unions
- 匿名的工会
- classes
- 类
- constructors and destructors
- 构造函数和析构函数
- exceptions and try/catch blocks
- 异常和try / catch块
- external function linkages (e.g., extern "C")
- 外部函数联系(例如,外部“C”)
- function overloading
- 函数重载
- member functions
- 成员函数
- namespaces
- 名称空间
- new and delete operators and functions
- 新建和删除操作符和函数。
- operator overloading
- 操作符重载
- reference types
- 引用类型
- standard template library (STL)
- 标准模板库(STL)
- template classes
- 模板类
- template functions
- 模板函数
#6
4
C++ is mainly an extension of C. Originally C++ was called “C with classes”, highlighting the main original language extension. Already at that time overloading of functions was supported. Since then C++ has acquired exceptions, hierarchical namespaces, generic programming in the form of templates, and lastly multi-threading support. As of C++11 there’s also some minimal core language support for garbage collection using e.g. the Boehm garbage collector. Smaller extensions include reference types, trailing function return types syntax, memory allocation and deallocation machinery that can be overridden, run time type information, and more.
c++主要是C语言的扩展,最初c++被称为“类”,突出了主要的原始语言扩展。在那时,已经支持重载函数。从那时起,c++已经获得了异常、分级名称空间、模板形式的泛型编程,以及最后的多线程支持。在c++ 11中,也有一些用于垃圾收集的最小核心语言支持,例如Boehm垃圾收集器。较小的扩展包括引用类型、拖尾函数返回类型语法、可被覆盖的内存分配和deallocation机制、运行时类型信息等等。
The differences between C and C++, i.e. between C and the “C subset” of C++, are summarized in the C++11 standard's appendix C, entitled “Compatibility”, which is about 20 crammed pages, where the C1 section that pertains to C compatibility is about 10 pages.
C和c++之间的差异,即C和c++的“C子集”之间的差异,在c++ 11标准的附录C中被总结为“兼容性”,这大约是20个填充页面,其中与C兼容的C1部分大约是10页。
It was appendix C also in C++98 and C++03, and it’s appendix C also in the fresh new C++14 standard.
它是附录C在c++ 98和c++ 03中,它的附录C也在新的c++ 14标准。
As of C++11 appendix C of that standard lists the following incompatibilities between C and C++:
在该标准的c++ 11附录C中,C和c++之间的不兼容性如下:
§C1.1 Lexical conventions
New keywords (§2.12)
新的关键字(§2.12)
The paragraph only refers to C++11 §2.12. The following list was generated from that table and a corresponding table in C99.
段仅指c++ 11§2.12。下面的列表是由该表和C99中相应的表生成的。
C99 C++11 _Bool _Complex _Imaginary alignas alignof asm auto auto bool break break case case catch char char char16_t char32_t class const const const_cast constexpr continue continue decltype default default delete do do double double dynamic_cast else else enum enum explicit export extern extern false float float for for friend goto goto if if inline inline int int long long mutable namespace new noexcept nullptr operator private protected public register register reinterpret_cast restrict return return short short signed signed sizeof sizeof static static static_assert static_cast struct struct switch switch template this thread_local throw true try typedef typedef typeid typename union union unsigned unsigned using virtual void void volatile volatile wchar_t while while
Type of character literal is changed from int
to char
(§2.14.3)
类型的字符文字改变从int、char(§2.14.3)
String literals made const
(§2.14.5)
字符串常量(§2.14.5)
§C1.2 Basic concepts
C++ does not have “tentative definitions” (§3.1).
c++没有“暂定定义”(3.1)。
A struct
is a scope in C ++ , not in C (§3.3).
结构是一个范围在c++中,不是在C(§3.3)。
A name of file scope that is explicitly declared const, and not explicitly declared extern
, has internal linkage, while in C it would have external linkage ($3.5).
文件范围的名称是显式声明的const,而不是显式声明的外观,具有内部链接,而在C中,它将具有外部链接($3.5)。
main
cannot be called recursively and cannot have its address taken (§3.6).
main不能被递归地调用,不能使用它的地址(3.6)。
C allows “compatible types” in several places, C++ does not (§3.9).
在一些地方,C允许“兼容类型”,c++不允许(3.9)。
§C1.3 Standard conversions.
Converting void*
to a pointer-to-object type requires casting (§4.10).
转换void * pointer-to-object类型需要铸造(§4.10)。
Only pointers to non-const
and non-volatile objects may be implicitly converted to void*
(§4.10).
只能暗中non-const和非易失性对象的指针转换为void *(§4.10)。
§C1.4 Expressions.
Implicit declaration of functions is not allowed (§5.2.2).
隐式声明的函数是不允许(§5.2.2)。
Types must be declared in declarations, not in expressions (§5.3.3, §5.4).
类型必须是声明中声明,而不是表达式(§5.3.3,§5.4)。
The result of a conditional expression, an assignment expression, or a comma expression may be an lvalue (§5.16, §5.17, §5.18).
一个条件表达式的结果,一个赋值表达式,或一个逗号表达式可能是一个左值(§5.16,§5.17,§5.16)。
§C1.5 Statements.
It is now invalid to jump past a declaration with explicit or implicit initializer (except across entire block not entered) (§6.4.2, §6.6.4)
现在是无效的跳过去的声明与显式或隐式初始化(整个块不进入除外)(§6.4.2,§6.6.4)
It is now invalid to return (explicitly or implicitly) from a function which is declared to return a value without actually returning a value (§6.6.3).
从一个函数中返回(显式或隐式)返回一个值而不返回值(6.6.3),这是无效的。
§C1.6 Declarations.
In C++, the static
or extern
specifiers can only be applied to names of objects or functions. Using these specifiers with type declarations is illegal in C++ (§7.1.1).
在c++中,静态或外部指示符只能应用于对象或函数的名称。使用这些说明符与c++类型声明是非法的(安装7.1.1§)。
A C++ typedef
name must be different from any class type name declared in the same scope (except if the typedef
is a synonym of the class name with the same name) (§7.1.3).
c++类型名称必须不同于任何类中声明的类型名称相同的范围(除了如果typedef是同义词的具有相同名称的类名称)(§7.1.3)。
const
objects must be initialized in C++ but can be left uninitialized in C (§7.1.6).
常量对象必须初始化在c++中,但可以离开未初始化在C(§7.1.6)。
No implicit int
in C++ (§7.1.6).
没有隐含的int在c++中(§7.1.6)。
The keyword auto
cannot be used as a storage class specifier (§7.1.6.4).
汽车不能用作存储类关键字说明符(§7.1.6.4)。
C++ objects of enumeration type can only be assigned values of the same enumeration type (§7.2).
c++对象的枚举类型只能被分配相同的枚举类型的值(§7.2)。
In C++ , the type of an enumerator is its enumeration (§7.2).
在c++中,枚举器的类型是其枚举(§7.2)。
§C1.7 Declarators.
In C++, a function declared with an empty parameter list takes no arguments ($8.3.5).
在c++中,使用空参数列表声明的函数没有参数($8.3.5)。
In C++, types may not be defined in return or parameter types (§8.3.5, §5.3.3).
在c++中,类型可能不被定义作为回报或参数类型(§8.3.5,§5.3.3)。
In C++, the syntax for function definition excludes the “old-style” C function (§8.4).
在c++中,函数的语法定义排除了“老式的”C函数(§8.4)。
In C++ , when initializing an array of character with a string, the number of characters in the string (including the terminating '\0'
) must not exceed the number of elements in the array (§8.5.2).
在c++中,当初始化一个字符的字符串数组,字符串中的字符数(包括终止\ 0)不得超过数组中元素的个数(§8.5.2)。
§C1.8 Classes.
In C++, a class declaration introduces the class name into the scope where it is declared and hides any object, function or other declaration of that name in an enclosing scope (§9.1, §7.1.3).
在c++中,类声明将类名引入到它被声明的范围内,并在封闭范围内隐藏该名称的任何对象、函数或其他声明(9.1,7.1.3)。
In C++, the name of a nested class is local to its enclosing class (§9.7).
在c++中,嵌套类的名称是本地包含它的类(§9.7)。
In C++, a typedef
name may not be redeclared in a class definition after being used in that definition (§9.9).
在c++中,类型名称后不得重新定义在类定义中使用这个定义(§9.9)。
§C1.9 Special member functions.
The implicitly-declared copy constructor and implicitly-declared copy assignment operator cannot make a copy of a volatile
lvalue (§12.8).
隐式声明的复制构造函数和隐式声明的复制赋值操作符不能复制volatile lvalue(12.8)。
§C1.10 Preprocessing directives.
Whether __STDC__
is defined and if so, what its value is, are implementation-defined (§16.8).
__STDC__是否定义,如果是这样,它的价值是什么,是实现定义(§16.8)。
To ease the regeneration of the comparative keyword table, and to build confidence in the one given, here’s the C++ program used to generate it:
为了简化比较关键字表的更新,并在给定的情况下建立信任,这里是用于生成它的c++程序:
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
#define ITEMS_OF( c ) c.begin(), c.end()
enum class Language { c, cpp };
auto compare( Language const a, Language const b )
-> int
{ return int( a ) - int( b ); }
struct Keyword
{
string word;
Language language;
friend
auto operator<( Keyword const& a, Keyword const& b )
-> bool
{
if( int const r = a.word.compare( b.word ) ) { return (r < 0); }
return (compare( a.language, b.language ) < 0);
}
};
void add_words_from(
string const& filename, Language const language, vector< Keyword >& words
)
{
ifstream f( filename );
string word;
while( getline( f, word ) )
{
words.push_back( Keyword{word, language} );
}
}
auto main() -> int
{
vector< Keyword > words;
add_words_from( "kwc.txt", Language::c, words );
add_words_from( "kwcpp.txt", Language::cpp, words );
sort( ITEMS_OF( words ) );
int const w = 20;
int previous_column = -1;
string previous_word = "";
cout << left;
for( Keyword const& kw : words )
{
int const column = (int) kw.language;
int const column_advance = column - (previous_column + 1);
if( column_advance < 0 || previous_word != kw.word )
{
cout << endl;
if( column > 0 ) { cout << setw( w*column ) << ""; }
}
else
{
cout << setw( w*column_advance ) << "";
}
cout << setw( w ) << kw.word;
previous_column = column; previous_word = kw.word;
}
cout << endl;
}
#7
3
Templates is another big difference (in addition to classes/objects). Templates enable for example typesafe generic container types (their first use-case) and (bastardized) lambda-expressions (boost::lambda
).
模板是另一个大的区别(除了类/对象)。模板支持范例类型的泛型容器类型(它们的第一个用例)和(bastardized) lambda-表达式(boost::lambda)。
C++ is a much bigger language (not just library) than C.
c++是一种比C语言更大的语言(不仅仅是库)。
#8
3
This question doesn't have short answer.
In general C++ supports:
- OOP paradigm;
- generic programminng;
- template methaprogramming;
- Abstract data types;
- New libraries and standard supports elemnts of functional paradigm;
- other tools for make your program most supportable;
- Also you could write programs on C-like style but use C++ compiller;
But pure C - a little faster than C++ and more low level.
这个问题没有简单的答案。一般c++支持:- OOP范例;——通用programminng;-模板methaprogramming;——抽象数据类型;-新的图书馆和标准支持功能范例的元素;-其他工具,使您的程序最可支持;-你也可以像C一样写程序,但是使用c++编译器;但是纯C -比c++快一点,低一点。
#9
3
Another feature C++ has over C is exception handling in the form of throw ... catch.
c++的另一个特性是C是抛出的异常处理…抓住。
#10
3
C++ is much more than C with classes. There are many other concepts inside of C++ like templates, function and operator overloading, exceptions and many others already mentioned here. This makes C++ very powerful and flexible, but also hard to learn. It's not that the single concepts are difficult to understand, but the sum of them and how they are playing together. Take a look on boost to see what everything is possible to do with C++. And I guess it tooks ages to understand what happens under the hood, which is very clear in the case of C.
c++比C语言要多得多。在c++中有许多其他的概念,如模板、函数和操作符重载、异常以及这里已经提到的许多其他概念。这使得c++非常强大和灵活,但也很难学习。这并不是说单一的概念很难理解,而是它们的总和以及它们是如何相互作用的。看一看boost,看看如何使用c++实现所有功能。我猜这是为了让人们了解在引擎盖下发生的事情,这在C的情况下是很清楚的。
Or to put it in a nutshell: C++ is much more then C with classes, or in other words C++ is much more then Java plus memory management.
或者简单地说:c++比C更重要,或者换句话说,c++更多的是Java +内存管理。
#11
2
In 1st of 2 parts of C++ for C programmers, some non-OOP differences are listed:
在C语言程序员的c++的2个部分中,有一些非oop差异被列出:
- Default arguments to functions
- 默认参数的函数
- Function overloading
- 函数重载
- You can declare local variables wherever in the code you need them, not just at the start of a function (also a C99 feature)
- 您可以在需要它们的代码中任意声明本地变量,而不仅仅是在函数的开始处(也是C99特性)
- References, as in
int& x
- 参考文献,如int&x。
- Const-correctness in general
- Const-correctness一般
- Namespaces
- 名称空间
- The built-in boolean type,
bool
, withtrue
andfalse
keywords (also in C99 via stdbool.h)- 内置的布尔型,bool,带有真和假的关键字(也在C99中通过stdbool.h)
- The inline keyword and associated quirks, including implicit inlining and auto-inlining
- 内联关键字和相关的怪癖,包括隐式内联和自动内联。
- A much larger standard library than C
- 一个比C大得多的标准库。
#1
23
Check out Stroustrup's FAQ here, specifically:
查看Stroustrup的FAQ,具体如下:
What is the difference between C and C++?
C和c++的区别是什么?
C++ is a direct descendant of C that retains almost all of C as a subset. C++ provides stronger type checking than C and directly supports a wider range of programming styles than C. C++ is "a better C" in the sense that it supports the styles of programming done using C with better type checking and more notational support (without loss of efficiency). In the same sense, ANSI C is a better C than K&R C. In addition, C++ supports data abstraction, object-oriented programming, and generic programming (see The C++ Programming Language (3rd Edition)"; Appendix B discussing compatibility issues is available for downloading).
c++是C的直系后代,它保留了几乎所有C作为子集。c++提供了比C更强的类型检查,并且直接支持比C c++更广泛的编程风格,因为它支持使用C编程的编程风格,更好的类型检查和更多的notational支持(不损失效率)。在同样的意义上,ANSI C比K&R C更好,c++支持数据抽象、面向对象编程和泛型编程(参见c++编程语言(第3版));附录B讨论兼容性问题可供下载。
#2
24
The C++ language says they are the same:
c++语言说它们是一样的:
int C = 0;
assert(C++ == C);
#3
15
I think you answered your own question: Classes
我想你回答了你自己的问题:课堂。
Its a completely different design paradigm. I think the confusion comes because many people develop C++ programs that are basically C programs and don't even realize it.
这是一个完全不同的设计模式。我认为这种困惑是因为很多人开发了c++程序,这些程序基本上都是C程序,甚至都没有意识到。
Which allows, in the word of its creator, Stroustrup:
这允许,在它的创造者的话语中,Stroustrup:
- data abstraction
- 数据抽象
- object-oriented programming
- 面向对象编程
- generic programming.
- 泛型编程。
Edit: added some other fun stuff
编辑:添加一些其他有趣的东西。
#4
7
In short, C aspires to be a "portable assembler language". It keeps things simple, lets you do things that map almost directly to the underlying hardware, and doesn't present a lot of high level abstractions (you've got functions and.... that's about it)
简而言之,C立志成为一种“便携式汇编语言”。它使事情变得简单,让你几乎直接映射到底层硬件的事情,并且不存在大量的高水平的抽象(你有功能和....这是关于它的)
C++ tries to be everything. A high level language, a low level language, an object oriented language, a multi-paradigm language, a systems programming language, an embedded programming language, and an application development language.
c++试图成为一切。高级语言、低级语言、面向对象语言、多范式语言、系统编程语言、嵌入式编程语言和应用程序开发语言。
The two language really don't have much in common, other than some shared syntax. A C program might compile as C++ with only minor changes, but it'll have nothing in common with "proper" C++ code written for the language.
这两种语言实际上没有多少共同之处,除了一些共享的语法。一个C程序可以编译成c++,只会有一些细微的变化,但是它与为语言编写的“适当的”c++代码没有任何共同之处。
#5
7
Here's a website showing the "incompatibilities" between c and c++:
这是一个显示c和c++之间“不兼容”的网站:
http://david.tribble.com/text/cdiffs.htm#C++-vs-C
http://david.tribble.com/text/cdiffs.htm c++ -vs-C
There are actually quite a few areas where c and c++ diverge (in addition to the classes, template, exceptions, etc).
实际上,在一些领域中,c和c++存在差异(除了类、模板、异常等)。
As far as major differences, here's a list which covers it well:
至于主要的区别,这里列出了一个很好的清单:
- anonymous unions
- 匿名的工会
- classes
- 类
- constructors and destructors
- 构造函数和析构函数
- exceptions and try/catch blocks
- 异常和try / catch块
- external function linkages (e.g., extern "C")
- 外部函数联系(例如,外部“C”)
- function overloading
- 函数重载
- member functions
- 成员函数
- namespaces
- 名称空间
- new and delete operators and functions
- 新建和删除操作符和函数。
- operator overloading
- 操作符重载
- reference types
- 引用类型
- standard template library (STL)
- 标准模板库(STL)
- template classes
- 模板类
- template functions
- 模板函数
#6
4
C++ is mainly an extension of C. Originally C++ was called “C with classes”, highlighting the main original language extension. Already at that time overloading of functions was supported. Since then C++ has acquired exceptions, hierarchical namespaces, generic programming in the form of templates, and lastly multi-threading support. As of C++11 there’s also some minimal core language support for garbage collection using e.g. the Boehm garbage collector. Smaller extensions include reference types, trailing function return types syntax, memory allocation and deallocation machinery that can be overridden, run time type information, and more.
c++主要是C语言的扩展,最初c++被称为“类”,突出了主要的原始语言扩展。在那时,已经支持重载函数。从那时起,c++已经获得了异常、分级名称空间、模板形式的泛型编程,以及最后的多线程支持。在c++ 11中,也有一些用于垃圾收集的最小核心语言支持,例如Boehm垃圾收集器。较小的扩展包括引用类型、拖尾函数返回类型语法、可被覆盖的内存分配和deallocation机制、运行时类型信息等等。
The differences between C and C++, i.e. between C and the “C subset” of C++, are summarized in the C++11 standard's appendix C, entitled “Compatibility”, which is about 20 crammed pages, where the C1 section that pertains to C compatibility is about 10 pages.
C和c++之间的差异,即C和c++的“C子集”之间的差异,在c++ 11标准的附录C中被总结为“兼容性”,这大约是20个填充页面,其中与C兼容的C1部分大约是10页。
It was appendix C also in C++98 and C++03, and it’s appendix C also in the fresh new C++14 standard.
它是附录C在c++ 98和c++ 03中,它的附录C也在新的c++ 14标准。
As of C++11 appendix C of that standard lists the following incompatibilities between C and C++:
在该标准的c++ 11附录C中,C和c++之间的不兼容性如下:
§C1.1 Lexical conventions
New keywords (§2.12)
新的关键字(§2.12)
The paragraph only refers to C++11 §2.12. The following list was generated from that table and a corresponding table in C99.
段仅指c++ 11§2.12。下面的列表是由该表和C99中相应的表生成的。
C99 C++11 _Bool _Complex _Imaginary alignas alignof asm auto auto bool break break case case catch char char char16_t char32_t class const const const_cast constexpr continue continue decltype default default delete do do double double dynamic_cast else else enum enum explicit export extern extern false float float for for friend goto goto if if inline inline int int long long mutable namespace new noexcept nullptr operator private protected public register register reinterpret_cast restrict return return short short signed signed sizeof sizeof static static static_assert static_cast struct struct switch switch template this thread_local throw true try typedef typedef typeid typename union union unsigned unsigned using virtual void void volatile volatile wchar_t while while
Type of character literal is changed from int
to char
(§2.14.3)
类型的字符文字改变从int、char(§2.14.3)
String literals made const
(§2.14.5)
字符串常量(§2.14.5)
§C1.2 Basic concepts
C++ does not have “tentative definitions” (§3.1).
c++没有“暂定定义”(3.1)。
A struct
is a scope in C ++ , not in C (§3.3).
结构是一个范围在c++中,不是在C(§3.3)。
A name of file scope that is explicitly declared const, and not explicitly declared extern
, has internal linkage, while in C it would have external linkage ($3.5).
文件范围的名称是显式声明的const,而不是显式声明的外观,具有内部链接,而在C中,它将具有外部链接($3.5)。
main
cannot be called recursively and cannot have its address taken (§3.6).
main不能被递归地调用,不能使用它的地址(3.6)。
C allows “compatible types” in several places, C++ does not (§3.9).
在一些地方,C允许“兼容类型”,c++不允许(3.9)。
§C1.3 Standard conversions.
Converting void*
to a pointer-to-object type requires casting (§4.10).
转换void * pointer-to-object类型需要铸造(§4.10)。
Only pointers to non-const
and non-volatile objects may be implicitly converted to void*
(§4.10).
只能暗中non-const和非易失性对象的指针转换为void *(§4.10)。
§C1.4 Expressions.
Implicit declaration of functions is not allowed (§5.2.2).
隐式声明的函数是不允许(§5.2.2)。
Types must be declared in declarations, not in expressions (§5.3.3, §5.4).
类型必须是声明中声明,而不是表达式(§5.3.3,§5.4)。
The result of a conditional expression, an assignment expression, or a comma expression may be an lvalue (§5.16, §5.17, §5.18).
一个条件表达式的结果,一个赋值表达式,或一个逗号表达式可能是一个左值(§5.16,§5.17,§5.16)。
§C1.5 Statements.
It is now invalid to jump past a declaration with explicit or implicit initializer (except across entire block not entered) (§6.4.2, §6.6.4)
现在是无效的跳过去的声明与显式或隐式初始化(整个块不进入除外)(§6.4.2,§6.6.4)
It is now invalid to return (explicitly or implicitly) from a function which is declared to return a value without actually returning a value (§6.6.3).
从一个函数中返回(显式或隐式)返回一个值而不返回值(6.6.3),这是无效的。
§C1.6 Declarations.
In C++, the static
or extern
specifiers can only be applied to names of objects or functions. Using these specifiers with type declarations is illegal in C++ (§7.1.1).
在c++中,静态或外部指示符只能应用于对象或函数的名称。使用这些说明符与c++类型声明是非法的(安装7.1.1§)。
A C++ typedef
name must be different from any class type name declared in the same scope (except if the typedef
is a synonym of the class name with the same name) (§7.1.3).
c++类型名称必须不同于任何类中声明的类型名称相同的范围(除了如果typedef是同义词的具有相同名称的类名称)(§7.1.3)。
const
objects must be initialized in C++ but can be left uninitialized in C (§7.1.6).
常量对象必须初始化在c++中,但可以离开未初始化在C(§7.1.6)。
No implicit int
in C++ (§7.1.6).
没有隐含的int在c++中(§7.1.6)。
The keyword auto
cannot be used as a storage class specifier (§7.1.6.4).
汽车不能用作存储类关键字说明符(§7.1.6.4)。
C++ objects of enumeration type can only be assigned values of the same enumeration type (§7.2).
c++对象的枚举类型只能被分配相同的枚举类型的值(§7.2)。
In C++ , the type of an enumerator is its enumeration (§7.2).
在c++中,枚举器的类型是其枚举(§7.2)。
§C1.7 Declarators.
In C++, a function declared with an empty parameter list takes no arguments ($8.3.5).
在c++中,使用空参数列表声明的函数没有参数($8.3.5)。
In C++, types may not be defined in return or parameter types (§8.3.5, §5.3.3).
在c++中,类型可能不被定义作为回报或参数类型(§8.3.5,§5.3.3)。
In C++, the syntax for function definition excludes the “old-style” C function (§8.4).
在c++中,函数的语法定义排除了“老式的”C函数(§8.4)。
In C++ , when initializing an array of character with a string, the number of characters in the string (including the terminating '\0'
) must not exceed the number of elements in the array (§8.5.2).
在c++中,当初始化一个字符的字符串数组,字符串中的字符数(包括终止\ 0)不得超过数组中元素的个数(§8.5.2)。
§C1.8 Classes.
In C++, a class declaration introduces the class name into the scope where it is declared and hides any object, function or other declaration of that name in an enclosing scope (§9.1, §7.1.3).
在c++中,类声明将类名引入到它被声明的范围内,并在封闭范围内隐藏该名称的任何对象、函数或其他声明(9.1,7.1.3)。
In C++, the name of a nested class is local to its enclosing class (§9.7).
在c++中,嵌套类的名称是本地包含它的类(§9.7)。
In C++, a typedef
name may not be redeclared in a class definition after being used in that definition (§9.9).
在c++中,类型名称后不得重新定义在类定义中使用这个定义(§9.9)。
§C1.9 Special member functions.
The implicitly-declared copy constructor and implicitly-declared copy assignment operator cannot make a copy of a volatile
lvalue (§12.8).
隐式声明的复制构造函数和隐式声明的复制赋值操作符不能复制volatile lvalue(12.8)。
§C1.10 Preprocessing directives.
Whether __STDC__
is defined and if so, what its value is, are implementation-defined (§16.8).
__STDC__是否定义,如果是这样,它的价值是什么,是实现定义(§16.8)。
To ease the regeneration of the comparative keyword table, and to build confidence in the one given, here’s the C++ program used to generate it:
为了简化比较关键字表的更新,并在给定的情况下建立信任,这里是用于生成它的c++程序:
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
#define ITEMS_OF( c ) c.begin(), c.end()
enum class Language { c, cpp };
auto compare( Language const a, Language const b )
-> int
{ return int( a ) - int( b ); }
struct Keyword
{
string word;
Language language;
friend
auto operator<( Keyword const& a, Keyword const& b )
-> bool
{
if( int const r = a.word.compare( b.word ) ) { return (r < 0); }
return (compare( a.language, b.language ) < 0);
}
};
void add_words_from(
string const& filename, Language const language, vector< Keyword >& words
)
{
ifstream f( filename );
string word;
while( getline( f, word ) )
{
words.push_back( Keyword{word, language} );
}
}
auto main() -> int
{
vector< Keyword > words;
add_words_from( "kwc.txt", Language::c, words );
add_words_from( "kwcpp.txt", Language::cpp, words );
sort( ITEMS_OF( words ) );
int const w = 20;
int previous_column = -1;
string previous_word = "";
cout << left;
for( Keyword const& kw : words )
{
int const column = (int) kw.language;
int const column_advance = column - (previous_column + 1);
if( column_advance < 0 || previous_word != kw.word )
{
cout << endl;
if( column > 0 ) { cout << setw( w*column ) << ""; }
}
else
{
cout << setw( w*column_advance ) << "";
}
cout << setw( w ) << kw.word;
previous_column = column; previous_word = kw.word;
}
cout << endl;
}
#7
3
Templates is another big difference (in addition to classes/objects). Templates enable for example typesafe generic container types (their first use-case) and (bastardized) lambda-expressions (boost::lambda
).
模板是另一个大的区别(除了类/对象)。模板支持范例类型的泛型容器类型(它们的第一个用例)和(bastardized) lambda-表达式(boost::lambda)。
C++ is a much bigger language (not just library) than C.
c++是一种比C语言更大的语言(不仅仅是库)。
#8
3
This question doesn't have short answer.
In general C++ supports:
- OOP paradigm;
- generic programminng;
- template methaprogramming;
- Abstract data types;
- New libraries and standard supports elemnts of functional paradigm;
- other tools for make your program most supportable;
- Also you could write programs on C-like style but use C++ compiller;
But pure C - a little faster than C++ and more low level.
这个问题没有简单的答案。一般c++支持:- OOP范例;——通用programminng;-模板methaprogramming;——抽象数据类型;-新的图书馆和标准支持功能范例的元素;-其他工具,使您的程序最可支持;-你也可以像C一样写程序,但是使用c++编译器;但是纯C -比c++快一点,低一点。
#9
3
Another feature C++ has over C is exception handling in the form of throw ... catch.
c++的另一个特性是C是抛出的异常处理…抓住。
#10
3
C++ is much more than C with classes. There are many other concepts inside of C++ like templates, function and operator overloading, exceptions and many others already mentioned here. This makes C++ very powerful and flexible, but also hard to learn. It's not that the single concepts are difficult to understand, but the sum of them and how they are playing together. Take a look on boost to see what everything is possible to do with C++. And I guess it tooks ages to understand what happens under the hood, which is very clear in the case of C.
c++比C语言要多得多。在c++中有许多其他的概念,如模板、函数和操作符重载、异常以及这里已经提到的许多其他概念。这使得c++非常强大和灵活,但也很难学习。这并不是说单一的概念很难理解,而是它们的总和以及它们是如何相互作用的。看一看boost,看看如何使用c++实现所有功能。我猜这是为了让人们了解在引擎盖下发生的事情,这在C的情况下是很清楚的。
Or to put it in a nutshell: C++ is much more then C with classes, or in other words C++ is much more then Java plus memory management.
或者简单地说:c++比C更重要,或者换句话说,c++更多的是Java +内存管理。
#11
2
In 1st of 2 parts of C++ for C programmers, some non-OOP differences are listed:
在C语言程序员的c++的2个部分中,有一些非oop差异被列出:
- Default arguments to functions
- 默认参数的函数
- Function overloading
- 函数重载
- You can declare local variables wherever in the code you need them, not just at the start of a function (also a C99 feature)
- 您可以在需要它们的代码中任意声明本地变量,而不仅仅是在函数的开始处(也是C99特性)
- References, as in
int& x
- 参考文献,如int&x。
- Const-correctness in general
- Const-correctness一般
- Namespaces
- 名称空间
- The built-in boolean type,
bool
, withtrue
andfalse
keywords (also in C99 via stdbool.h)- 内置的布尔型,bool,带有真和假的关键字(也在C99中通过stdbool.h)
- The inline keyword and associated quirks, including implicit inlining and auto-inlining
- 内联关键字和相关的怪癖,包括隐式内联和自动内联。
- A much larger standard library than C
- 一个比C大得多的标准库。