Tag Info

Hot answers tagged

11

If you are starting a new green field project that is at a high enough level (as in you're talking about writing an enterprise Foo rather than a device driver) and has no external dependencies you can probably avoid using pointers entirely. But most development isn't brand new development, it's leveraging the millions of existing lines of C++ code out there ...


8

As humans, we are fallible and prone to making errors. Therefore, we should try to write our programs as safe as possible, and let tools like the compiler or Valgrind help us find problems before they lead to crashes in production. It is clear that it is impossible to avoid pointers at all levels – they provide a mechanism for indirection that is invaluable ...


7

Checking array bounds like you want is implementation specific, because buffer overflow is an example of undefined behavior (and this explains why UB can be really bad). It is also an undecidable problem in general. You can easily show that statically finding (by static program analysis, e.g. of the C++ source code, without actually running the program) ...


6

if (first<second) In this case, there is something different involved here, the operator< from std::basic_string. and was wondering if alphabetical string characters are assigned numerical values for the purpose of comparison. No, not really. You don't assign a number to a character just for comparison. A computer does not know anything about ...


6

The range to iterate over The C++ standard library offers something of a canonical form here, accumulate; template< class InputIt, class T > T accumulate( InputIt first, InputIt last, T init ); It takes a range from first to last (excluding last) and adds each element in the range to the init value as an initial value. It will also infer the return ...


6

First, you have a false dilemma. If your users are not programmers and know only IDL, any other language (custom or existing) will look foreign to them. But that does not mean that they cannot learn something else. In fact, you would say that this is not really a problem: you'll have to write a tutorial and point them to documentation and other materials. ...


6

I may be wrong, but your design seems to be horribly overengineered. To serialize just one Widget, you want to define WidgetReader, WidgetWriter, WidgetDatabaseReader, WidgetDatabaseWriter interfaces which each have implementations for XML, JSON, and binary encodings, and a factory to tie all those classes together. This is problematic for the following ...


5

Your requirements describe Lua. Lua is designed to be embeddable as well as work as a declarative configuration language. Other options I’ve seen or used include Tcl, Python, and JavaScript. All of these languages can be embedded to various degrees of work and tend to be fairly easy for non-programmers (who think they can program) to use.


5

However, in the actual code I'm dealing with, there are quite a few (about 6 or more) members of A that B will require, so this could get unwieldy. Create a Data Transfer class that holds the members you wish to transfer from class A to class B, and then write your method on class B so that it takes an instance of the new class.


5

I don't think you should have syntax highlighting information directly in your text buffer. Instead, I would add additional data structures for the display code. Here's why: Once you're providing functionality like selections etc, you'll probably need an anchor concept (a steady pointer to a specific location in the buffer, even when characters are ...


4

I think these implementations are reasonable and a generally good solution. Adding an appropriate move constructor and move assignment may help deal with your copy concerns - the default should be appropriate with the shared wrapper. Some may argue (or advise) that you do not need to wrap the Standard Library facilities that you use here; whilst this is ...


4

You say you need to use const char *, but not that raw C arrays are a requirement. Therefore, I'd do it like this: std::array<const char *, 50> array; array.fill(".");


4

N0345 Lifetime of temporaries explicitly bound to references, by John Bruns and from September 1993, is what fleshed out the details of lifetime extension. The paper briefly mentions a motivation for the preexisting, simpler form of lifetime extension, which is to avoid copying class objects. It also mentions reliably letting function arguments be ...


4

The answer is really fairly simple: if you want safety, use something that actually provides it--and that's not C, and not raw C-style arrays. Without departing too far from the basic style of C and raw arrays, you can use C++ and an std::vector with [i] replaced by .at(i), and get bounds checking. Using std::vector instead makes most of the problems with ...


4

A function that receives a pointer does not know of the length of the corresponding array. You must pass in as a parameter yourself explicitly: void myFunc(int *yourArray, size_t yourArrayLen) Once you've done that, throwing an error is trivial. Of course, this still leaves the possibility that your caller might give you the wrong length. You can't ...


4

As your API depends on a library that is licensed with the GPL license, the answer to your first question is: No, you can not apply those restrictions to your API. The GPL is a copyleft open-source license. This means that any project that is based upon (or links to) GPL code must be made available under the same license (this is the copyleft nature of the ...


4

You seem to be confused because “runtime” is both a noun and an adjective. Your question asks about the three distinct concepts of the “run-time environment”, the “language runtime”, and “run-time linking”: As a noun, the “run-time environment” of a program, or phrased differently: the “environment of a process”, refers to the (changing) state of the ...


3

Just use shared_ptr directly, with your custom deleter. Maybe typedef it if you prefer. This way you get correct move & copy constructors and assignment operators with no typing. You also get weak_ptr for free, if you want it. Unless your code will add some actual functionality - or at least an interface compatible with some external requirement you ...


3

You have not specified a development environment (IDE) and mentioned in the comments that you want this to be a more conceptual problem, so here is a way to organize this that is not specific to any IDE. I have actually done this before when developing static libraries. First, you create your library project with its own makefile and ensure it compiles ...


3

C++ treats int, const int, int&, and const int& as separate types with ways to convert to/from each type (except to const int or to const int&). If you know what types you have and what types are expected, then given a list of converters, you know if you can make each passed parameter pass as an argument parameter. Since there may also be ...


3

Here's my opinion on the matter: consider whether both variants can actually be used in your case. reference_wrapper is, by design, not default-constructible. That means you will not be able, for example, to call container.resize() when using the reference wrapper. A shared_ptr, on the other hand, is default-constructed to an invalid/NULL state. So, for ...


3

Though there is no "official" definition of a "resource", the term is generally used for anything that your program has to explicitly acquire before use, and possibly release after use, where the act of acquiring and releasing is either expensive or complicated enough that we want to avoid doing it more than we have to. Typically, this includes any file we ...


2

If you really want to tick some people off, use FORTH. You get an interactive control interface (in reverse Polish notation, admittedly) AND a programming (scripting) language. Alternatively, you could dig up one of David Betz's little XLISP or XScheme packages. As I heard it, this is basically what Autodesk used to make AutoLISP for AutoCAD, a few ...


2

In most respects the std::unique_ptr was made to be drop in (but safer) replacement for std::auto_ptr, so there should be very few (if any) code changes required other than (as you ask) directing the code to use either unique_ptr or auto_ptr. There a few ways to do this (and each comes with its own list tradeoffs) below. Given the code sample provided, I ...


2

You could embed some interpreter in your application. If you like Lisp as I do, you should consider embedding GNU guile or librep etc... See also this & that & that answers


2

A problem I can see with your approach is that your preconditions return, instead of throwing an exception. Usually, preconditions throw an exception when violated. Since you are using C++, asserts can be used instead. If you use exceptions/asserts, an additional refactoring can be made easily. Instead of: if (!isConditionValid()) { log("doFoo not ...


2

C++ supports several programming paradigms (including); OO based techniques Generic and template programming Procedural style programming (coming from C) Using any of these techniques where is appropriate is not going to be "overkill". The broader question is more what design or pattern would be appropriate for the problem being solved, given the context ...


2

Yes, performance will be impacted by creating a new connection every time - it's not a zero cost operation. However, is this a significant problem for your application? Only you can answer that after having done some profiling. The answer to this is not to use a singleton though - at some point, your app is almost certainly going to want two simultaneous ...


2

No, they are not avoided per se. Use "raw" pointers as appropriate, with a long list of things where they are not appropriate. To keep this short: Do not use raw pointers to model ownership (avoid naked deleteand possibly also naked new -> make_sharedor make_uniqueare your friends) Do not use raw pointers to "pass around" a conceptual non-owning ...


2

Note: This answer addresses the final part of the question. The bulk of the question has already been nicely addressed in Niall's answer. I am particularly worried about the getData implementation; whether I should return a copy of the shared pointer or a naked pointer and why. Suppose some code calls getData() and saves that pointer in some ...



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