how to implement Interfaces in C++?

C++ has no built-in concepts of interfaces. You can implement it using abstract classes which contains only pure virtual functions. Since it allows multiple inheritance, you can inherit this class to create another class which will then contain this interface (I mean, object interface 🙂 ) in it. An example would be something like this … Read more

When to use reinterpret_cast?

The C++ standard guarantees the following: static_casting a pointer to and from void* preserves the address. That is, in the following, a, b and c all point to the same address: reinterpret_cast only guarantees that if you cast a pointer to a different type, and then reinterpret_cast it back to the original type, you get the original value. So in the following: a and c contain the same value, … Read more

Simple dictionary in C++

If you are into optimization, and assuming the input is always one of the four characters, the function below might be worth a try as a replacement for the map: It works based on the fact that you are dealing with two symmetric pairs. The conditional works to tell apart the A/T pair from the … Read more

Very basic C++ program issue – Invalid operands to binary expression

You’ve fallen victim to the most vexing parse, which means thisisanumber is being treated as a function. Take out the parentheses and you should be fine: Also consider making it a bit more readable, such as thisIsANumber. If you ever need to know it, thisIsANumber uses the camel-case naming convention.

rand() between 0 and 1

This is entirely implementation specific, but it appears that in the C++ environment you’re working in, RAND_MAX is equal to INT_MAX. Because of this, RAND_MAX + 1 exhibits undefined (overflow) behavior, and becomes INT_MIN. While your initial statement was dividing (random # between 0 and INT_MAX)/(INT_MAX) and generating a value 0 <= r < 1, … Read more

The Definitive C++ Book Guide and List

Beginner Introductory, no previous programming experience C++ Primer * (Stanley Lippman, JosĂ©e Lajoie, and Barbara E. Moo) (updated for C++11) Coming at 1k pages, this is a very thorough introduction into C++ that covers just about everything in the language in a very accessible format and in great detail. The fifth edition (released August 16, 2012) … Read more

How do I include the string header?

You want to include <string> and use std::string: But what you really need to do is get an introductory level book. You aren’t going to learn properly any other way, certainly not scrapping for information online.

‘was not declared in this scope’ error

The scope of a variable is always the block it is inside. For example if you do something like The solution is to define y outside of the if blocks In your program you have to move the definition of y and c out of the if blocks into the higher scope. Your Function then … Read more

Usage and Syntax of std::function

std::function is a type erasure object. That means it erases the details of how some operations happen, and provides a uniform run time interface to them. For std::function, the primary1 operations are copy/move, destruction, and ‘invocation’ with operator() — the ‘function like call operator’. In less abstruse English, it means that std::function can contain almost any object that acts like a function … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)