Tag Info

Hot answers tagged

79

Because there are no functions shared by all objects. There's nothing to put in this interface that would make sense for all classes.


51

Because what would that object have for functionality? In java all the Base class has is a toString, a hashCode equality and a monitor+condition variable. ToString is only useful for debugging. hashCode is only useful if you want to store it in a hash-based collection (the preference in C++ is for std::vector and plain unordered lists). equality without a ...


29

Your measurement is probably flawed. If you're just running both applications and measure the time from start to end in the terminal, you are not measuring execution time, but also startup time. For Java application, this means that those 170 ms. also contain JIT compilation. Moreover, 50 ms. vs. 170 ms. is not representative. Doing a task in a loop and ...


22

Whenever you build tall inheritance hierarchies of objects you tend to run into the problem of the Fragile Base Class (Wikipedia.). Having many small separate (distinct, isolated) inheritance hierarchies reduces the chances of running into this problem. Making all of your objects part of a single humongous inheritance hierarchy practically guarantees ...


20

Because: You shouldn't pay for what you don't use. These functions make less sense in a value-based type system than in a reference-based type system. Implementing any sort of virtual function introduces a virtual-table, which requires per-object space overhead that is neither necessary nor desired in many (most?) situations. Implementing toString ...


14

There is no one answer to this, largely because it's not always true. In fact, arguably it's never really true. You can't measure the speed of a language, only the speed of some particular implementation (and rarely even of the implementation as a whole, only of its execution speed on some particular piece(s) of code. Java code can be slower than C++ ...


13

At least for free software on Linux, you usually use some builder like make. You could use some other builder program, like scons or omake For some (mostly historical) reasons, the Makefile may be generated by utilities like autoconf or cmake; these generators also deal with configuration issues (e.g. they disable some features of the software if a ...


13

First, a Makefile for make is really useful when you build a program from several translation units (i.e. several *.c or *.cc files which are #include-ing some other header files) which are linked together (it is not very useful for a single source file tiny program). It organizes the various compilation steps of the translation units (and avoid running ...


11

Having one root object limits what you can do and what the compiler can do, without much payoff. A common root class makes it possible to create containers-of-anything and extract what they are with a dynamic_cast, but if you need containers-of-anything then something akin to boost::any can do it without a common root class. And boost::any also supports ...


10

A "raw" pointer is unmanaged. That is, the following line: SomeKindOfObject *someKindOfObject = new SomeKindOfObject(); ... will leak memory if an accompanying delete is not executed at the proper time. auto_ptr In order to minimize these cases, std::auto_ptr<> was introduced. Due to the limitations of C++ prior to the 2011 standard, however, it's ...


8

There are many good answers above, and the clear fact that anything you would do with a base-class-of-all-objects can be done better in other ways as shown by @ratchetfreak's answer and the comments on it is very important, but there is another reason, which is to avoid creating inheritance diamonds when multiple inheritance is used. If you had any ...


5

I'm going to suggest another reason that comes from Java. Because you can't create a base-class for everything at least not without a bunch of boiler-plate. You may be able get away with it for your own classes - but you'll probably find that you end up duplicating a lot of code. E.g. "I can't use std::vector here since it doesn't implement IObject - I'd ...


5

In fact Microsofts early C++ compilers and libraries (I know about Visual C++, 16 bit) had such a class named CObject. However you have to know that at that time "templates" were not supported by this simple C++ compiler so classes like std::vector<class T> were not possible. Instead a "vector" implementation could only handle one type of class so ...


5

Simply. Don't mix code built with debug with code built without. If that class is exported by library you link against, you either need to: Have debug and release version of the library. That's what one always does on Windows where even the standard runtime has such two versions and they are incompatible, so there is no other way in most cases. This is ...


4

The heart of creating abstractions is to give things a good name, which makes clear what a specific data type represents. And that's what you are doing here, no less, no more. Creating the right abstractions is what makes actually the difference between good code and bad code, so yes, assumed these abstractions serve you well, that is good practice.


4

Although const as a language feature does not guarantee thread safetyness, lot of the constant functions, by their nature, happens to be threadsafe as they typically just read some constant variables, calculate a result and give it back. This is the bitwise constant mentioned in the video. The cases where internal synchronization is needed (I beleive) are in ...


4

The solution is a remote procedure call. The callee must run in its own process space. How exactly you achieve that is a fairly minor detail. I'd strongly suggest not inventing the wheel yourself. Not that you'd need this after you've "formally proven its correctness". Correct code doesn't cause segfaults.


4

No, the access to the functions is restricted to base class functions only. But there's easy way to fix this problem, by providing pure virtual function in the base class: class Polygon { public: virtual int area()=0; }; Then it'll just work fine.


3

If you use a modern IDE(*) you will probably never have to look at a makefile in your life, even though the IDE generally will generate one for you on the fly. Makefiles were a needed skill when I started out 30+ years ago. I rarely see them nowadays (and, if you do need one, there are makefile generators). In short, don't learn a skill until you need ...


3

Use the following rules: For any non-static method of a class which a) does not access private fields directly and b) only calls public methods, move the method to a static helper class and turn it into an extension method. Any public static method can also be moved to a helper class. By convention, a helper is a new static class with the same name as ...


