New answers tagged

1

@SebastianRedl already gave the simple, direct answers, but some extra explanation might be useful. TL;DR = there's a style rule to keep constructors simple, there's reasons for it, but those reasons mostly relate to a historic (or simply bad) style of coding. Handling of exceptions in constructors is well defined, and destructors will still be called for ...


3

You could create a static method that performs the calculation and returns either an object in case of success or not case of failure. Depending on how this construction of the object is done, it might be better to create another object that allows construction of objects in a non static method. Calling a constructor indirectly is often referred to as a "...


4

1) Yes, though some coding standards may prohibit it. 2) Yes. The recommended way is to throw an exception. Alternatively, you can store the error information inside the object and provide methods to access this information. 3) No.


2

The pointer to the parent is a valid approach. But it's a very specific solution that will be difficult to extend. A nicer approach could be to implement the observer design pattern. There are plenty of examples how to do it; to start with the GoF reference book, or here. This is in fact a general clean form of your option 2 (parent is observer, child is ...


1

Try something object oriented. Pass the child an interface that the parent implements. Since this does create a cyclic data structure, you just have to free things in the right order; something we don't worry about in other languages... You may find that the child only needs access to the parent (interface) at certain well-known times, so maybe you ...


5

It probably depends on how tightly you want your objects to be coupled. But for faster development and more readable code I think the method 1 is preferable. Just store the reference to parent as a weak pointer (assuming you use modern C++). You could do some pattern in which the parent would register a function within the child (in this case prefer ...


3

The existing rendering capabilities of the hardware may not always suit your needs, so sometimes you just have to paint the pixels yourself. So, your question boils down to "why allow direct VRAM access when you can always paint the pixels in RAM and then have them Blted from RAM to VRAM?" The answer is that back in the nineties we did not have the vast ...


0

For what it's worth, std::wstring does nothing what you'd expect (it's UCS-2, not UTF-16, they are different; the former cannot express characters outside the basic multilingual plane, including Emoji such as U+1F44E THUMBS DOWN SIGN ๐Ÿ‘Ž). For information on Unicode handling in C++11 (and later), see this overview. TL;DR you probably really want something ...


0

Your doing what is called 'downcasting'. You can do this in C++, however, it's not recommended (imho) because of the various baggage that C++ has that Java doesn't. Direct downcasting (like the example above) will 'slice' off derived data when cast to the base class. To avoid this, you need to use pointers. RTTI. This is a feature where you can determine ...


0

... copy the content of raw into m_rawData or it will copy the actual reference of the raw and m_rawData will become invalid when the otherClass::someOtherMethod returns? this->m_rawData =raw; will likely trigger the copy assignment operator on QByteArray. This is because you didn't initialize m_rawData in the initializer block and you therefore ...


0

I'm not a fan of reverse DNS java packaging. There's a few reasons for it. Names of companies change (future IP problem?) Pointless directories like 'com' that never hold anything When I do C++ development I usually do something like this: ::application::package::subpackage src/application/package/file.h #ifndef SRC_APPLICATION_PACKAGE_FILE_H_ #...


-1

Actually it's a bit hard to answer this question. We don't know the platform and CPU architecture that you are using and it would be better if you would let us know why you want to do this. Damn Small Linux for sure supports multithreading. If it wasn't then your mouse would froze while loading apps. As it was suggested compile Linux kernel or use Ubuntu ...


1

As a commenter said, look up 'Audio Resampling'. Also, maybe buy the book called 'The Art of Digital Audio' or similar. Basically, what you are doing is linear interpolation, should work. But resampling at a much higher rate should allow you to pick samples (from the denser set) close to the sample points you need for e.g 2.5 speedup. Analyzing the error ...


1

I have little mathematical background but I do see an approach that might work. I would assign a value to each coordinate in the grid that is determined by the amount and closeness of dotted neighbors. Then apply a threshold: only keep the coordinates that exceed the threshold value. Those will form the result path.


3

Yes. The Windows NT kernel API (which is traditionally accessed by using the functions defined in ntdll.dll) can be accessed directly by use of the int 2e instruction. However this is not a supported way of using the system, and details of the implementation (including function codes) are likely to change between Windows versions. The basic approach is: ...


0

The key to this is to extend the range of the counter that you are using to track time. Just keep a "number of overflows" 32bit counter that you increment each time the millis() value wraps, and use your counter and the millis() value to form a 64-bit counter -- that will give you a wrap time of 50 * 2^32 (or about 200 billion) days -- probably longer than ...


3

What is the difference between a normal int and a 32-bit int? As David Arno points out, you haven't said what you mean by a "normal" int. So is what he saying is when you use bitwise operators in C++ it would only cost 1 bit of memory? No, he's saying that if you want to treat integers as collections of bits, you can do so with bitwise operators. ...


-1

So whenever you initialize a variable with a certain datatype (int, float, long, double, etc.) it preserves a certain number of bits for it in the memory. A basic difference between a 16 and 32-bit floatpoint-number is the range of possible values respectively the count of digits. Examples: In case of a 16 bit number it is a range from 3,1ยท10^โˆ’5 to 6,6ยท10^4....


2

Your title and question body are asking two different questions. What is the difference between a normal int and a 32-bit int? There's no such thing as a "normal" int. The size of int in C++ is platform and compiler dependent. So is what he saying is when you use bitwise operators in C++ it would only cost 1 bit of memory? The first thing to note ...


14

The book is talking about what is commonly known as bitfields, and their use is often more memory efficient on most platforms, and especially so in serialization or communication contexts. A boolean requires its own address to be usable by the compiler. This means that while, practically, we only need one bit to represent a true (1) or false (0) condition, ...


1

In C++, an int is at least 16 bits. A long (which I have seen referred to as a 32-bit int) is at least 32 bits. When you declare and initialize something, like long i = 0, you actually initialize at least 32 bits of memory. If you use bitwise operations, you can access each bit individually. For example, if you have 32 traits that have two options (meaning ...


1

If you insist on using millis() to index the array, then you either have to make the array size a factor of the max millis() value, or you have to reset the millis() value to zero when your array wraps. But these seem like bad ideas and I don't understand why you are using millis() to index the array. Think of the array as a circular (ring) buffer. Create ...


1

If the integrate function is only invoked once per object, what you have is a trajector builder, only it doesn't expose the trajectory as a first-class entity. So, you might make a first-class notion of the concept of a trajectory, and have your integrate function be a builder to construct the trajectory entity. Once you have a first-class trajectory ...


7

If you want a TrajectoryPrinter ask for a TrajectoryPrinter. Right now you're only asking for doubles. Something will need to build Oscillator. Something will need to build TrajectoryPrinter. I don't recommend that Oscillator build or even find TrajectoryPrinter. Oscillator shouldn't know TrajectoryPrinter as anything except as something it can call a ...


3

There have been languages that, as you suggest, provide only a single datatype and then have operations where the expected encoding of the data is provided as part of the operation, rather than being determined by the type of the variable as it is in most modern languages. The best known of these was probably B, the predecessor of C (in fact, C can be ...


1

The text you quoted is a bit sloppy (if for no other reason than && and || are not bitwise operators). In the expression a && b, the expression b will be evaluated if and only if the expression a evaluates to true. If a evaluates to false, then a && b will evaluate to false regardless of the value of b, so b isn't evaluated at all: ...


0

The text you quoted would convince me never to let anyone buy this book. && and || are not bitwise operators, they are logical operators. & and | are bitwise operators, and they are not short-circuited. Short-circuit operations are obviously different for && and ||. If you evaluate condition1 && condition2, then if condition1 ...


3

Under the hood, it might be implemented differently, but the visible effect of MyClass(const QByteArray & raw){ this->m_rawData =raw; } will be that the contents of raw get copied into m_rawData and will survive after raw has been destructed. This works because m_rawData is declared as being a value of type QByteArray. If it would have ...


5

Short circuit evaluation isn't about true or false. It's about not evaluating part of an expression if you can predict the result without it. Since false && whatever() returns false regardless of whatever() you don't need to call whatever(). Your code doesn't demonstrate this at all. You simply can't demonstrate this with true and false. You ...


0

When the compiler sees the if statement: if(condition1 && condition2) It says to itself, condition 1 is true, so now my users wants to know if condition2 is also true.....hmmmm..... condition2 is false, therefore this statement is false If you want to see a short circuit then refactor your if state to be: if(condition2 && ...


0

The short circuit evaluation only works in cases where you can guarantee after evaluating the first operand that the end result won't be influenced by the second operand. So for &&, if the first operand evaluates to true, you can't logically guarantee the result and have to evaluate the second operand (remember: && only returns true if both ...


-1

if (condition1 && condition2) Is same as if (condition1 == true && condition2 == true) Since your code shows : bool condition1 = true; bool condition2 = false; If statement will not be true,because condition2 is false


8

Let's forget for the moment that processors have specific hardware for manipulating byte sequences of a particular size. Let's forget for the moment that processors have specific hardware for operating on specific interpretations of byte sequences (floating-point registers, SEE registers, etc). What good does this abstraction do from a user perspective? ...


1

You are correct that numeric types are just patterns of bits. But processors are optimized for certain operations on certain patterns of bits. For example x86 processors have registers of sizes 8, 16 and 32 bit, arithmetic operators corresponding to these sizes, and memory is addressed in chunks of 8 bits, known as bytes. There is no support for 19-bit ...


0

This will be from a kind of OOP and/or enterprisey/application point of view and might not be applicable in certain fields/domains, but I kind of wanna bring up the concept of primitive obsession. It IS a good idea to use different data types for different kinds of information in your application. However, it's probably NOT good idea to use the built-in ...


8

So why did all those programming languages decided to provide multiple built-in data types Because the built-in types like int, float, byte and char are used in almost all use cases โ€“ and it turns out that standards are a convenience for everyone. If everyone used their own variant, writing code that uses libraries with different types, exchanging data ...


11

Because processors have operations specifically for ints, and specifically for floats. The compiler has to know what operation to target. And I mean, even if you had adding for a series of bits, adding 0110 and 0001 have very different meanings if 0001 is treated as a float rather than an int - not to mention actual operations required to calculate it. And ...


3

A major difference between C++ programs and Javascript scripts is that a C++ program typically runs for a much longer time than a Javascript script. A C++ program with a GUI executes continuously while you are working with the program. A Javascript script on the other hand only executes for a short time to respond to an event and then it ends (even if it ...


1

The JavaScript event handler is executed by the browser engine which also handles rendering of CSS/HTML, user interaction, network traffic and so on. When the engine executes an event handler, uncaught exceptions in the JavaScript code terminates the execution of the event handler code, but does not terminate the browser engine, since this would mean a ...


2

That's not true at all. The JS exception is not uncaught in the slightest. It's simply caught by the browser. A C++ UI library can trivially produce the same effect by calling the onClick handler inside a try/catch. The difference in behaviour has nothing to do with language - it's all library.


2

(I'm writing this as an answer since it's too long for a comment) Here's a sample layout for using virtual methods to solve your problem: class MoveObjects { public: virtual void doSomething() = 0; // declare a method name // other members of MoveObjects }; class Item : public MoveObjects { public: virtual void doSomething() { // ...


5

In most cases, there won't be a noticeable difference (unless you have the obvious case with method parameters named the same as member variables). But beware of templates! template <typename T> struct Base { int i; }; template <typename T> struct Derived : public Base<T> { int get_i() { return i; } }; This will cause a ...


0

If a class has a member variable named data and one of its (non-static) member functions also has a local variable (or parameter) named data, then data is the local variable and this->data is the member variable. class Point { double x, y; // ... }; // Constructor with parameters named the same as the member vars Point::Point(double x, double y) { ...


0

No surprises. They are functionally identical. In order for the compiler to locate the member, the compiler must use the hidden this parameter. Therefore there is no functional difference between accessing a member with or without using this. The reason to use this is to disambiguate other names that may be in scope.


2

Many other good answers have been written, I just want to add a short point. The traditional answer, especially when the ISO C++ FAQ was written, mainly compares "C++ exception" vs. "C-style return code". A third option, "return some type of composite value, e.g. a struct or union, or nowadays, boost::variant or the (proposed) std::expected, isn't ...


4

The C++ syntax is designed in such a way that a declaration always needs a type to distinguish it from assignment. You simply cannot leave out the type, because that already has a different meaning: int i = 1; // Leaving out the type turns it into assignment: i = 1; int i; // Leaving out the type turns it into evaluation: i; You simply need an explicit ...


4

Yes, apparently there is such an option. I strongly suggest you avoid using it. Fix the code instead. In early versions of C++, the scope of a variable defined in a for loop header extended to the end of the block containing the loop, making the code in your question valid. In modern versions of the language, the scope ends at the end of the loop, making ...


2

The first thing to realize is that the situation you describe (some items are 'never' processed) can only arise if you simply don't have the capacity to handle the volume of requests. If you have the capacity to handle the expected volume of requests over a given time period, you will be able to handle all the requests. This is a tautology. Changing the ...


5

I don't believe such a possibility was ever considered by the C++ committee. Further, although FORTRAN did have a sort-of implicit type system (variables starting with 'I' through 'N' were integers, everything else was real) I can't quite see how such a thing would work in a block-structured language like C++. For example, consider code like this: int f() {...


8

In 1963, Tony Hoare proposed adding implicit type rules to ALGOL. The ALGOL committee boxed his ears, HARD. Requiring variables to be declared explicitly was known, EVEN THEN, shown to reduce errors in programming. Tony mentioned this in his Turing talk, and said it was BEFORE the probably-apocryphal Venus probe FORTRAN story, where a typo in a FORTRAN DO-...



Top 50 recent answers are included