译自five popular myths about c++ --by Bjarne Stroustrup (1)

原文:https://isocpp.org/blog/2014/12/five-popular-myths-about-c-bjarne-stroustrup

Myth 1: "To understand C++, you must first learn C"

想要理解c++,必须先学c

No. Learning basic programming using C++ is far easier than with C.
这是不对的,c++基础编程的学习要远远比 c 容易

C is almost a subset of C++, but it is not the best subset to learn first because C lacks the notational support, the type safety, and the easier-to-use standard library offered by C++ to simplify simple tasks. Consider a trivial function to compose an email address:
c 可以看作是 c++ 的一部分,但并不是最容易学的那部分,因为 c 没有运算符重载,没有类型安全,也没有用起来更加方便的 c++ 的标准库,这些库可以大大简化工作。试着考虑做一个组合电邮地址的小功能函数:

c++的代码:
string compose(const string& name, const string& domain)
{
  return name+'@'+domain;
}


It can be used like this
这样使用
string addr = compose("gre","research.att.com");


The C version requires explicit manipulation of characters and explicit memory management:
c版本就需要显式的字符控制和显式的内存管理,代码如下:

char* compose(const char* name, const char* domain)
{
  char* res = malloc(strlen(name)+strlen(domain)+2); // space for strings, '@', and 0
  char* p = strcpy(res,name);
  p += strlen(name);
  *p = '@';
  strcpy(p+1,domain);
  return res;
}


It can be used like this

使用时是这样的

char* addr = compose("gre","research.att.com");
// …
// release memory when done
// 最后使用完,记得释放内存
free(addr); 

Which version would you rather teach? Which version is easier to use? Did I really get the C version right? Are you sure? Why?
你愿意教哪个版本?哪一个用起来更简单?我的c代码写对了吗?你确定?为什么?

Finally, which version is likely to be the most efficient? Yes, the C++ version, because it does not have to count the argument characters and does not use the free store (dynamic memory) for short argument strings.
最后,哪一个版本更高效?当然是 c++了,因为它不需要计算字符的长度,也不要申请、释放动态内存

Learning C++

c++ 的学习


This is not an odd isolated example. I consider it typical. So why do so many teachers insist on the “C first” approach?
这不是一个稀奇的特例,我觉得它很典型。但为什么仍然有这么多老师要坚持用 c 入门呢?

Because that’s what they have done for ages.

因为他们经验丰富


Because that’s what the curriculum requires.

因为课程要求


Because that’s the way the teachers learned it in their youth.

因为他们年轻时老师也这么教


Because C is smaller than C++ it is assumed to be simpler to use.

因为c 比 c++ “小”,它用起来应该比 c++ 更简单 


Because the students have to learn C (or the C subset of C++) sooner or later anyway.

因为这些学生早晚要学 c


However, C is not the easiest or most useful subset of C++ to learn first. Furthermore, once you know a reasonable amount of C++, the C subset is easily learned. Learning C before C++ implies suffering errors that are easily avoided in C++ and learning techniques for mitigating them.
可是,c “作为 c++ 的一部分”在初期的学习中并不是最容易最有用的。另外,一旦你懂了一些 c++ 后,再去学c会更容易。先学习 c,经常掉入陷阱,但这些陷阱在 c++ 中可以轻易的避免或通过其他技术弱化。

For a modern approach to teaching C++, see my Programming: Principles and Practice Using C++ [13]. It even has a chapter at the end showing how to use C. It has been used, reasonably successfully, with tens of thousands of beginning students in several universities. Its second edition uses C++11 and C++14 facilities to ease learning.

对于c++教学的现代方法,参考《Programming: Principles and Practice Using C++》第二版。在最后有一章展示如何使用 c.成千上万的大学初学者已经开始使用,毫无疑问,它很有效。在第二版中使用 c++11 和 c++14的工具使学习更容易。


With C++11 [11-12], C++ has become more approachable for novices. For example, here is standard-library vector initialized with a sequence of elements:
新版的c++11 对于新手更容易上手。举个例子,用初始化列表初始化 vector 
vector v = {1,2,3,5,8,13};

In C++98, we could only initialize arrays with lists. In C++11, we can define a constructor to accept a {} initializer list for any type for which we want one.

在 c++98,我们只能用初始化列表初始化数组,而在c++11中,我们可以为任何需要的类型定义一个接受初始化列表的构造函数


We could traverse that vector with a range-for loop:
我们可以用范围 for 循环遍历这个 vector
for(int x : v) test(x);

This will call test() once for each element of v.

每一个 v 中的元素都会调用一次test();


A range-for loop can traverse any sequence, so we could have simplified that example by using the initializer list directly:

范围 for 循环可以遍历任何序列,所以我们可以简化一下刚才的例子,直接使用初始化列表

for (int x : {1,2,3,5,8,13}) test(x);

One of the aims of C++11 was to make simple things simple. Naturally, this is done without adding performance penalties.
c++11 的一个目的就是将简单的事情简单化,当然,这样做不会带有额外的性能损失

你可能感兴趣的:(c++,c++)