598 Answered Questions for the topic C++

C++

07/26/19

How to get rid of `deprecated conversion from string constant to ‘char*’` warnings in GCC?

So I'm working on an exceedingly large codebase, and recently upgraded to gcc 4.3, which now triggers this warning: > warning: deprecated conversion from string constant to ‘char*’ Obviously,... more
C++

07/25/19

unsigned int vs. size_t?

I notice that modern C and C++ code seems to use `size_t` instead of `int`/`unsigned int` pretty much everywhere - from parameters for C string functions to the STL. I am curious as to the reason... more
C++

07/23/19

When to use dynamic vs. static libraries?

When creating a class library in C++, you can choose between dynamic (`.dll`, `.so`) and static (`.lib`, `.a`) libraries. What is the difference between them and when is it appropriate to use which?
C++

07/23/19

Check if a string contains a string in C++?

I have a variable of type `std::string`. I want to check if it contains a certain `std::string`. How would I do that? Is there a function that returns true if the string is found, and false if it... more
C++

07/22/19

Differences between C++ string == and compare()?

I just read some recommendations on using std::string s = get_string(); std::string t = another_string(); if( !s.compare(t) ) { instead of if( s == t ) { I'm almost always... more
C++

07/21/19

Static constant string (class member)?

I'd like to have a private static constant for a class (in this case a shape-factory). I'd like to have something of the sort. class A { private: static const string RECTANGLE =... more
C++

07/21/19

Are == and != mutually dependent?

I'm learning about operator overloading in C++, and I see that `==` and `!=` are simply some special functions which can be customized for user-defined types. My concern is, though, why are there... more
C++

07/20/19

Struct Constructor in C++?

Can a `struct` have a constructor in C++? I have been trying to solve this problem but I am not getting the syntax.
C++

07/19/19

When should you use constexpr capability in C++11?

It seems to me that having a "function that always returns 5" is breaking or diluting the meaning of "calling a function". There must be a reason, or a need for this capability or it wouldn't be in... more
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

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.