Showing posts with label cplusplus. Show all posts
Showing posts with label cplusplus. Show all posts

Wednesday, April 22, 2015

C++: Find string within a string

There is a string::find function that is pretty straightforward to use. The key thing to note here is that when it's not found the return value is "string::npos".

Reference: http://www.cplusplus.com/reference/string/string/find/

C++: Constructing a std::string from std::vector

std::vector<char> is actually a vector of the characters that make up the string.

There is a constructor for std::string that can be used for this i.e. std::string s(v.begin(),v.end());

Reference: http://stackoverflow.com/questions/5115166/how-to-construct-a-stdstring-from-a-stdvectorchar

Thursday, December 11, 2014

C++ Necessary to Call Base Destructor?

The answer is no, it is automatically called, but it is also somehow important that the destructor is virtual if the class will be inherited.

Reference: http://stackoverflow.com/questions/677620/do-i-need-to-explicitly-call-the-base-virtual-destructor

Thursday, February 27, 2014

C++ Avoid Circular Header File Dependencies

In C++, you will get compile errors when header files have a circular dependence on each other. Therefore, it is best to use forward declarations in the header files, and include the header files only in the "cpp" definition files.

However, there are 2 exceptions to this, where you will need to include another header file in the header file:

  1. When you are inheriting from another class, you will need the header class for that class in the header file
  2. When you are using a class as a stack variable rather than dynamically allocated heap variable, you will also need the header file for that class in the header file

Wednesday, October 30, 2013

C++ Static Variables

Other than the usual differences in scope, there are some unique aspects of the syntax that will stump a developer from a Java background.

While instance variables only require declaration in the header file, a static variable in C++ needs to be declared twice, once in the header file and another in the CPP file. Otherwise you'll get a linker error.

See here for example: http://stackoverflow.com/questions/9282354/static-variable-link-error