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

4885 ワード

原文: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++』第2版を参照してください.最後の章では、c.をどのように使うかを示しています.何千人もの大学の初心者が使用し始めました.間違いなく、それは有効です.第2版では、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の目的は、簡単なことを簡単にすることです.もちろん、追加のパフォーマンス損失はありません.