2011-10-17 01:38:01 8 Comments
I'm working on a string class that employs pointers and I'm just having some difficulty in understanding how my print
function works here. Specifically, why does cout << pString
output the string and not the memory address of the dynamic array that it's pointing to? My understanding was that the variable pString was a pointer.
class MyString
{
public:
MyString(const char *inString);
void print();
private:
char *pString;
};
MyString::MyString(const char *inString)
{
pString = new char[strlen(inString) + 1];
strcpy(pString, inString);
}
void MyString::print()
{
cout << pString;
}
int main( )
{
MyString stringy = MyString("hello");
stringy.print();
return 0;
}
Related Questions
Sponsored Content
27 Answered Questions
[SOLVED] Easiest way to convert int to string in C++
- 2011-04-08 04:19:41
- Nemo
- 2736336 View
- 1489 Score
- 27 Answer
- Tags: c++ string int type-conversion
5 Answered Questions
[SOLVED] Why does changing 0.1f to 0 slow down performance by 10x?
- 2012-02-16 15:58:39
- Dragarro
- 138082 View
- 1494 Score
- 5 Answer
- Tags: c++ performance visual-studio-2010 compilation floating-point
25 Answered Questions
[SOLVED] Why do we need virtual functions in C++?
- 2010-03-06 07:10:35
- Jake Wilson
- 523493 View
- 1226 Score
- 25 Answer
- Tags: c++ virtual-functions
37 Answered Questions
21 Answered Questions
7 Answered Questions
[SOLVED] C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?
- 2011-06-11 23:30:14
- Nawaz
- 208899 View
- 1813 Score
- 7 Answer
- Tags: c++ multithreading c++11 language-lawyer memory-model
10 Answered Questions
[SOLVED] Why is reading lines from stdin much slower in C++ than Python?
- 2012-02-21 02:17:50
- JJC
- 246365 View
- 1741 Score
- 10 Answer
- Tags: python c++ benchmarking iostream getline
3 Answered Questions
6 Answered Questions
[SOLVED] What does "dereferencing" a pointer mean?
- 2011-02-10 09:16:22
- asir
- 531791 View
- 500 Score
- 6 Answer
- Tags: c++ c pointers dereference
3 comments
@Mysticial 2011-10-17 01:40:10
This is because the
<<
operator has been overloaded to handle the case of achar*
and print it out as a string. As opposed to the address (which is the case with other pointers).I think it's safe to say that this is done for convenience - to make it easy to print out strings.
So if you want to print out the address, you should cast the pointer to a
void*
.@redache 2011-10-17 01:44:25
This is down to the fact that "<<" will automatically follow the pointer and print out the string instead of just printing out the memory address. This is easier to see in printf as you can specify the print out of a pointer OR what the pointer references.
You can see here that %s prints out the string and %p prints out the memory address.
@Tanveer Badar 2019-07-13 05:49:15
But only for char* and wchar_t* types, not any and all pointers.
@Greg Hewgill 2011-10-17 01:41:01
The variable
pString
is a pointer. However, the implementation of<<
when used with an output stream knows that if you try to output achar *
, then the output should be printed as a null-terminated string.Try: