586 Answered Questions for the topic C++

C++

08/23/19

error: request for member '..' in '..' which is of non-class type?

I have a class with two constructors, one that takes no arguments and one that takes one argument. Creating objects using the constructor that takes one argument works as expected. However, if I... more
C++

08/23/19

How to get current time and date in C++?

Is there a cross-platform way to get the current date and time in C++?
C++

08/22/19

std::string to char*?

I want to convert a **std::string** into a **char*** or **char[]** data type. std::string str = "string"; char* chr = str; Results in: **“error: cannot convert ‘std::string’ to ‘char’... more
C++

08/22/19

Simple example of threading in C++?

Can someone post a simple example of starting two (Object Oriented) threads in C++. I'm looking for actual C++ thread objects that I can extend run methods on (or something similar) as opposed to... more
C++

08/22/19

How to convert std::string to lower case?

I want to convert a `std::string` to lowercase. I am aware of the function `tolower()`, however in the past I have had issues with this function and it is hardly ideal anyway as use with a... more
C++

08/22/19

Does the 'mutable' keyword have any purpose other than allowing the variable to be modified by a const function?

A while ago I came across some code that marked a member variable of a class with the `mutable` keyword. As far as I can see it simply allows you to modify a variable in a `const` method: class... more
C++

08/22/19

How to call a parent class function from derived class function?

How do I call the parent function from a derived class using C++? For example, I have a class called `parent`, and a class called `child` which is derived from parent. Within each class there is a... more
C++

08/21/19

Why are #ifndef and #define used in C++ header files?

I have been seeing code like this usually in the start of header files: #ifndef HEADERFILE_H #define HEADERFILE_H And at the end of the file is #endif What is the purpose of this?
C++

08/20/19

What is this weird colon-member (" : ") syntax in the constructor?

Recently I've seen an example like the following: #include <iostream> class Foo { public: int bar; Foo(int num): bar(num) {}; }; int main(void) { std::cout <<... more
C++

08/20/19

When should I write the keyword 'inline' for a function/method?

When should I write the keyword `inline` for a function/method in C++? After seeing some answers, some related questions: - When should I **not** write the keyword 'inline' for a function/method... more
C++

08/19/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/18/19

What are C++ functors and their uses?

I keep hearing a lot about functors in C++. Can someone give me an overview as to what they are and in what cases they would be useful?
C++

08/16/19

What does the C++ standard state the size of int, long type to be?

I'm looking for detailed information regarding the size of basic C++ types. I know that it depends on the architecture (16 bits, 32 bits, 64 bits) and the compiler. But are there any standards for... more
C++

08/15/19

How to make a SIMPLE C++ Makefile?

We are required to use a Makefile to pull everything together for our project but our professor never showed us how to. I only have ONE file, `a3driver.cpp`. The driver imports a class from a... more
C++

08/15/19

What is the most effective way to get the index of an iterator of an std::vector?

I'm iterating over a vector and need the index the iterator is currently pointing at. AFAIK this can be done in two ways: - `it - vec.begin()` - `std::distance(vec.begin(), it)` What are the... more
C++

08/15/19

How can I get the list of files in a directory using C or C++?

How can I determine the list of files in a directory from inside my C or C++ code? I'm not allowed to execute the `ls` command and parse the results from within my program.
C++

08/14/19

How can I convert a std::string to int?

Just have a quick question. I've looked around the internet quite a bit and I've found a few solutions but none of them have worked yet. Looking at converting a string to an int and I don't mean... more
C++

08/12/19

What is a smart pointer and when should I use one?

What is a smart pointer and when should I use one?
C++

08/12/19

How do I output coloured text to a Linux terminal?

How do I print coloured characters to a Linux terminal that supports it? How do I tell whether the terminal supports colour codes?
C++

08/11/19

What is the lifetime of a static variable in a C++ function?

If a variable is declared as `static` in a function's scope it is only initialized once and retains its value between function calls. What exactly is its lifetime? When do its constructor and... more
C++

08/10/19

When you should use virtual inheritance?

C++

08/09/19

Why does C++ compilation take so long?

Compiling a C++ file takes a very long time when compared to C# and Java. It takes significantly longer to compile a C++ file than it would to run a normal size Python script. I'm currently using... more
C++

08/07/19

How to convert a std::string to const char* or char*?

How can I convert an `std::string` to a `char*` or a `const char*`?
C++

08/07/19

How do malloc() and free() work?

I want to know how `malloc` and `free` work. int main() {  unsigned char *p = (unsigned char*)malloc(4*sizeof(unsigned char));  memset(p,0,4);  strcpy((char*)p,"abcdabcd"); //... more
C++

08/07/19

What is external linkage and internal linkage?

I want to understand the external linkage and internal linkage and their difference. I also want to know the meaning of > `const` variables internally link by default unless otherwise... 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.