Misunderstood C ++: learning and using

Source: Internet
Author: User

Learning and using
C ++ is too complicated. I guess you must be stuck in front of the computer screen. In fact, I also nodded. That's right. C ++ is really complicated, probably the most complicated language so far. Therefore, a deduction is generated: C ++ is difficult to learn and use.
This statement is also quite accurate, but not always. The Learning and usage of C ++ can be divided into several layers, from simplicity to complexity, from ease to difficulty, which are suitable for learners and users at different levels.
Let's take a look at the complexity of C ++. I have outlined a list, including the main language features of C ++. Some of the basic features, such as variables, conditions, and choices, have been removed, mainly focusing on the complexity of C ++:
1. pointer. Including variable pointers and function pointers. It can be computed.
2. reference. Including variable reference and function reference.
3. Free functions.
4. Class. Including specific classes and abstract classes.
5. Reload. Including function overloading and operator overloading.
6. member data. Including static and non-static.
7. member functions. Including static and non-static.
8. virtual functions. Includes the override of the virtual function.
9. Inheritance. Including Multi-inheritance and virtual inheritance.
10. polymorphism. These include dynamic and static polymorphism.
11. type conversion. Including implicit and explicit, and notorious forced conversion.
12. template. Includes class templates and function templates.
13. Special template. Including full and partial features.
14. Non-type template parameters.
15. template-template parameters.
I compared the C # feature list. C ++ has many features than C #: Multi-inheritance of 1, 2, 3, and 9, static polymorphism of 10, 13, 14, and 15.
C # has more features than C ++ (C ++/CLI is not considered here, C ++/CLI supplements the Standard C ++ ):
1. for each keyword.
2. as and is keywords.
3. seal keyword.
4. abstract keywords.
5. override/new keyword.
6. Some supplementary usage of the using keyword.
7. implicit/explicit keywords. (C ++ has the explicit keyword, but is only used for constructors)
8. interface keyword.
9. property.
10. delegate.
Let's analyze it. C ++ has many features than C # In programming technology, especially programming modes, such as generic programming and metaprogramming functions brought by templates. In OOP, C ++ goes further and more thoroughly, mainly in the aspects of Multi-inheritance and operator overloading. In traditional programming technology, C ++ distinguishes object and object references (pointers and references ). C # references all referenced objects. Finally, C ++ has free functions.
C # And C # are mostly keywords. These keywords are all focused on OOP and aimed at making programming technology easy to understand. Many people (including me) want C ++ to have these keywords (at least part) of C ). But unlike the average person's understanding, the C ++ Standards Committee is actually held by the compiler developers, who are especially allergic to the introduction of keywords. There is no way, but there are always shortcomings in reality.
From these aspects, we can see that C ++ has far more programming mechanisms than C # (Java has even fewer mechanisms ). For new customers, it is enough to swallow the content in one breath to hold them to death. On the contrary, the added keywords in C # help beginners understand the meaning of the Code. These are the real reasons why C # and Java are easier to learn (easier to understand) than C ++.
However, it must be emphasized that C # and Java are easy to learn and understand code, rather than the technical principles and background behind the code.
The vast majority of C # code I have seen is full of repetitive code and a large number of switch operations. If these programmers make full use of the oop mechanism of c # and Java, these serious code redundancy can be eliminated by half. (If C # and Java have generic programming capabilities like C ++, the other half can also be eliminated .) These programmers write software without fully understanding the language mechanism and OOP technology.
This also happens in C ++, but it is relatively small. This is probably because C ++ is complex enough that the learner has the idea that "C ++ cannot be used without a thorough understanding of C ++. The advantage and disadvantage of this idea is to encourage learners to fully understand the technology behind the language and language, but the disadvantage is that it has scared many people away. In fact, we will soon see that C ++ can be the same as C # and Java, and can be programmed only according to the established rules without understanding the principles. Of course we don't want this. This is a bad practice. However
The impetuous mentality, we will also follow the customs.
Note! The following sentence is the most important and ignored for a long time: the reason why C ++ is complex is to make it easier to use. I cannot understand! Self-conflict! Nonsense! Don't worry, and listen to me.
(For limited space, I will only provide the last part of the case code here. The complete case is in my blog :)
There are three containers: c1, c2, and c3. The container type and element type are unknown. It is required to write an algorithm framework to perform some operation on the Elements in c1 and those in c2, and put the results in c3.
Because the container type is unknown, all common interfaces of the container must be used. Therefore, I wrote the following C # code:
Public delegate void alg <T1, T2, R> (T1 v1, T2 v2, R r );
Public static void Caculate_Contain <C1, T1, C2, T2, C3, T3>
(C1 c1, C2 c2, C3 c3, alg <T1, T2, T3>)
Where C1: IEnumerable <T1>
Where C2: IEnumerable <T2>
Where C3: IEnumerable <T3>
{
IEnumerator <T1> eai1 = c1.GetEnumerator ();
IEnumerator <T2> eai2 = c2.GetEnumerator ();
IEnumerator <T3> eai3 = c3.GetEnumerator ();

While (eai1.MoveNext () & eai2.MoveNext () & eai3.MoveNext ())
{
A (eai1.Current, eai2.Current, eai3.Current );
}
}
// Use
Public static void CaculThem (int v1, int v2, int r ){
R = v1 * v2;
}
Caculate_Contain (ai1, ai2, ai3, new alg <int, int, int> (CaculThem ));
Public static void CaculThem2 (float v1, int v2, double r ){
R = v1 * v2;
}
Caculate_Contain (af1, ai2, ad3, new alg <float, int, double> (CaculThem2 ));
I used a delegate as a carrier to pass the algorithm for processing container elements. Use a specific algorithm to create a delegated instance. However, the specific algorithm CaculThem () must be consistent with the corresponding container element type.
C ++:
Template <typename C1, typename C2, typename C3, typename Alg>
Caculate_Container (const C1 & c1, const C2 & c2, C3 & c3, Alg)
{
Transform (c1.begin (), c1.end (), c2.begin (), c3.begin (), );
}
// Use
Template <typename T1, typename T2, typename R>
R mul_them (T1 v, T2 u ){
Returnv * u;
}
Caculate_Container (ai1, ai2, ai3, mul_them <int, int, int> );
Caculate_Container (af1, ad2, ad3, mul_them <float, double, double> );
If the container elements change, the C # code must rewrite the CaculThem () algorithm (). But C ++ is not required. Since mul_them <> () itself is a function template, you only need to instantiate this function template with a new type.
The C ++ code is relatively simple and more flexible. But this is not all. C ++ has a final solution, which does not require loops, template algorithms, or write operation functions:
Transform (c1.begin (), c1.end (), c2.begin (), c3.begin (), _ 1 * _ 2 );

