Tag Info

Hot answers tagged

45

It's a redesign but you can prevent misuse of many APIs but not having available any method that shouldn't be called. For example, instead of first you init, then you start, then you stop Your constructor inits an object that can be started and start creates a session that can be stopped. Of course if you have a restriction to one session at a time you ...


18

You can have the startup method return an object that is a required parameter to the configuration: Resource *MyModule::GetResource(); MySession *MyModule::Startup(); void Resource::Configure(MySession *session); Even if your MySession is just an empty struct, this will enforce through type safety that no Configure() method can be called before the ...


13

The point is that this is an implicit formal parameter (containing the address of the object whose method you are calling). It is not a local variable. Look at the generated code of your program. I compiled (on Linux/Debian/Sid/x86-64 with GCC 4.9.1) your example arman.cc with gcc -O1 -fverbose-asm -S arman.cc and got the function main below ...


11

The pointer this is not stored anywhere in relation to the class instance (because if you already have the class instance, getting its pointer is trivial!). Rather it behaves like a hidden argument that is always implicitly passed into member functions such as resize. (In some languages like Python this is passed explicitly.) Note that the standard says ...


11

The second code is conceptually the same as providing public access to the member variable. It also has the exact same drawbacks: You can't change your mind on how a person's name will be stored and you can't control how the property will be used. You can't change the implementation, because to return a reference, you need an actual std::string object to ...


8

While you can include .cpp files as you mentioned, this is a bad idea. As you mentioned, declarations belong in header files. These cause no problems when included in multiple compilation units because they do not include implementations. Including a the definition of a function or class member multiple times will normally cause a problem (but not always) ...


8

Rather than debating over the details of how to give the outside world access to the person's name, you should (IMO) be thinking about how to design the class so the outside world doesn't need access to something internal to the class. If you want to support printing out the person's name, consider overloading operator<< for the class, and provide ...


8

One of the most popular design pattern here on PO is the strategy pattern. And yes, if you build some example code around this pattern, you can demonstrate all the SOLID principles: S = is fulfilled when each strategy subclass is only responsible for exactly one task, and the "context" class does not take responsibilities which belong into the strategy ...


8

When you want to represent distinct concepts, you should create separate types. Yes, this may come with some boilerplate for operator overloading, but having distinct types may offer significant advantages down the line. std::vector is a generic dynamic array, not a mathematical vector (despite borrowing its name). So yes, you should create a separate type. ...


7

UPDATE - this answer, though it seemed to make sense to me and others, turns out to be largely wrong (and sufficiently wrong regarding the intent, as to be effectively just plain wrong). Since (as pointed out in a comment by AProgrammer) it's not permitted to use UCS outside of string constants when the same character could be represented normally in the ...


7

