588 Answered Questions for the topic C++
C++
09/08/19
Why use pointers?
I know this is a really basic question, but I've just started with some basic C++ programming after coding a few projects with high-level languages.
Basically I have three questions:
1. Why use...
more
C++
09/08/19
Why do we need virtual functions in C++?
I'm learning C++ and I'm just getting into virtual functions.
From what I've read (in the book and online), virtual functions are functions in the base class that you can override in derived...
more
C++
09/08/19
What is the effect of extern "C" in C++?
What exactly does putting `extern "C"` into C++ code do?
For example:
extern "C" {
void foo();
}
C++
09/07/19
How to Detect if I'm Compiling Code with a particular Visual Studio version?
Is there any way to know if I'm compiling under a specific Microsoft Visual Studio version?
C++
09/07/19
How to use Boost in Visual Studio 2010?
What is a good step by step explanation on how to use the Boost library in an empty project in Visual Studio?
C++
09/07/19
What is a "cache-friendly" code?
What is the difference between "**cache unfriendly code**" and the "**cache friendly**" code?
How can I make sure I write cache-efficient code?
C++
09/06/19
Unnamed/anonymous namespaces vs. static functions?
A feature of C++ is the ability to create unnamed (anonymous) namespaces, like so:
namespace {
int cannotAccessOutsideThisFile() { ... }
} // namespace
You would think that such a feature...
more
C++
09/06/19
How do I declare a 2d array in C++ using new?
How do i declare a 2d array using new?
Like, for a "normal" array I would:
int* ary = new int[Size]
but
int** ary = new int[sizeY][sizeX]
a) doesn't work/compile and b) doesn't accomplish...
more
C++
09/06/19
In what cases do I use malloc vs new?
I see in C++ there are multiple ways to allocate and free data and I understand that when you call `malloc` you should call `free` and when you use the `new` operator you should pair with `delete`...
more
C++
09/06/19
When to use reinterpret_cast?
I am little confused with the applicability of `reinterpret_cast` vs `static_cast`. From what I have read the general rules are to use static cast when the types can be interpreted at compile time...
more
C++
09/06/19
Is inline assembly language slower than native C++ code?
I tried to compare the performance of inline assembly language and C++ code, so I wrote a function that add two arrays of size 2000 for 100000 times. Here's the code: #define TIMES 100000 ...
more
C++
09/03/19
Is uninitialized local variable the fastest random number generator?
I know the uninitialized local variable is undefined behaviour(*UB*), and also the value may have trap representations which may affect further operation, but sometimes I want to use the random...
more
C++
09/01/19
How do you clear a stringstream variable?
I've tried several things already,
std::stringstream m;
m.empty();
m.clear();
both of which don't work.
C++
09/01/19
Can I call a base class's virtual function if I'm overriding it?
Say I have classes `Foo` and `Bar` set up like this:
class Foo
{
public:
int x;
virtual void printStuff()
{
std::cout << x << std::endl;
}
};
...
more
C++
09/01/19
When should you use a class vs a struct in C++?
In what scenarios is it better to use a `struct` vs a `class` in C++?
C++
08/31/19
Iteration over std::vector: unsigned vs signed index variable?
What is the correct way of iterating over a vector in C++?
Consider these two code fragments, this one works fine:
for (unsigned i=0; i < polygon.size(); i++) {
sum += polygon[i];
...
more
C++
08/31/19
Forward declaring an enum in C++?
I'm trying to do something like the following:
enum E;
void Foo(E e);
enum E {A, B, C};
which the compiler rejects. I've had a quick look on Google and the consensus seems to be "you...
more
C++
08/31/19
What are the rules about using an underscore in a C++ identifier?
It's common in C++ to name member variables with some kind of prefix to denote the fact that they're member variables, rather than local variables or parameters. If you've come from an MFC...
more
C++
08/30/19
What XML parser should I use in C++?
I have XML documents that I need to parse and/or I need to build XML documents and write them to text (either files or memory). Since the C++ standard library does not have a library for this, what...
more
C++
08/30/19
How do I get the directory that a program is running from?
Is there a platform-agnostic and filesystem-agnostic method to obtain the full path of the directory from where a program is running using C/C++? Not to be confused with the current working...
more
C++
08/30/19
Why does NaN - NaN == 0.0 with the Intel C++ Compiler?
It is well-known that NaNs propagate in arithmetic, but I couldn't find any demonstrations, so I wrote a small test:
#include <limits>
#include <cstdio>
int main(int argc, char*...
more
C++
08/30/19
What is the easiest way to initialize a std::vector with hardcoded elements?
I can create an array and initialize it like this:
int a[] = {10, 20, 30};
How do I create a `std::vector` and initialize it similarly elegant?
The best way I know is:
...
more
C++
08/27/19
What is the easiest way to make a C++ program crash?
I'm trying to make a Python program that interfaces with a different crashy process (that's out of my hands). Unfortunately the program I'm interfacing with doesn't even crash reliably! So I want...
more
C++
08/25/19
Why does C++ not have reflection?
This is a somewhat bizarre question. My objectives are to understand the language design decision and to identify the possibilities of reflection in C++.
1. Why C++ language committee did not go...
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.