1st question:
what does c_str() do ? when should i use and include it ?
2nd question:
what does 'this pointer' do ? when should i use and include it ?
Printable View
1st question:
what does c_str() do ? when should i use and include it ?
2nd question:
what does 'this pointer' do ? when should i use and include it ?
>what does c_str() do ? when should i use and include it ?
It returns the contents of a string in nul terminated C-style string format. You should only need it for functions that take char * or const char * instead of std::string. For example, printf.
>what does 'this pointer' do ? when should i use and include it ?
Every object has a pointer to itself, called this. Any time you need an object to refer to itself you can use it. For example, when implementing the assignment, you can use this to check for self assignment:
Code:Test& Test::operator= ( const Test& rhs )
{
if ( this == &rhs )
return *this;
...
}
the above code means ?Code:void test(int x)
{
int x;
this->x=x;
}
Assuming it is a member function, it means you will get a compiler diagnostic complaining about a redefinition of a formal parameter. If you remove the local definition of x then it means the object member x is assigned the value of the parameter x:
Code:#include <iostream>
using namespace std;
class C {
public:
void test(int x)
{
//int x;
this->x=x;
}
int x;
};
int main()
{
C c;
c.test(5);
cout<< c.x <<endl;
}