| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| C++ (Non Visual C++ Issues) Ask or answer C and C++ questions not related to Visual C++. This includes Console programming, Linux programming, or general ANSI C++. |
![]() |
|
|
Thread Tools | Search this Thread |
Rating:
|
Display Modes |
|
#1
|
|||
|
|||
|
Hi all,
I want to know which standards i should follow (C++0x , TR1..etc?) if i want to continue further on C++ development. I came from heavy Java programming background and fairly new to C++ .I do almost all the development under linux/GCC (4>) . I want to know what kind good-practices I have to study more.For example ,instead of dynamic memory allocation for arrays,now I know I can use vectors,usage of smart pointers..etc. Since I came from Java , I tend to compare C++ features with Java.For example,I saw several GC libraries and confused about choosing correct one. What other good-practices I should learn? Is there a standard I should follow? Thanks in advance. |
|
#2
|
|||
|
|||
|
Re: C++ standards and good-practises ?
Quote:
Quote:
__________________
C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way Kindly rate my posts if you found them useful
|
|
#3
|
||||
|
||||
|
Re: C++ standards and good-practises ?
Most of the good practices are explained in a series of books called 'In depth C++ series'. There are also some other well-known and valuable books. Have a look at the FAQ section for a starter.
__________________
Cheers, D Drmmr Please put [code][/code] tags around your code to preserve indentation and make it more readable. As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky |
|
#4
|
|||
|
|||
|
Re: C++ standards and good-practises ?
Quote:
There's an initiative called Boost. It's a kind of a testing ground for what would be good to have in the C++ standard. It's a very high quality library you should use rather than implementing the corresponding features yourself. And eventually many Boost classes make it into the C++ standard. http://www.boost.org/ There's also a book called Beyond The C++ Standard Library by Karlson which discusses extensively some of the most important Boost classes. When it comes to good C++ usage there's an outstanding book called C++ Coding Standards by Sutter & Alexandrescu. The Java equivalent to this book would be Effective Java by Bloch. Finally it's better for you to treat C++ as a brand new language rather that looking back at Java. These languages may look alike because of the shared C syntax, but that's only superfluously. At a deeper level they're extremely different. |
|
#5
|
|||
|
|||
|
Re: C++ standards and good-practises ?
Quote:
The problem occurs when a Java programmer attempts to program in C++, but then uses Java as a model in programming C++. This lends to disastrous results at worst (buggy programs, memory leaks, etc.), and writing nonsensical, slow C++ code (though the code may work) at the least. Quote:
Regards, Paul McKenzie |
|
#6
|
|||
|
|||
|
Re: C++ standards and good-practises ?
Why, if you have a C++ garbage collector at your disposal?
|
|
#7
|
|||
|
|||
|
Re: C++ standards and good-practises ?
Learning how to manage memory, both manually and with methods like RAII, is an integral part of programming with C++. Even if you have a GC that you're ok with using, about 99.99% of the C++ code in existance doesn't use a GC.
|
|
#8
|
|||
|
|||
|
Re: C++ standards and good-practises ?
Quote:
What about smart pointers? Are they integral to C++? If so, why more so than a GC? Last edited by nuzzle; August 8th, 2009 at 10:13 PM. |
|
#9
|
|||
|
|||
|
Re: C++ standards and good-practises ?
Quote:
Quote:
Quote:
|
|
#10
|
|||
|
|||
|
Re: C++ standards and good-practises ?
Quote:
In addition, once you write a program that is tied to a garbage collector, you're stuck using that GC unless you rewrite the program to do memory management the normal way. If GC were standard in C++, then that's another story, but it isn't. C++ programmers are not going to tie an integral part of C++ (memory management) to something that is non-standard. What shold be learned is proper handling of dynamic memory, or in other words, how to avoid doing a lot of that work yourself and rely on properly built classes that handle the memory for you (i.e. RAII). Quote:
Regards, Paul McKenzie Last edited by Paul McKenzie; August 8th, 2009 at 11:22 PM. |
|
#11
|
|||
|
|||
|
Re: C++ standards and good-practises ?
umanga,
Along with the "In Depth" series, I also recommend the "Exceptional C++" series, and any good texts on design patterns. However, coding standards are a slightly different subject than these. MISRA, JSF++ or other formalized standards more accurately reflect the notion. You're really asking mostly about best practices vs known bad ideas. You've received good advice thus far. I suggest TR1 is a good idea, especially for shared_ptr. C++0x will take some time, so for now stay with those libraries which function well with your current compiler targets (TR1 qualifies). It's important to keep in mind why C++ and Java are different (to a Java developer studying and using C++). C was originally conceived as an architecturally independent assembler, and C++ retains that heritage. The focus is on performance and adaptability within that paradigm. From the beginning, the adaptation between this language and the underlying machine and operating system is through libraries (the standard C library started this convention). The language does not incorporate features and functionality directly, but through interfaces to libraries. Java and C#, among others, take the opposite view - that of incorporating ever increasing features within the language or it's model of the operating system or machine. A great many complaints are resolved if this is better understood. It causes me to inquire as to why you're now using C++? As the the point about garbage collection....it's a solution in search of a problem. Garbage collection may be central to languages as far back as BASIC, but in 30 years of development in C and C++, I've never had a reason to want for it. It is more often a bad idea or bad solution, because of the performance implications, but mostly, as has been suggested already, there are methods available which better solve the issues for which garbage collection is often indicated. Generally, memory fragmentation isn't the problem it is claimed to be, except in the tightest of memory operating circumstances - and specifically when available RAM is small, garbage collection is generally not as effective as better use of that RAM in the first place. In other words, garbage collection is better suited for chaotic memory usage problems (when usage isn't a problem, garbage collection is a solution for which there is no problem - thus it's in the way). However, if one uses custom memory allocation features available in the STL, or other similar measures, memory usage isn't chaotic, it's organized. There again, the need for garbage collection isn't strong. Consider that a great many high performance applications of considerable ambition don't use garbage collection, and don't appear to need it. Quote:
Quote:
Concepts like RAII, design patterns, the STL - these are not exactly integral to C++ (even though the STL is standard, you can write C++ applications without it, even more so than you can write applications without the use of the C library). However, these concepts are to C++ what writing style is to English (or any spoken/written language). You must use several if not all of these concepts in order to write in C++ effectively. Smart pointers are no more integral to C++ than RAII, but they are among the most basic implementations of RAII - the control over the acquisition and release of memory allocation. You can write in C++ without them, but the leverage of increased reliability and productivity are obvious enough that choosing not to use them is all but a liability. Smart pointers were not widely available until the introduction of templates. Prior to templates it was nearly impossible to create a smart pointer (what was available required derivation from a common base class, and was difficult to use effectively). When templates were first created, smart pointers were among the first classes any fair C++ developer would create. Even naive implementations were useful. They perform a task which is very close to the native behavior of C, adding merely the RAII paradigm to allocation and de-allocation. On the other hand, garbage collection is not so native to C or C++. In most instances a form of smart pointer or representative object is best for a garbage collection library, because the application code need not be written all that differently from that which uses smart pointers without garbage collection, but the concept that pointers may be relocated at any time can be so foreign to some C or C++ coding methods as to create odd bugs and crashes. To put it simply, if the CPU doesn't do it, C and C++ are going to need a library to adapt to it - it is the nature of an assembler like language. In the case of C++/CLI an interesting solution is presented, but careful analysis of the approach reveals that what's happening is very similar to the implementation of a "smart pointer like handle", but instead of implementing that as a library, it's implemented within the compiler. The result is an uncomfortable "new" language (my opinion) which has a small following that might be better served if they worked in C# (though I realize it would also be the only way to provide C++ native code products TO .NET applications - a potential market).
__________________
If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post). |
|
#12
|
|||
|
|||
|
Re: C++ standards and good-practises ?
While C++0x isn't finalized yet, the GNU compiler already supports the majority of it's features. With g++ add the -std=c++0x or -std=gnu++0x compiler options.
|
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|