586 Answered Questions for the topic C++

C++

10/07/19

push_back vs emplace_back?

I'm a bit confused regarding the difference between `push_back` and `emplace_back`. void emplace_back(Type&& _Val); void push_back(const Type& _Val); void... more
C++

10/06/19

std::string formatting like sprintf?

I have to format [`std::string`](http://en.cppreference.com/w/cpp/string/basic_string) with [`sprintf`](http://en.cppreference.com/w/cpp/io/c/fprintf) and send it into file stream. How can I do this?
C++

10/06/19

How many levels of pointers can we have?

How many pointers (`*`) are allowed in a single variable? Let's consider the following example. int a = 10; int *p = &a; Similarly we can have int **q = &p; int ***r =... more
C++

10/06/19

How can I iterate over an enum?

I just noticed that you can not use standard math operators on an enum such as ++ or += So what is the best way to iterate through all of the values in a C++ enum?
C++

10/05/19

How to use the PI constant in C++?

I want to use the PI constant and trigonometric functions in some C++ program. I get the trigonometric functions with `include <math.h>`. However, there doesn't seem to be a definition for PI... more
C++

10/05/19

Difference between private, public, and protected inheritance?

What is the difference between `public`, `private`, and `protected` inheritance in C++? All of the questions I've found on SO deal with specific cases.
C++

10/05/19

How do I tokenize a string in C++?

Java has a convenient split method: String str = "The quick brown fox"; String[] results = str.split(" "); Is there an easy way to do this in C++?
C++

10/05/19

What is the difference between 'typedef' and 'using' in C++11?

I know that in C++11 we can now use `using` to write type alias, like `typedef`s: typedef int MyInt; Is, from what I understand, equivalent to: using MyInt = int; And that new syntax... more
C++

10/04/19

How to avoid "if" chains?

Assuming I have this pseudo-code: bool conditionA = executeStepA(); if (conditionA){ bool conditionB = executeStepB(); if (conditionB){ bool conditionC = executeStepC(); if... more

How do I properly fill in and create a While Loop Flowchart and Pseudo code for this? I also need to perform 3 desk checks, details below...

"Farmer Pete’s pigs and sheep are all intermingled. He wants to be able to determine how many pigs and sheep he has by just walking out and counting them one at a time. He also needs to know what... more

10/02/19

Since C++ is an old language why it is still taught in universities and colleges?

Since C++ is an old language why it is still taught in universities and colleges?
C++

09/30/19

Difference of keywords 'typename' and 'class' in templates?

For templates I have seen both declarations: template < typename T > template < class T > What's the difference? And what exactly do those keywords mean in the following example... more
C++

09/30/19

Benefits of inline functions in C++?

What is the advantages/disadvantages of using inline functions in C++? I see that it only increases performance for the code that the compiler outputs, but with today's optimized compilers, fast... more
C++

09/30/19

What is the difference between #include <filename> and #include "filename"?

In the C and C++ programming languages, what is the difference between using angle brackets and using quotes in an `include` statement, as follows? 1. `#include <filename>` 1. `#include... more
C++

09/30/19

Debug vs Release in CMake?

In a GCC compiled project, - How do I run CMake for each target type (debug/release)? - How do I specify debug and release C/C++ flags using CMake? - How do I express that the main executable will... more
C++

09/30/19

What are inline namespaces for?

C++11 allows `inline namespace`s, all members of which are also automatically in the enclosing `namespace`. I cannot think of any useful application of this -- can somebody please give a brief,... more
C++

09/30/19

When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?

What are the proper uses of: - `static_cast` - `dynamic_cast` - `const_cast` - `reinterpret_cast` - C-style cast `(type)value` - Function-style cast `type(value)` How does one decide which... more
C++

09/30/19

What are virtual functions

C++

09/29/19

How to determine CPU and memory consumption from inside a process?

I once had the task of determining the following performance parameters from inside a running application: - Total virtual memory available - Virtual memory currently used - Virtual memory... more
C++

09/28/19

Is it possible to write a template to check for a function's existence?

Is it possible to write a template that changes behavior depending on if a certain member function is defined on a class? Here's a simple example of what I would want to write: ... more
C++

09/28/19

How to concatenate a std::string and an int?

I thought this would be really simple but it's presenting some difficulties. If I have std::string name = "John"; int age = 21; How do I combine them to get a single string `"John21"`?
C++

09/27/19

Why should I use a pointer rather than the object itself?

I'm coming from a Java background and have started working with objects in C++. But one thing that occurred to me is that people often use pointers to objects rather than the objects themselves,... more
C++

09/24/19

What is the difference between float and double?

I've read about the difference between double precision and single precision. However, in most cases, `float` and `double` seem to be interchangeable, i.e. using one or the other does not seem to... more
C++

09/23/19

Why don't C++ compilers define operator== and operator!=?

I am a big fan of letting the compiler do as much work for you as possible. When writing a simple class the compiler can give you the following for 'free': - A default (empty) constructor - A... more
C++

09/23/19

*.h or *.hpp for your class definitions?

I've always used a `*.h` file for my class definitions, but after reading some boost library code, I realised they all use `*.hpp`. I've always had an aversion to that file extension, I think... more

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.