I use C + + for the reasons--about C and C + + choice

Source: Internet
Author: User

?

Excerpt from: http://www.xue163.com/32/6/325715.html, Wang Ke. Well organized!


First of all, I will not use Java or C #, the ability to not, subjectively not, because two point reason: 1, they are in the interpretation of the language, there are many problems I can not tolerate, the speed of the program and the security of the package, 2, they are not enough bottom, no pointers, but loaded the memory manager, For me these are troubles and bondage, and to me they are not free enough language. Or, I can't accept their idea of the user, it seems they think that using their programmers are lazy and easy to make mistakes, and their high-level is dependent on a variety of libraries, but there are many restrictions on the underlying operations, such as type conversion, very verbose. C and C + + are different, they can give me full freedom, by the programmer to assume the consequences of their own logic errors, and the development of the required class library, which is more in line with my personality.

I am now able to skillfully use C and C + +, my work mainly maintain the previous C code, this part of the code to modify and supplement, I still use C language, I use C + + for new development tasks, such as Digital image processing class library, data migration module and so on. I first was a skilled C programmer, and then gradually mastered C + +, the original use of C + + is that he may be more convenient and advanced than C, do not rule out the pursuit of a fashionable plot. Now I am also a master of both languages, and have hesitated to end up using C or C + +, mainly worried about C + + speed than C.


Recently saw about C and C + + controversy, adhere to the C language of the argument to me have a certain touch, because I am now basically using C + + development, they put forward some C + + 's shortcomings is C + + to be proud of things, I have never considered these, these views are very novel, and very insightful; The so-called object-oriented and design patterns, whether we really need or just for the pursuit of some fashionable concepts, this has aroused my deep thinking. For this reason I deliberately try to force C to write the class library that has been written in C + +, found that no, it is too wordy; I try to use only C + + basic content, instead of using advanced features such as inheritance, virtual functions, templates, and so on, and directly implement functional classes without using complex classes of relationships such as my template dynamic array, You need to use a collection of memory allocator classes, object constructor classes, object allocator classes, and template algorithms, and I can't find a way to change this design. So, after this attempt, I decided to choose C + + as my main language tool, I know that C and C + + can not replace each other, but for me personally, I still choose c + +. I use C + +, I can only use it and the C language intersection, I could choose not to use any advanced features, or select the part of the advanced features I need, in the appropriate location, but the choice of C means almost impossible, although some features of C + + can be in C or there is an equivalent implementation method, But that will make your code look more tired.

namespaces, I like to use namespaces, although this is not a big problem, but I feel more comfortable using namespaces. In the case of C, I would choose to add a string of prefixes to prevent duplicate names or to indicate that the function is part of this library, such as:?
Lib_func ();?

and C + + is:?
Lib::func ();?

Or:?
Using namespace Lib;?
Func ();?
I prefer to accept the form of C + +, especially if your function name is too long, C will make it longer.

using a method in a struct, I can declare a function that belongs to a struct as its member method, and it is a function of classifying functions, and if it is C, perhaps the author is clear, but as a caller it is necessary to find a function in a large heap of functions to manipulate the struct. And, in my habit, it makes the function name more complex:?

struct STRUCTA A;

Lib_structa_func (&a);?
and the C + + form is:?
Lib::structa A;?
A.func ();?

constructor, I like to initialize the struct with a constructor, which avoids the user forgetting to use memset, or sometimes the initialization value of the struct is not all 0, such as the handle should be-1, or the C language needs to write a function separately for the initial, such as:?
struct structa A;?
memset (&a, 0, sizeof (a));?

Or:?
struct structa A;?
Lib_structa_init (&a);?

and C + + has only one sentence:?
Lib::structa A;?

copy constructors, assignment operators, destructors, and the same thing, overloading these users will consider fewer problems, and the code will be more concise.

class scope control, it can be very simple to control which members and methods are for the user access, which are internal members and methods, and C you want to make sure that the function will not be accessed, can only be written in the source file, but this can not be shared within their own library, So only use the function name to tell the user, this is the internal function, it is best not to call it directly:?
void _lib_func ();?

Usually the underscore is used to represent such a function, but you still cannot completely eliminate the risk.

virtual function, I think it can really affect performance very much, I also seldom use it, but it is a good way to implement callbacks, or I am personally more willing to accept this form. Implementing the algorithm in the parent class, and then providing a set of virtual functions, as long as the subclass inherits it and implements those methods that require the parent class to callback during the algorithm, it is possible to implement the callback, which is a very typical virtual function usage in C + +. And as C, you have to pass function pointers, and write functions that set these function pointers. Although they are the same in essence, the C + + form is more receptive.

For the use of virtual functions in textbooks, to manipulate subclasses with virtual pointers, you can simplify the code, and I don't like to use them, and this is actually less, and it's extremely impacting performance.

