Tag Info

Hot answers tagged

37

Each programming language has a set of idioms and best practices, which will usually lead to elegant, correct, and performant code. Here are a few worst-practices that are perfectly fine in some other language: I will yell if you write for ($i = 0; $i < 42; $i++) { … } in Perl, but not in PHP (in Perl, variables should be declared, and such loops should ...


25

Without seeing the code in question, there are a few ways to write Java code in C++, some worse than others. At the one extreme, there's laying out your source like Java: everything in one file, everything within the class definition, etc.: class HelloWorldApp { public: void main() { cout << "Hello World!" << endl; } }; ...


13

This whole question is dodgy. The question statement implies the possibility of multiple choices, while the radio buttons indicates a single choice. Furthermore, b is pretty suspect, as void functions don't return anything. D is also questionable, for as far as I know, you cannot have an array of functions. Sure, you can have an array of function pointers, ...


11

I'm not a hardcore C++ developer, but... Why would it be a bad programming practice to use more error prone code in prototype situations, if refactoring makes it more robust afterward? One thing to keep in mind is that an error in C++ usually means "undefined behavior". In a safe language the worst that could happen is an exception terminating your ...


11

Use UTF-8. string.size() won't equal the amount of code points, but that is mostly a useless metric anyway. In almost all cases, you should either worry about the number of user-perceived characters/glyphs (and for that, UTF-32 fails just as badly), or about the number of bytes of storage used (for this, UTF-32 is offers no advantage and uses more bytes to ...


9

I am writing a C++ library So why don't you use C++ instead of C? getData(std::vector<float> &data); is most probably what you want. And even when programming in C, do yourself a favor and avoid to use fixed limits like MAX_ITEMS_SIZE, that number will almost be too big (wasting resources) or too small (which means your program might not ...


6

With your restriction of "besides header files", the answer is: No. The C++ compiler compiles each source file independently. If you intend to use a declaration that appears only once, it must appear in a header file. (This does not consider things that wouldn't be considered good programming practice, such as including one .cpp file within another, or ...


6

The canonical way to create an interface in C++ is to give it a pure virtual destructor. This ensures that No instances of the interface class itself can be created, because C++ does not allow you to create an instance of an abstract class. This takes care of the not constructible requirements (both default and copy). Calling delete on a pointer to the ...


5

Slightly offtopic answer... Don't worry - this is a common "behavior" in any expert community. And be honest, if you're good in any language, and you will meet a code what is "strange", probably you will critizing it too. (because, want TEACH). I'm in the perl-world - when will see something like: $imax=$#array; $str="" for($i=0; $i<$imax; $i++) { ...


5

Typically you would see size_t getDate(float* buffer, size_t bufferSize); The return value is the amount of data returned and you pass in how large your buffer actually is. EDIT: In your comments to want to have an error return code and remain C compatible, if so I suggest doing like Doc brown suggested: int getDate(float* buffer, size_t bufferSize, ...


4

You don't use namespaces to separate interface from implementation in C++. Namespaces are essentially packages in C++; you use them to group together related classes. Rather, you use classes to hide the implementation details from the user. Classes in C++ are the same as classes in any other OOP language in that regard; they provide a public interface, ...


4

I would break them so each line is conveying a different concern such as superclass constructor invocation or an expression. This might not be 79 characters, but with modern widescreen, high resolution monitors is that truly necessary anymore? Whenever possible, let the IDE format it for you. Some are better at this than others: for example, I have found ...


4

Answer c) is also arguably false. "Inline functions are expanded during compile time to avoid invocation overhead." Firstly, the C++ compiler is permitted to ignore the inline hint and not expand the functions at all. Secondly, the stated "reason" for doing expansion is an oversimplification. In fact, the real reason for doing the expansion (or not) is ...


4

This is because: 1) it defeats the entire purpose of namespaces, which is to reduce name collision; 2) it makes available to the global namespace the entire namespace specified with the using directive. For example, if you include and define your own max() function, it will collide with std::max(). http://en.cppreference.com/w/cpp/algorithm/max The ...


3

