Found 7346 Articles for C++

What is the copy-and-swap idiom in C++?

Ankith Reddy
Updated on 23-Jun-2020 13:51:21

189 Views

The assignment consists of 2 steps, tearing an object's old state and building a new state for it. The destructor is used for the first step and a copy constructor does the second step. Implementing both of these is straightforward. But when overloading the assignment operator, it can become quite difficult to implement. The copy and swap idiom is a solution for the same.This idiom uses the copy-constructor to build a local copy of the data. It then swaps the old data with the new data using the swap function. The temporary copy is then destructed using the destructor. We ... Read More

How can I profile C++ code running in Linux?

Vrundesha Joshi
Updated on 11-Feb-2020 10:28:25

814 Views

There are many great profiling tools for profiling C++ programs on Linux. The most widely used tool is Valgrind. It is a programming tool for memory debugging, memory leak detection, and profiling. You can use valgrind by passing the binary to it and setting the tool to callgrind. First generate the binary by compiling the program −$ g++ -o hello.cpp hello Now use valgrind to profile it: $ valgrind --tool=callgrind ./helloThis will generate a file called callgrind.out.x. You can read this file using a tool called kcachegrind.If you're using gcc, you can use the inbuilt profiling tool, gprof. You can ... Read More

The most elegant way to iterate the words of a C/C++ string

Arjun Thakur
Updated on 11-Feb-2020 10:27:10

1K+ Views

There is no one elegant way to iterate the words of a C/C++ string. The most readable way could be termed as the most elegant for some while the most performant for others. I've listed 2 methods that you can use to achieve this. 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;         ... Read More

What is object slicing in C++ or Java?

Ankitha Reddy
Updated on 30-Jul-2019 22:30:21

2K+ Views

Object slicing is used to describe the situation when you assign an object of a derived class to an instance of a base class. This causes a loss of methods and member variables for the derived class object. This is termed as information being sliced away. For example, class Foo { int a; }; class Bar : public Foo { int b; }; Since Bar extends Foo, it now has 2 member variables, a and b. So if you create a variable bar of type Bar and then create ... Read More

What are the rules about using an underscore in a C++ identifier?

Arjun Thakur
Updated on 11-Feb-2020 10:22:52

2K+ Views

From MSDN docs −Use of two sequential underscore characters ( __ ) at the beginning of an identifier, or a single leading underscore followed by a capital letter, is reserved for C++ implementations in all scopes. You should avoid using one leading underscore followed by a lowercase letter for names with file scope because of possible conflicts with current or future reserved identifiers.So you should avoid using names like −__foo, __FOO, _FOOAnd names like the following should not be used in the global namespace −_foo, _barOther than this, there are some more prefixes like LC_, SIG_, and suffixes like _t ... Read More

Can a local variable's memory be accessed outside its scope in C/C++?

Rishi Rathor
Updated on 11-Feb-2020 10:20:27

125 Views

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

Why the use of "using namespace std' considered bad practice?

Abhinaya
Updated on 30-Jul-2019 22:30:21

396 Views

C++ has a standard library that contains common functionality you use in building your applications like containers, algorithms, etc. If names used by these were out in the open, for example, if they defined a queue class globally, you'd never be able to use the same name again without conflicts. So they created a namespace, std to contain this change.The using namespace statement just means that in the scope it is present, make all the things under the std namespace available without having to prefix std:: before each of them.While this practice is okay for example code, pulling in the ... Read More

Why the use of iostream::eof inside a loop condition considered wrong?

Nancy Den
Updated on 23-Jun-2020 13:40:06

140 Views

Just because we haven't reached the EOF, doesn't mean the next read will succeed.Consider you have a file that you read using file streams in C++. When writing a loop to read the file, if you are checking for stream.eof(), you're basically checking if the file has already reached eof. So you'd write the code like −Example#include #include using namespace std; int main() {    ifstream myFile("myfile.txt");    string x;        while(!myFile.eof()) {       myFile >> x;       // Need to check again if x is valid or eof       if(x) { ... Read More

What is The Rule of Three with reference to C++?

Govinda Sai
Updated on 23-Jun-2020 13:40:50

100 Views

The Rule of three is a rule of thumb when using C++. This is kind of a good practice rule that says that If your class needs any ofa copy constructor, an assignment operator, or a destructor, defined explicitly, then it is likely to need all three of them.Why is this? Its because, if your class requires any of the above, it is managing dynamically allocated resources and would likely be needing the other to successfully achieve that. For example, if you require an assignment operator, you would be creating copies of objects currently being copied by reference, hence allocating ... Read More

Why can C++ templates only be implemented in the header file?

Ramu Prasad
Updated on 11-Feb-2020 10:14:53

743 Views

When you instantiate a template in C++, the compiler creates a new class. This class has all the places where you placed the template arguments replaced with the actual argument you pass to it when using it. For example −template class MyClass {    T foo;    T myMethod(T arg1, T arg2) {       // Impl    } };And somewhere in your program use this class, MyClass x;The compiler creates a new class upon encountering this for every type argument you pass it. For example, if you created 3 objects with different template arguments you'll get 3 classes, ... Read More

Advertisements