Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ string class

Name: Anonymous 2012-09-03 13:53

If you have a C++ string and a char pointer

string str = "mudkipz";
char * ptr;


how would you iterate through each char in the string using only the pointer and only pointer notation? In other words, you can't assign the address with ptr = &str[0];

I don't see how it's possible, since a string isn't an array. If it were, I'd just do this

string str = "mudkipz";
    char * ptr = str;

    for(int i = 0; i < str.length(); ++i)
    {
        cout << *(ptr + i);
    }


Any ideas?

Name: Anonymous 2012-09-03 16:26

>>4
You shouldn't mix oop and pointer magic. One of the main principles of oop is encapsulation which, in terms of your example, means that you should manipulate objects of the stirng class only though their public methods, the inner representation of the string should never leak outside. Trying to "tinker" the state of objects through pointers completely violates this principle.

You can obtain a pointer to an inner array of chars using the [code]data()[/spoiler] method. c++ is shit

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List