(*iter)->someMethod()
Reference: http://stackoverflow.com/questions/23318875/iterating-through-a-vector-of-pointers
My online tech scrapbook where I keep my notes in case I need to look them up later
Showing posts with label cplusplus. Show all posts
Showing posts with label cplusplus. Show all posts
Wednesday, June 17, 2015
C++: Iterating Through Vector of Pointers
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/
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
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
Saturday, April 18, 2015
C++ Optional Parameter
You can make an optional parameter by giving it a default value. Important note: you need to do it in the header declaration.
Reference: http://stackoverflow.com/questions/2842928/default-value-of-function-parameter
Reference: http://stackoverflow.com/questions/2842928/default-value-of-function-parameter
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
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:
However, there are 2 exceptions to this, where you will need to include another header file in the header file:
- When you are inheriting from another class, you will need the header class for that class in the header file
- 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
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
Tuesday, October 15, 2013
C++ Warning "Deprecated conversion from string literal to 'char*'"
This post in StackOverflow provides a good explanation and solution for it.
http://stackoverflow.com/questions/9650058/deprecated-conversion-from-string-literal-to-char
http://stackoverflow.com/questions/9650058/deprecated-conversion-from-string-literal-to-char
Subscribe to:
Posts (Atom)