Tag Info

New answers tagged

0

You can do the following: split the code into one class per header file. in the case of templates, the implementation of the class functions can/should reside in the same header file (there is probably no scenario when you will want include the header, but not those definitions). dependencies (your AuxiliaryClassK) should be #included instead of being ...


0

Quoting the link you provide: You may use a using-declaration anywhere in a .cc file, and in functions, methods or classes in .h files. // OK in .cc files. // Must be in a function, method or class in .h files. using ::foo::bar; Google style forbids you using importing namespaces in global context, but allows to do so in local ones. ...


0

they do not recommend that one use the using namespace ornamespace:function` - if I did not misinterpret it. You did. The disrecommendation only applies to the using namespace directive (which is commonly referred to as abusing namespace, not entirely humorously). It is strongly preferred that you use the fully qualified name of a function or object, ...


2

It looks like the code interprets the string of random bits as a binary fraction, converts this to a N-ary fraction, and looks at the first digit of that N-ary fraction to determine the winner. The question then becomes: How many random bits do we need until the first digit of the N-ary fraction is stable? For all examples, I'll use N=3. The 3-ary fraction ...


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 ...


0

Analysis. It has to use some kind of intrusive pointer (specifically user-implemented reference counting), though it could be Boost intrusive_ptr or some other flavor. My personal opinion. I have used Microsoft COM pointers in my projects and it works fine. If your code only has to work on Microsoft Windows, this may be the recommended route as the C ...


1

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 ...


4

3 options make popping en emtpy stack undefined and don't worry about it throw an exception return default constructed T() (which happens to be 0 for the numeric types)


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 ...


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 ...


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, ...


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 ...


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 ...


0

You're not paranoid. My first professional task as a C++ programmer resulted in slicing and crashing. I know of others. There are not a lot of good solutions for this. Given your compiler constraints, option 2 is the best. Instead of making a macro, which your new programmers will view as weird and mysterious, I would suggest a script or tool for auto ...


1

As long as the functionality you'd like to test doesn't invoke any pure virtual methods and isn't overridden in subclasses, I'd stick with your bullet point number 1 - creating a test-specific subclass. You could name it Sub<ClassName> or any generic name reflecting that the subclass itself is not important. Now if what you want to test can be altered ...


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 ...


1

You could do the following: create an abstract base class with only pure virtual methods, for example AmazingObjectInterface; add a subclass called BasicAmazingObject that defines the non-virtual methods that you need to test; finally, your 2 original subclasses becomes subclasses of BasicAmazingObject. This allows you to separate what varies and test ...


0

Meeting the requirements, including performance requirements, trumps all other design considerations -- language, maintainability, readability, scalability, time to implement, coupling, etc. Either it works or it doesn't -- if you have to crank out hand tuned assembly in order to make it work, then that's what you have to do. Only when there are tradeoffs ...


0

In line with RAII, I would certainly make sure that you had a reference-counting smart pointer (shared_ptr) instead of a raw pointer. Besides that, there's nothing inherently wrong with what you've described. However, as many people have noted already, it is impossible to give an accurate answer without two things: Code. Are you turning your code into an ...


5

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 ...


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 ...


0

It is difficult to know precisely why the code is slow without being able to debug it, however unless your 2d array is extremely large, it is likely not how you're accessing it that slows it down but rather something else that is responsible for the delay. I noticed several things which may help: You're using an array of indices to determine access order ...


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 ...


0

Your wrapper may have some logic inside to deal with the template. And basically all STLs are templates by themselves. This doesn't shock me. Imagine you want to have an array of objects "Cars" and other of objects "Bicycles". Same structure but different data manipulation.


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 ...


0

I've preferred to derive from the STL to add functionality and also hide the templating. So rather than use the vector as a member variable, I would derive so that my CustomVector is still a vector, and all the methods that are available on a vector, are also available on my CustomVector. Also, any method that can work off a Vector will also work on my ...


1

I wouldn't, I'd rather see a class that is a vector if its basically just a vector with 1 or 2 custom functions. For these custom functions, I'd say its better to make functions that operator on the type, for example all the methods in . You might find this approach is a little more generic and so encourages reuse across your wrapper classes. However, if ...


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 ...


1

The purpose of reinterpet_cast is to allow conversions between unrelated types without losing information, and that can round trip. That usually means 'reinterpreting' a given bit pattern with a new meaning. The section of the C++ standard that deals with it more than 1 page, see n3797 S5.2.10. The format is reinterpret_cast<T>(v) where T is a type ...


1

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 ...


1

After reading some of the answers I think that, yes, it's time for me to look for down-votes. Does << represent bit shift with std::ostream ? Does + represent an arithmetic addition with std::string? Does * represent dereference in boost::spirit? Despite this well established fact, there is still people claiming that overloading operator makes ...


0

My understanding of adaptive Huffman coding is that its implementation requires a weighted tree, and then weights are being operated on, so a standard binary tree, or binary search tree, or a heap, cannot be used 'as is'. In fact, a BST doesn't seem very appropriate, whereas a heap might be a good start, given that (in Vitter's algorithm at least) nodes must ...


0

The current situation is that the quantity of portable C you can write is severely limited, and its usefulness is marginal, unless you make some very significant choices. Most vendors (including ourselves) are picking a single platform and hoping that's enough. Those with more resources are replicating the app on other platforms. For most situations, it's ...


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, ...


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 ...


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, ...


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 ...


1

How do people/companies handle this type of test? There are many kind of testing, but your interests are unit and functional : unit tests - needs to be very fast, in order to be execute on every change in the code. Their purpose is to test modules. functional tests - they are similar to unit tests, except they do not need to be fast. They are ...


1

Say I have a class A that creates class B. Class B depends on class C and class C depends on class D. Who should be responsible for creating class D? You're jumping steps. Consider a set of conventions optimized for loose coupling and exception safety. The rules go like this: R1: if A contains a B, then the constructor of A receives a fully ...


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 ...


0

If you're using a DI Container, the DI Container is responsible for figuring out what the needed dependencies are. If you're injecting dependencies through the class constructor, the caller of the constructor is responsible. So in your original example of A --> B --> C --> D A would be responsible for handing the necessary dependencies to B, ...


1

Who should be responsible for creating class D? Every time the question like that pops up, it indicates a dependency to inject. The whole idea is to "delegate" responsibility outside of the object that seems to be having a problem figuring how to handle it. Using your example, since class A doesn't seem to possess sufficient "knowledge" to figure how ...


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++) { ...


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 ...


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 ...


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; } }; ...


1

Why would it be a bad programming practice to use more error prone code in prototype situations, if refactoring makes it more robust afterward? When writing quick and dirty with the mind of fixing later there is the danger of forgetting something that you need to fix. How would can program written in C++ be like it was written in Java? What makes it ...


1

Is it ok to make the cursor attribute mutable ? No. const is a promise that calling methods on the object won't change its state in an observable way. I use the term observable loosely here; it doesn't need to be observable from within the program. If a Sound object remembers its position in the playback, it has observable mutable state, because calling ...


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.



Top 50 recent answers are included