Didn't you understand? I couldn't understand it at first. The Lambda expression of the boost library is used here. The _ 1 placeholder corresponds to the c1 element, and the _ 2 placeholder corresponds to the c2 element. _ 1 * _ 2 indicates that the c1 element is multiplied by the c2 element, and the result is placed in c3. Expressions can be more complex, such as (_ 1 * _ 2 + 3 * _ 1)/(_ 1-_2 ). Lambda expressions can be used in all algorithms that need to be operated. For example, if I want to remove "-" from the string, I can write it like this:
Remove_if (s. begin (), s. end (), _ 1 = '-');
Lambda expressions are based on a technique called "template expressions". Through Operator overloading, an expression is expanded layer by layer to form a parsing tree. Then, it is passed as a function object to the algorithm. The algorithm calls the function object in a loop and runs corresponding calculations.
Nothing is simpler than this. The principle is complex enough, but we can ignore the complicated principle, just use it. Although it's just a small algorithm, you need to know that a huge number of software (like JSF's code with more than 19 million lines) are made up of these tiny algorithms. The algorithms provided by C ++ and the libraries used by simplified algorithms help almost all program algorithms, not only for these underlying algorithms, but also for higher-level algorithms.
Here I will no longer give the C # code, because C # does not support Lambda expressions and cannot be simulated. If you want to, wait for C #3.0.
Well, it is time to make a summary. From the above examples, we can see that in the most basic statements, C # is sometimes easier than C ++, because C # provides more keywords. However, with the gradual complexity of algorithms, the abstract capabilities of C ++ gradually play a role. Once abstract algorithms and code need to be built, C ++'s generic programming capabilities immediately generate a huge amount of energy. Finally, we use the boost: lambda library to minimize the use of algorithms. More importantly, the implementation of Lambda expressions is extremely complicated, but the usage is extremely simple.
This is the meaning of the sentence "C ++ is complicated to make it easier to use. C ++ provides complex mechanisms to build libraries and provide features that are not implemented by languages. These features can greatly simplify development. Such as containers and algorithms in the standard library, Lambda expressions in the boost library, BGL naming parameters, and smart pointers.
That is to say, a programmer can use a variety of ready-made libraries to develop software simply by learning the most basic C ++ programming technology. Just use it. Don't ask why. In this case, the difficulty of learning and using C ++ is no essentially different from that of C # and Java. However, because C ++ can provide more flexible and efficient libraries, it is easier to use than C # and Java in many cases.
To reach this level, programmers need more training than C # and Java. The required training is mainly focused on the use of the standard library, the difference between objects, pointers and references, the processing methods of pointers, memory and resources, and the use of smart pointers; class usage Special Points (constructor, implicit conversion, etc.); correct processing of polymorphism; Template usage. In addition, some "rules" need to be set for learners to avoid misuse of some sensitive language mechanisms. These "rules" only need to be followed and do not ask why. Once these "rules" become part of the instinct (enhanced training can achieve this effect), programmers will become mature. Even if you look back at C # or Java, it is easy to achieve benefits and circumvent weaknesses. (Be Careful. At this time, programmers are likely to swear. I am a relatively gentle man. I generally don't swear, except when I drive and use C # & #61514 ;).
As long as the content is properly organized and used in a standard way, the learners do not need to spend a long time to master it. It takes about two or three months. If they have half a year, they can be skillful. In this way, the trained programmers have a very solid foundation, and can learn any language or technology in the future. If

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.