Am I paranoid... Am I paranoid to want the code to be protected against slicing in interfaces? (I believe I'm not, but one never knows...) Isn't this a risk management issue? do you fear that a bug related to slicing is likely to be introduced? do you think it can go unnoticed and provoke unrecoverable bugs? to what extent are you willing to ...


3

It depends on the nature of those custom operations, but probably yes, it's bad design. And the main reason for stating this is that you are coupling operation and storage. If a certain custom operation is semantically independent, it can be better implemented as an standalone (functor) class or function, taking an specific container parameter. Moreover, if ...


3

I believe that the Hide()/Show() alternative is attractive because it's easier to understand what's going on than with SetVisible(true), while having a single function is preferable because it avoids lots of conditionals. If that's the case then I suggest using an enumeration as the input to SetVisible, so you get either SetVisible(Visibility.Visible) or ...


3

As depicted here, what you are looking for is the 1st-order 2-dimensional Voronoi diagram under the Manhattan, or L1-metric. This is a quite non-trivial problem (to solve efficiently), fortunately with many existing algorithms and software. You actually want the subset of the Voronoi diagram that coincides with the discrete grid defined by your matrix ...


3

The idea of headers is that we separate the public interface (i.e. declarations) from internal implementation details (i.e. the actual method bodies). This split has all kinds of advantages: There's a helper function I need? Let's just put it into the .cpp file, and outside code cannot see it. I can also do things like using std without interfering with ...


3

If anything, it would have to be b. You can't return a void, but you can return a null. Void on the function declaration should be there to notify the compiler of a lack of a returned value. Void Type In C and C++ A function with void result type ends either by reaching the end of the function or by executing a return statement with no returned ...


2

I believe your design would be better if the cursor attribute was changed to a function that created and returned a new cursor object that referenced this sound object. That way you can have multiple modifiable cursors referencing the same sound object, all the cursors at different positions in the sound file -> which is just how cursors should work.


2

If I can #include "your_library.h" and then type "ThatGuysModularClass obj;" and continue to do exactly what I want it to do, with a well documented API (i.e.: "this method does this, that one does that", etc.) you have successfully created something modular, in my opinion. Example: I need to calculate the area of a triangle. For this I will need a height, ...


2

An additional disadvantage of header files is that the program depends on the order of their inclusion, and the programmer has to take extra steps to ensure correctness. That, coupled with C's macro system (inherited by C++), leads to many pitfalls and confusing situations. To illustrate, if one header defined some symbol using macros for its use, and ...


2

You are talking about performance, which is essentially an Non Functional Requirement. My view is that most of these are for the whole system. A user is never going to say "I want the WidgetListCollection class to return a widget within 20 nanoseconds" -- he might say "I want a list of all products on my screen within 1.5 seconds". The only way to ...


2

reinterpret_cast does what the old-style, now-deprecated casts in C used to do: it disables the type checks normally performed by the language to prevent you from applying operations defined for one type to values of another type. In terms of machine code, it's essentially a no-op - in fact it does less than nothing: it instructs the compiler to suspend ...


2

It depends on the library writer, some write C, some write C++ and then add a C interface when required. You'd have to ask them :-) I do think it would be nice to get a standard C++ ABI so interacting with C++ binaries would be much easier, and more natural, but so far the C ABI is the only one we've practically got (unless you count Microsoft's attempts at ...


2

My few cents... Since you want to maintain compatibility with C, I would say - neither. C does not have a "reference" concept and it also does not allow for passing array by reference. Furthermore, C++ has name mangling so unless the function is declared with C-linkage, it would not be possible to call it from a C code in a straightforward way. Therefore, ...


2

It depends on the intended lifetime and ownership of your objects. To construct an object of type C you need an object of type D. Shall this D object have the same lifetime as the C object? Then it makes sense to construct the D object at the same scope where the C object is constructed. Shall the D object live longer than C? Then you should construct it ...


2

c. This is the definition of inline functions, yes. That is not completely right. Despite its name, the keyword inline does not guarantee that a function will be inlined. The only thing that you can be sure of is that the compiler will not complain if it sees the definition of an inline function multiple times. So, strictly speaking, option C is ...


2

The fact that C++11 offers a range of smart-pointer classes to make life easier with memory management does not mean that you can't or shouldn't use raw pointers where that is appropriate. The committee that maintains the C++ standard is very reluctant to remove existing features from the language and more so if it is a widely used and useful feature like ...



Only top voted, non community-wiki answers of a minimum length are eligible