3

This depends on what "mathematically well defined" means. All of your functions are well defined in the sense of having a unique definition. However, multiplication and division are problematic, since they are not guaranteeing (b * n) * m == b * (n * m) nor (b * n) / m == b * (n / m) where b is a bearing and n is a numeric value, and that is what you ...


3

Do you have a specific need for make? Tools like make are all about automation - being able to repeat a process flawlessly, time after time, as efficiently as possible. Check out this thread on github regarding a JavaScript development framework and the potential choice of using make as the build tool: ...


3

Maintain a map in which you accumulate function names and associate them with unique integer ids. You should be able to optimize the performance of the map by taking into consideration the fact that the function name produced by the compiler for a certain function will always have the same address. (There will be only one string constant per function name ...


3

What Python does is writing the encoding out in the source code, specifically a specially-formatted comment. See PEP 263. It doesn't need to be a comment, but spelling out the encoding is pretty much the only sensible approach. "Guessing" encoding from raw bytes does not work very well, and is far too magical for the tastes of most programmers. It would be ...


3

At the risk of sounding silly: Yes, there is a C++ feature for this, it looks like this: if (DEBUG) { // Your debugging stuff hereā€¦ } If DEBUG is a compile-time constant (I think using a macro is reasonable but not required in this case), the compiler will almost certainly generate no code (not even a branch) for the debugging stuff if debug is ...


2

I can speak as the main architect and implementor of GCC MELT, a Lispy domain specific language to customize the GCC compiler, translated to C++ code suitable for GCC internals. MELT has some tricks (devices like defprimitive, defciterator definitions) to formalize the generation of part of the generated C++ code. The generated C++ (mostly C-like) code is ...


2

There are some language translators whose intended usage is to take a piece of code and translate it, once, into a form which a human will be able to maintain from then on; for such translators, the human-readability of the translated code is extremely important. More often, however, the intended use of a code translator is to produce a file that will be ...


2

After much debate, we found a method that we like, but it involved other substantial changes to the project. We turned on pre-compiled headers. Within the pre-compiled header, we first include any and all system headers (and other third-party library headers) that are not compliant with banned.h, and then we include banned.h. This has a lot of properties ...


2

At our company, the dividing line between approach one and approach two comes down to the external dependencies of the component(s). For instance, we have hundreds of "core" components with no dependencies whatsoever, and those are grouped into only a single library. On the other hand, the one component that we use to talk to a specific type of database gets ...


2

Object pointers are not primitives in the sense that strings or ints are. A PersonName* is not a Genus* and its not a ProductDecription*. Unsurprisingly, the equivalent to primitives for pointers is pointers to primitives. Adding an extra level of indirection won't change that either way.



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