using template classes to implement data structures,is a very intuitive thing to the user, and the traditional C method, that is, to define a void * pointer, implementation and use is very verbose, such as:?
struct Array?
{?
void *buffer; /* Data address */?
int bufsize; /* Total number of bytes in memory */?
int typesize; /* The byte length of the element */?
int count; /* The valid number of elements */?
};?
This form has two problems: 1, the access element needs to do pointer type conversion, and there is no type check, error prone, 2, if the data structure of the methods are encapsulated, then certainly no C + + template class library Fast, because the latter most of the functions are inline.
Then there is only the second form, that is, completely with the macro to do, this is the so-called macro to achieve with the C + + template class equivalent function, I believe many people have seen that code, it can almost reach the same form as the C + + template class: (note, these are macros)?
Lib_declarearray (A, int);/* declares an int array */?
Lib_array_element (A, 0) = 100; /* Access to the first element */?
C + + template classes in the form of: (after compilation is the real function instantiated)?
lib::array<int> A;?
A.element (0) = +;?
It may even be faster than C + +, because it is all macros, but its implementation code is too readable and very difficult to debug.
The advantage of C + + is the intuitive use of templates and the support of inline functions, if there is no inline function, I do not think I would choose to use a template class to implement the data structure.
?
If you say namespaces, member functions, automatic initialization and destruction, virtual function callbacks, these are not really big problems, because just C + + form better, then for me,templates and template classes are essential. If you want to use C to implement the same template mechanism as C + +, the programmer's skill requirements are too high, and the resulting code is not only a lot of confusion, for I used to use C + + template, I will not be happy to use C's macro skills. and C can only use macros, and then these macros represent a large segment of the code to embed in the caller's code, so that its function becomes very large, it is like a template class all member functions must be inline functions, and C + + is optional.
A template also has the benefit of just writing a code for the algorithm, especially if the algorithm is not too concerned or completely independent of the type of the parameter, you do not use this technology, you have to write a lot of code. If the type is limited, then fortunately, you write a few identical functions at most, you can also take advantage of the macro technique, but if your function needs to accept an indeterminate parameter type, or for an infinite number of possible types to be valid, then it is not the problem of how many code to write, but you simply can not be implemented perfectly or completely.
such as copy a variable to another variable, so simple algorithm, in C + + as long as the implementation of the copy constructor or assignment operator, you can do:?
Template<class t>?
void Copy (T &a, const T &b)?
{?
A.~t ();?
New (&a) T (b);?
}
It's easier to use an assignment operator:?
Template<class t>?
void Copy (T &a, const T &b)?
{?
A = b;?
}?
But if it is C, you must specify the address of an assignment function:?
void copy (void *pa, const void *PB, void (* func) (void *, const void *))?
{?
(*func) (PA, Pb);?
}
The function is meaningless, writing the copy constructor and assignment operator in C + + is a default habit, but it is not customary to write a separate copy function in C. If there are no templates, a large number of such operations are passed through the function pointer.
?
exceptions,If you're fully aware of the difference between returning an error code and using exceptions, you'll think that's not the problem with WHO to replace, at least you have one more way in C + +, and in C you can only:?
int ret = 0;?
if (SUCCESS! = (ret = func1 ()))?
return ret;?
else if (SUCCESS! = (ret = FUNC2 ()))?
return ret;?
else if (SUCCESS! = (ret = func3 ()))?
return ret;?
Else?
return SUCCESS;?
It looks tired, and the logic of the program looks very unclear.
and the C + + form:?
try?
{?
Func1 ();?
Func2 ();?
Func3 ();?
}?
catch (Exception &e)?
{?
return ERROR;?
}?
You can choose to catch or ignore, let the higher level function to catch, also can handle part after continue to throw up, or completely rid of the exception. And you can still choose how to return the error code.
For me, this is a two-way error-handling approach, and I will choose a different approach depending on the need, instead of replacing all the return error codes with exceptions. The C language also has similar things, that is, longjmp, so-called Super Goto, it is very similar to the anomaly, but if you want to deal with multiple-level exceptions like C + + will be very tired, because longjmp must be in error occurs when to specify where to go to the location, then can not be like C + + That can be captured at any level. But it also has a function that C + + exceptions can not do, is to jump to a farther place, you can go to the previous call order, and not just the level, such as:?
func1 ();?
Func2 (); /* Can you jump back to func1 * *?
In C + +, func1 is not likely to catch FUNC2 exceptions. As a result, C + + 's exception pass order is based on the depth of the function stack, and C's longjmp is based on the process, which can go back to any location previously traversed. But I think I will never be able to use this feature.
?
More importantly, I am writing a library, is to be used, if a variable length string is a lot of macros, then how to call people. The development library takes into account the user experience and saves his code for the user, rather than reducing readability for his program, leaving him with your macro name everywhere, and his own logic is overwhelmed by your code. I think that in this respect C + + do well, he is the understanding of modern people to show your interface, in C + + the performance of the simple things are fast, and C do not have C + + concise, if you want to achieve maximum performance, but it must be complex.

?

I use C + + for the reasons--about C and C + + choice

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.