C + + I want to use this (v)

Source: Internet
Author: User

In the first part of the "C with Class" syntax, the second part continues:

Part II: Process-oriented programming styles

What is process-oriented I think if you do not know, then you are definitely not a C programmer! In fact, I personally feel that the process-oriented, modular C programming style is the best paradigm, because usually we encounter most of the problems can be solved with this paradigm, and the design is quite concise and crisp, not to be muddy, contrary to constant tossing object-oriented model really will let you away from the nature of the problem more and more far. No matter how familiar you are with the process paradigm, there are still some differences in the C + + environment:

1. About references:

Reference is a common concept in modern programming language, and it is also a new language feature introduced by C + +, which is an important content commonly used in C + +. But can't I just throw out a groove and not use any other operator? Friends who are accustomed to using C for development may be confused when they see the & symbols appearing in C + +, because the symbol in C is a representation of the address, but in C + + it is used to denote references, and thus produces a large number of confusing formulations:

#include <stdio.h>
int main (void)

{
  
int a = 0; &a = 0x0012ff60
  

int *p = &* (int*) 0x0012ff60;

printf ("The value is:%d\n", *p);

return 0;

}

I really do not want to post it, often see all want to vomit, in short, this is a very wonderful phenomenon, in a hand has been a pointer to the weapon of the language in the introduction of references, really is very chaotic. My advice is to:

A) If you are a C + + programmer, use your references, and don't play with pointers;

b) counter you are C programmer, that please self-love, away from references!! At least C with class is forbidden.

Use of 2.new and delete note

So far, the new and delete operators are the only things I have accepted in C + +, and since they have to be accepted for object-oriented, they must be used well.

New usage:

1. Open a single variable address space

1) int *a = new int; Opens an array of storage space and returns an address to that storage space. int *a = new int is the assignment of an int type address to an integer pointer a.

2) int *a = new int (5); function as above, but at the same time assign the integer value to 5

2. Open Array space

int *a = new int[100];//Open an integer array space of size 100

Delete usage:

1. Delete A; Free space for a single int

2. Delete [] A;//release int array space

Be aware of the difference between delete and delete[], C + + tells us to use delete[] when reclaiming the memory space of a single object allocated with new, using Delete to reclaim the memory space of a set of objects allocated with the new[]. About new[] and delete[], which are divided into two situations: (1) Allocate and reclaim space for basic data types, and (2) allocate and reclaim space for custom types.

Take a look at the following program:

#include <iostream>;using namespace Std;class t {public:  t () {cout << "constructor" << Endl;}  ~t () {cout << "destructor" << Endl;}}; int main () {  const int NUM = 3;  t* p1 = new T[num];  cout << hex << p1 << Endl;  delete[] P1;  Delete P1;  t* P2 = new T[num];  cout << p2 << endl;  Delete[] P2;}     

You can run this program on your own, and look at the different results of the delete P1 and delete[] P1, and I won't post the results here. From the running results we can see that delete P1 in the process of reclaiming space, only p1[0] This object called the destructor, other objects such as p1[1], p1[2] and so on have not called their own destructor, this is the crux of the problem. If you use delete[], all objects will first call their own destructors before reclaiming the space. Basic types of objects do not have destructors, so it should be possible to reclaim the array space of basic types with delete and delete[], but only with delete[] for an array of class objects. For a single object of new, you can only use Delete to reclaim space with delete[].

So a simple use principle is: New and delete, new[] and delete[] corresponding to use.

3. function parameter Default value

Yes, many C programmers have long known that C + + can assign default values to function parameters, and default values if they are not specified at the time of invocation. I think this feature is really good, can provide great convenience for the design of the API, but there is no in C, for my obsessive-compulsive disorder, I still give up this feature, but give up does not mean that no, in fact, we can also simulate the use of C to achieve this feature, is the most sharp macro!!

The specific practice is as follows:

1#include <stdio.h>2 3  4 5 enum{6 7plain=0, italic=1, bold=28 9 };Ten  One   A  - voidPrintstring (Const Char* Message,intSizeintstyle) { -  theprintf"%s%d%d\n", Message,size,style); -  - } -  +   -  + #definePrint_string_1_args (message) printstring (message, italic) A  at #definePrint_string_2_args (message, size) printstring (message, size, italic) -  - #definePrint_string_3_args (message, size, style) printstring (message, size, style) -  -   -  in #defineGet_4th_arg (Arg1, arg2, Arg3, Arg4, ...) arg4 -  to #definePrint_string_macro_chooser (...) +  - Get_4th_arg (__va_args__, Print_string_3_args, the  * Print_string_2_args, Print_string_1_args,) $ Panax Notoginseng   -  the #definePrint_string (...) Print_string_macro_chooser (__va_args__) (__va_args__) +  A   the  + intMainintargcChar*Constargv[]) { -  $Print_string ("Hello, world!."); $  -Print_string ("Hello, world!.", A); -  thePrint_string ("Hello, world!.", A, bold); - Wuyi     return 0; the  -}

Well, it is really complicated, but it can be properly simplified, but if you really want to design the library, the above complete writing is really great, oh, and portability is very high, of course, this is not what I thought out, the original link here:

Http://stackoverflow.com/questions/3046889/optional-parameters-with-c-macros

4.inline function

The previous C has no inline this thing, for some of the small functions to be used frequently, we often write them as macro functions, which can reduce the overhead of function expansion. But C + + is some, so a lot of compilers also for C added this extension, such as GCC, but C99 appeared, to C added this feature. It was all good, but the cup with the C99 of the inline and many compilers previously added that the usage is not exactly the same, if you are accustomed to the GCC style of inline, please pay attention to Ah, I found this on the network, see it:

http://blog.163.com/zhaojie_ding/blog/static/172972895200811545032240

5. Overloaded functions

A word in real life can have a variety of meanings, such as washing clothes, washing your hair, washing your car, having a wash word, but they do not operate the same way. Functions are the same, sometimes they are not exactly the same, but the same name is the overloaded function.

The overloaded function is that more than two functions take the same function name, but the number or type of function parameters is different, and the compiler will automatically determine which function to invoke based on the type and number of actual participating parameters. Why do we have overloaded functions? Because if there are no overloaded functions, it is necessary to do similar operations on different types of data to define functions with different names, such as addition functions, and you must define different function names for integer addition and floating-point addition, respectively:

int Nadd (int a, int b);

Float Fadd (float A, float b);

This calls to remember the function name too many, and the function is similar, very inconvenient. This is a syntax feature of C + +, not commenting good and bad, I am not in C with class.

6. Template functions

Sometimes we don't use overloaded functions to achieve optimal results, for example, two functions that seek absolute values:

int abs (int x)

{

Return x<0? -x:x;

}

Double abs (Double x)

{

Return x<0? -x:x;

}

We observe these two functions, these two functions are only return value types and parameter types, function exactly the same, if there is a way to write a common code for a number of different data types, it is not necessary to write two functions as above and just a piece of code can implement the functions of two functions, Isn't the reusability of the code much higher? The development efficiency will also increase. This requires a function template to be implemented. The function template is defined in the following form:

Template <typename identifiers >

function definition

Yes, the template function has been designed to the template, and most importantly, this is not the idea of OOP, but a higher level of a paradigm, namely: generic programming. Obviously this is too far from the C with Class .

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.