Found 7346 Articles for C++

When to use virtual destructors in C++?

Govinda Sai
Updated on 11-Feb-2020 11:05:48

683 Views

Scott Meyers in Effective C++ says −If a class has any virtual function, it should have a virtual destructor, and that classes not designed to be base classes or not designed to be used polymorphically should not declare virtual destructors.So you should declare destructors virtual in polymorphic base classes. This is because if you create an object of base class using a derived constructor −Base *b = new Derived(); // use b delete b;If Base's destructor is not virtual then delete b has undefined behavior in this case. The call to the destructor will be resolved like any non-virtual code. ... Read More

Difference between const int*, const int * const, and int const * in C/C++?

George John
Updated on 11-Feb-2020 11:04:26

3K+ Views

The above symbols mean the following −int* - Pointer to int. This one is pretty obvious. int const * - Pointer to const int. int * const - Const pointer to int int const * const - Const pointer to const intAlso note that −const int * And int const * are the same. const int * const And int const * const are the same.If you ever face confusion in reading such symbols, remember the Spiral rule: Start from the name of the variable and move clockwise to the next pointer or type. Repeat until expression ends.

Tokenize a string in C++?

Ramu Prasad
Updated on 11-Feb-2020 11:03:12

205 Views

First way is using a stringstream to read words seperated by spaces. This is a little limited but does the task fairly well if you provide the proper checks. example#include #include #include using namespace std; int main() {    string str("Hello from the dark side");    string tmp; // A string to store the word on each iteration.    stringstream str_strm(str);    vector words; // Create vector to hold our words    while (str_strm >> tmp) {       // Provide proper checks here for tmp like if empty       // ... Read More

How to read and parse CSV files in C++?

Nitya Raut
Updated on 11-Feb-2020 10:54:53

3K+ Views

You should really be using a library to parsing CSV files in C++ as there are many cases that you can miss if you read files on your own. The boost library for C++ provides a really nice set of tools for reading CSV files. For example, example#include vector parseCSVLine(string line){    using namespace boost;    std::vector vec;    // Tokenizes the input string    tokenizer tk(line, escaped_list_separator    ('\', ', ', '\"'));    for (auto i = tk.begin();  i!=tk.end();  ++i)    vec.push_back(*i);    return vec; } int main() {    std::string line = "hello, from, ... Read More

How do you set, clear, and toggle a bit in C/C++?

Chandu yadav
Updated on 11-Feb-2020 10:52:13

7K+ Views

You can set clear and toggle bits using bitwise operators in C, C++, Python, and all other programming languages that support these operations. You also need to use the bitshift operator to get the bit to the right place.Setting a bitTo set a bit, we'll need to use the bitwise OR operator −Example#include using namespace std; int main() {    int i = 0, n;        // Enter bit to be set:    cin >> n;    i |= (1 > n;    i ^= (1

How to compare float and double in C++?

Sravani S
Updated on 24-Jun-2020 05:41:44

579 Views

Comparing floats and double variables depends on what your end goal is. If you want a runnable function without going too much in details and won't have a problem in some inaccurate calculations you can use the following function −Example#include using namespace std; // Define the error that you can tolerate #define EPSILON 0.000001 bool areSame(double a, double b) {    return fabs(a - b) < EPSILON; } int main() {    double a = 1.005;    double b = 1.006;    cout

How to access a local variable from a different function using C++ pointers?

George John
Updated on 24-Jun-2020 05:42:36

1K+ Views

You can't access a local variable once it goes out of scope. This is what it means to be a local variable. Though, Let us look at an example where you MIGHT be able to access a local variable's memory outside its scope.Example#include int* foo() {    int x = 3;    return &x; } int main() {    int* address = foo();    cout

Explain C++ Singleton design pattern.

Jennifer Nicholas
Updated on 24-Jun-2020 05:42:05

15K+ Views

Singleton design pattern is a software design principle that is used to restrict the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. For example, if you are using a logger, that writes logs to a file, you can use a singleton class to create such a logger. You can create a singleton class using the following code −Example#include using namespace std; class Singleton {    static Singleton *instance;    int data;      // Private constructor so that no objects can be created. ... Read More

When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used in C++?

V Jyothi
Updated on 23-Jun-2020 13:57:11

3K+ Views

const_castcan be used to remove or add const to a variable. This can be useful if it is necessary to add/remove constness from a variable.static_castThis is used for the normal/ordinary type conversion. This is also the cast responsible for implicit type coersion and can also be called explicitly. You should use it in cases like converting float to int, char to int, etc.dynamic_castThis cast is used for handling polymorphism. You only need to use it when you're casting to a derived class. This is exclusively to be used in inheritence when you cast from base class to derived class.reinterpret_castThis is ... Read More

Where can I find the current C or C++ standard documents?

Nishtha Thakur
Updated on 23-Jun-2020 13:59:38

75 Views

You can find the current C standard documents on ANSI web store. https://webstore.ansi.org/RecordDetail.aspx?sku=INCITS%2FISO%2FIEC+9899-2012You can find the current C++ standard documents on the ISO C++ website for buying −  https://www.iso.org/standard/68564.htmlThe working draft of the ISO C++ standard is also available on  https://isocpp.org/std/the-standard

Advertisements