C++
We have seen the importance of using COM server components for building a web-based application, but the problem is not whether to build them, but what language to create them. One option is to use C + +.
There are many different views of C + +, and conservative C + + programmers insist on creating COM components in other languages, and they think that only real programmers use C + +. On the other hand, VB programmers think that C + + is a difficult language to master and use, there is no need to increase programming time and make difficult attempts. Java programmers think they are stronger than C + + programmers because James Gosling (the Java inventor) has invented Java with the advantages of absorbing many languages (including C + +), and the purpose of this chapter and the next chapter is to eliminate prejudices and misconceptions about C + +.
This chapter focuses on the use of C + + to build server components, do not speak C + + language, if you want to learn C + +, please refer to Ivor.horton's "beginning Visual c++6", Wrox published, ISBN for ISBN 1-861000-88-x.
The main contents of this chapter are:
? A brief History of C + +.
? Use C + + reason.
? Porting from VB to C + +.
? Introduction to ATL, STL, and MFC.
? Establish a COM component.
? Error handling and debugging.
It is more important to remember that now, instead of just building a component in C + +, you can use any of the tools available in Visual C + + to make the process easier to build. First from the origin of C + +.
17.1 C + + language
Before deciding whether to use C + + language, it is best to understand the nature of this language, let us look at the history and status of C + +.
A brief history of 17.1.1 C + +
The beginning of the C language, those who want to build faster and more effective code is very appreciate the C language, a person named Bjarne Stroustrup is not satisfied with just the production of fast code, he wants to create object-oriented C language programming. He began to make the necessary changes to the C language kernel so that it could meet the requirements of the object-oriented model. C + + from this generation.
Bjarne Stroustrup is the original designer and implementation of C + +. Since its inception, it has been developed and expanded into a fully fledged programming language. Now C + + has been ANSI, BSI, DIN, several other national standards bodies and ISO as the standard. The ISO standard was formally approved by the vote on November 4, 1997.
The C + + standard has evolved for many years. C + + template is an extension of this language in recent years, the template is based on the type parameters to produce functions and classes of the mechanism, sometimes called the template is "parameterized type." Using templates, you can design a class that operates on many types of data without having to create a separate class for each type of data. Both the Standard Template library (Standard tempalte library,stl) and Microsoft's Active Template Library (active Tempalte Library,atl) are based on this C + + language extension.
C + + standards can be divided into two parts, the C + + language itself and the C + + standard library. The C + + standard library is quite new to Visual C + +, in fact, Microsoft only removed some "bugs" when it released Visual C + + 5.0. Standard libraries provide standard input/output, strings, containers (such as vectors, lists, and mappings), non-numeric operations such as sorting, searching, and merging, and support for numeric calculations. It should be said that C + + contains a relatively small number of keywords, and many of the most useful functions are derived from the library, C + + standard library implementation of the container and the algorithm is part of the STL.
STL is a framework of data structure and algorithm, including vector, list and mapping, and the algorithm includes searching, copying and sorting of these data structures. The Ansi/iso C + + standards committee voted in July 1994 to accept the STL as part of the C + + standard library, based on the programming and software Library studies of Alex Stepanov, Meng Lee and David Musser. STL is created to meet the design goal of universality, not to improve performance.
So what is Microsoft's approach to C + + standards? Microsoft running VC + + and Plum-hall C + +, want to compare the score between 92% and 93%. One reason why not 100% is that tracking this standard and building a compiler at the same time is difficult, and Microsoft has considered the importance of compatibility with existing encodings, and sometimes they have to deviate from the standard to maintain this compatibility.
17.1.2 why C + + is used
There should be good reason to use C + + to create a server component, not just to impress your boss with C + +. If you've never used C + + before, you have to do your best to learn.
1. Performance
Performance has two aspects, algorithm speed and machine code efficiency. An algorithm can be defined as the path through which the data is conceptualized through the system, and it describes points where the data can be manipulated and converted to produce a result. For example, an algorithm is defined to get a string, to compute the number of characters in a string, and to return a procedure as a result. Algorithm and
Language is independent, so before programming, you must design algorithms, the first step to write a quick program is to design a good algorithm, with minimal operational steps to get the answer to the question. The second step is to select the language, which also affects the speed of the program.
From the performance point of view, the use of assembly language programming is the best choice, it is the computer can understand the natural language. However, few people wrote the complete program in assembly language because it was extremely tedious. Another best option is the C language. However, all the tools provided by VC + + are produced in C + +, not C. A wizard with VC + + can generate a lot of code without having to write code manually. C + + is the best choice to take into account the ease of writing programs and the performance of programs.
C + + performance is good because it is compiled into machine code. For languages such as VBScript and Java, Code is interpreted by the program at run time, and the code is converted to machine code every time the program is run, which is less efficient, not only that the compiled C + + program runs faster, but that Microsoft C + + compilers have existed for many years. This means that Microsoft's compiler programmer has put a lot of advantages into the compiler so that it can produce very efficient machine code. Because C + + is compiled language, and very natural, more close to the machine code than VB, so the code generated by C + + compiler must be more efficient than VB compiled code.
2. Error handling
The difference between a good program and a great program is whether it has good error-handling support. In fact, the process of developing and testing the entire program will be more perfect if you first make error handling in the implementation, rather than at the end. However, error handling can only be consistent with what the language supports.
VBScript has basic error-handling support capabilities. By default, errors in VBScript cannot be captured. Each time an error is suspected, the On Error Resume next feature is invoked and the Error object is checked.
Error handling in C + + is better because there is "exception handling", which is described in more detail later in this chapter.
3. The smallest dependence
As mentioned above, C + + is a compiled language in which C + + code is converted to machine code before execution. As long as this code does not depend on external dynamic link libraries (DLLs), C + + can move to other machines and microprocessors running the same operating system without having to install additional programs, while moving Java programs requires installing the Java Runtime Library first.
4. Using the existing code
Since C and C + + have existed for many years now there is a lot of available code, your server component can use existing C/COM + + code or libraries. such as the statistical library and the C interface to the old system.
5. Maximize COM Features
COM is very close to C + +, in fact, Don Box (COM's authority) in his first chapter of "Essential com" wrote: "com is better C + +." He explained how the COM specification was generated from the C + + language laws. By understanding C + +, you will have a deeper understanding of COM.
Some languages cannot take advantage of all COM features, and in C + +, you can use almost all of the COM features.
Why 17.1.3 does not use C + +
It's important to know when to use C + +, and it's also important to know when not to use C + +. Imagine people who maintain code for long periods of time, and if none of them support C + +, developers have to turn their eyes to other languages they know well.
When you change the C + + component, to see the results of these changes, you must recompile the component code, which can take a long time to develop. C + + can not be like the ASP page code, only use Notepad, change the line of code, reload and get results. Therefore, do not use C + + if certain jobs require frequent changes (such as prototypes).
In C + +, it's easy to write a code that crashes a component without getting more protection for some fatal bugs. This is the price to pay for fast code, and C + + does not stop to check whether the code is running as designed and whether the program doesn't crash depends on the developer's skills. If you spend less time in this area or just learn C + +, it's best not to use C + +. Wait until you are aware of all the errors in C + +, and spend a lot of time before testing components to use C + +, if you want to quickly and easily build a component, and do not consider the implementation of the component speed, then use VB Bar!
17.1.4 to transfer ASP skills to C + +
The best way to learn new things is to use existing skills. For ASP developers, many of the techniques required by C + +, especially the concepts of JScript syntax and ActiveX or COM object-oriented programming, have been learned.
1. JScript
Most ASP developers use JScript on a browser using DHTML. JScript's syntax is very similar to C, so if you know JScript, you know the basic C syntax. Of course, just a subset of C + + syntax. C + + has a lot of extra syntax to support object-oriented programming, and that's what we're going to do next.
2. Object-Oriented Programming
If you have used classes in VB, you should be familiar with any COM objects and document object models (documents Objectmodel,dom), as there is already a concept of object-oriented programming (OOP). As mentioned earlier, the difference between C and C + + is that C + + supports object-oriented programming.