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

C++ string in class

Name: Anonymous 2007-09-10 20:06 ID:aWmcM4MD


class Myclass {
    public:
        Myclass (char*);
        ~Myclass ();
        char* Myclass ::getName();
    protected:
        char* name;
};

Myclass ::Myclass (char* inname){
    name = new char[strlen(inname)*sizeof(char)];
    strcpy(name, inname);
}

char* Tile::getName(){
    return name;
}


How come name returns a random pointer? I'm declaring everything right, but for some reason it gets corrupted...

Name: Anonymous 2007-09-11 7:24 ID:2JgDgQWe


$ cat > hello.cc
#include <iostream>
int main() { std::cout << "Hello world!" << std::endl; return 0; }
$ g++ -o hello hello.cc
$ ./hello
Hello world!
$ ls -l hello
-rwxr-xr-x 1 anon anon 8443 2007-09-11 21:19 hello
$

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