586 Answered Questions for the topic C++

C++

07/19/19

Passing a 2D array to a C++ function?

I have a function which I want to take, as a parameter, a 2D array of variable size. So far I have this: void myFunction(double** myArray){ myArray[x][y] = 5; etc... } And I have... more
C++

07/19/19

Undefined reference to vtable?

So, I'm getting the infamously horrible > undefined reference to 'vtable... error for the following code (The class in question is CGameModule.) and I cannot for the life of me understand... more
C++

07/19/19

Why does the C++ STL not provide any "tree" containers?

Why does the C++ STL not provide any "tree" containers, and what's the best thing to use instead? I want to store a hierarchy of objects as a tree, rather than use a tree as a performance... more
C++

07/18/19

What are the new features in C++17?

C++17 is now feature complete, so unlikely to experience large changes. Hundreds of proposals were put forward for C++17. Which of those features were added to C++ in C++17? When using a C++... more
C++

07/16/19

What is object slicing?

Someone mentioned it in the IRC as the slicing problem.
C++

07/15/19

How do I list the symbols in a .so file?

How do I list the symbols being exported from a .so file? If possible, I'd also like to know their source (e.g. if they are pulled in from a static library). I'm using gcc 4.0.2, if that makes a... more
C++

07/14/19

What is The Rule of Three?

- What does *copying an object* mean? - What are the *copy constructor* and the *copy assignment operator*? - When do I need to declare them myself? - How can I prevent my objects from being... more
C++

07/13/19

Is it safe to delete a NULL pointer?

Is it safe to delete a NULL pointer? And is it a good coding style?
C++

07/12/19

What is the most effective way for float and double comparison?

What would be the most efficient way to compare two `double` or two `float` values? Simply doing this is not correct: bool CompareDoubles1 (double A, double B) { return A == B; } But... more
C++

07/11/19

usr/bin/ld: cannot find -l<nameOfTheLibrary>?

I'm trying to compile my program and it returns this error : usr/bin/ld: cannot find -l<nameOfTheLibrary> in my makefile I use the command `g++` and link to my library which is a symbolic... more
C++

07/11/19

Difference between 'struct' and 'typedef struct' in C++?

In C++, is there any difference between: struct Foo { ... }; and typedef struct { ... } Foo;
C++

07/11/19

Is multiplication and division using shift operators in C actually faster?

Multiplication and division can be achieved using bit operators, for example i*2 = i<<1 i*3 = (i<<1) + i; i*10 = (i<<3) + (i<<1) and so on. Is it actually faster to... more
C++

07/11/19

What is an undefined reference/unresolved external symbol error and how do I fix it?

What are undefined reference/unresolved external symbol errors? What are common causes and how to fix/prevent them? <sub>Feel free to edit/add your own.</sub>
C++

07/08/19

Which is better option to use for dividing an integer number by 2?

Which of the following techniques is the best option for dividing an integer by 2 and why? Technique 1: x = x >> 1; Technique 2: x = x / 2; Here `x` is an integer.
C++

07/08/19

What are the rules for calling the superclass constructor?

What are the C++ rules for calling the superclass constructor from a subclass one? For example, I know in Java, you must do it as the first line of the subclass constructor (and if you don't, an... more
C++

07/07/19

How do I pass a unique_ptr argument to a constructor or a function?

<!-- language-all: lang-cpp --> I'm new to move semantics in C++11 and I don't know very well how to handle `unique_ptr` parameters in constructors or functions. Consider this class... more
C++

07/03/19

Is there any advantage of using map over unordered_map in case of trivial keys?

A recent talk about `unordered_map` in C++ made me realize, that I should use `unordered_map` for most cases where I used `map` before, because of the efficiency of lookup ( *amortized O(1)* vs.... more
C++

07/03/19

Printing 1 to 1000 without loop or conditionals?

**Task**: Print numbers from 1 to 1000 without using any loop or conditional statements. Don't just write the `printf()` or `cout` statement 1000 times. How would you do that using C or C++?
C++

07/01/19

Why use static_cast<int>(x) instead of (int)x?

I've heard that the `static_cast` function should be preferred to C-style or simple function-style casting. Is this true? Why?
C++

06/30/19

What are POD types in C++?

I've come across this term POD-type a few times. What does it mean?
C++

06/30/19

Is there a standard sign function (signum, sgn) in C/C++?

I want a function that returns -1 for negative numbers and +1 for positive numbers. http://en.wikipedia.org/wiki/Sign_function It's easy enough to write my own, but it seems like something that... more

06/29/19

Force all && to be executed?

Consider the following variadic function template <typename Type, typename... Types> bool f(Type& arg, Types&... args) { return f(arg) && f(args...); } template... more
C++

06/29/19

Why is enum class preferred over plain enum?

I heard a few people recommending to use enum *classes* in C++ because of their **type safety**. But what does that really mean?
C++

06/28/19

Where to put default parameter value in C++?

What's the place for the default parameter value? Just in function definition, or declaration, or both places?
C++

06/27/19

In C++, what is a virtual base class?

I want to know what a "*virtual base class*" is and what it means. Let me show an example: class Foo { public: void DoSomething() { /* ... */ } }; class Bar : public virtual Foo ... 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.