Building on the Answer of Cashcow - why do you have to present a new Object to the caller, when you can just present a new Interface ? Rebrand-Pattern: class IStartable { public: virtual IRunnable start() = 0; }; class IRunnable { public: virtual ITerminateable run() = 0; }; class ITerminateable { public: virtual void ...


6

Read more on the role of the C and C++ preprocessor, which is conceptually the first "phase" of the C or C++ compiler (historically it was a separate program /lib/cpp; now, for performance reasons it is integrated inside the compiler proper cc1 or cc1plus). Read in particular the documentation of the GNU cpp preprocessor. So in practice the compiler ...


6

GCC switched to hand-written parsing because error messages are more meaningful when using recursive descent techniques, as I explained here. Also, C++ is becoming such a (syntactically) complex language to parse that using parser generators is not worthwhile for it. At last, the bulk of the work of a real compiler is not parsing, it is optimizing. GCC ...


5

The for loop is not a function, the for_each is http://en.cppreference.com/w/cpp/algorithm/for_each which takes , as parameter separator. for is a statement, according to the C++ standard ยง6.5.3. You can look at a for as a set of actions being performed, like this: for(initialization; condition; expression) at which point they really aren't different ...


5

C has no specific string concatenation operator (+) like C# and Java. In C# or Java, when the compiler sees "a" + "b" it can compile the code exactly as if "ab" were written in the source code. In C, however, there is no similarly easy syntax for describing concatenation of strings that the compiler can recognise and pre-calculate. So the designers of ...


5

Frama-C's value analysis, a static analyzer the purported goal of which is to find all undefined behaviors in a C program, considers the assignment const int b = a; as okay. This is a deliberate design decision in order to allow memcpy() (typically implemented as a loop over unsigned char elements of a virtual array, and that the C standard arguably allows ...


5

Undefined behavior ultimately means the behavior is non-deterministic. Programmers who are unaware that they are writing non-deterministic code are just bad ignorant programmers. This site aims to make programmers better (and less ignorant). Writing a correct program in the face of non-deterministic behavior is not impossible. However, it is a specialized ...


4

First of all, Ford, Honda and Audi probably should not be classes derived from Car. Derivation should reflect differences in behavior on an abstract level. In this case, it probably makes more sense to simply have a Car object with a field identifying the manufacturer. The Car owns an engine--but in a typical case, you don't need a different class for each ...


4

Update based on what the question is actually asking As a compiler writer, your diagnosis job is simple (at least conceptually): You need to emit at least one diagnostic message for Code that contains a violation of a diagnosable rule or Code that uses a "conditionally supported" construct that your implementation has opted not to implement. The C++ ...


4

I think the problem is that your state machine class is too specific for the protocol. If it was generic, the table of actions would be provided by the protocol class to the state machine class. Those actions would just be std::function<void(ServiceClass*). Clearly the protocol class has access to the service class, as the protocol defines which service ...


3

You'll want to redraw the whole thing. Keeping track of which parts are still the same as the unsized image is going to be computationally more difficult as simply redrawing the whole lot, especially considering that in general usage you're going to have a dialog full of controls, not full of whitespace. Even in the case of text, the text displayed will be ...


3

You are doing it correctly. The thing is called a mixin and is rather common. Quick search shows e.g. What are Mixins (as a concept) or What is C++ Mixin-Style?. They are a case of the Curiously Recurring Template Pattern, name of which even comes from the fact that it is, well, recurring. In your case actually using full CRTP would let you avoid the ...


3

I take "sets of files" in this context as meaning named files stored in a filesystem on some kind of random access storage, and I take "present to the programmer" to imply that this filesystem should make its directory information (filenames) visible to the user. Early computers presented source code to the compiler in the form of cards, paper tape or ...


3

The original C language was designed in 1969-1972 when computing was still dominated by the 80 column punched card. Its designers used 80 column devices such as the ASR-33 Teletype. These devices did not automatically wrap text, so there was a real incentive to keep source code within 80 columns. Fortran and Cobol had explicit continuation mechanisms to do ...


3

Deriving from a C++ standard container should look really weird, because they were never designed to be used as a base class. The biggest source of problems is if your 'extended' container needs to override behavior of the underlying container (including the destructor!), or if your 'extended' container has additional constraints that don't exist for the ...


3

As with any question of naming, this is more about consistency in an arbitrary choice than objective measures. However, there is precedent toward specific conventions. The Google C++ Style Guide says nothing about type parameters, but since they constitute type names, the usual rules apply: Type names start with a capital letter and have a capital letter ...


3

"Undefined Behavior" is shorthand for "This behavior is not deterministic; not only will it probably behave differently in different compilers or hardware platforms, it may even behave differently in different versions of the same compiler." Most programmers would consider this an undesirable characteristic, especially since C and C++ are standards-based ...


2

As Basile hints in a comment, there are other ways to store source code than in text files. VisualAge had the option to store it in a DB/2 database (worked with it, but never looked at the database) for example. Borland's C++ Builder stored some code in binary format (especially the code for the visual representation of screens) rather than text files. This ...


2

First, you cannot write entirely an operating system kernel in C, because by definition an OS is managing physical hardware resources, and some of them (at least on common hardware like x86) are not accessible in C (e.g. MMU, I/O ports, ...). So you'll need a little assembly. (perhaps most of them can be asm instructions inside C functions). Then, look at ...


2

Use a builder-pattern. Have an object which has methods for all the operations you mentioned above. However, it doesn't perform these operations right away. It just remembers each operation for later. Because the operations aren't executed right away, the order in which you pass them to the builder doesn't matter. After you defined all the operations on ...



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