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

Pages: 1-

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-10 20:23 ID:Heaven

I lol'd

Name: Anonymous 2007-09-10 20:31 ID:izLcHRUf

srand(time());
name = (char *)rand();

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


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

thats a typo. its of the same class

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

bah found reason. delete constructor is gay, avoid it like the plague.

Name: Anonymous 2007-09-10 20:44 ID:Heaven

>>3
an int cannot necessarely hold the value of a void*.

Name: Anonymous 2007-09-10 20:58 ID:Heaven

>>5
No - "lol its gay" isn't a reason. Your failure is due to incompetence.

Name: Anonymous 2007-09-10 21:46 ID:yfcWJcZw

There's already a string class, stop wasting your time reinventing shit.
Better yet, switch to a different language.

Name: Anonymous 2007-09-10 21:50 ID:Heaven

#include <string>

Name: Anonymous 2007-09-10 21:51 ID:Cexl1IX2

>>1
First, you aren't allocating enough space for the string. They're zero-terminated, so what you should have is name = new char[strlen(inname) + 1]; -- also drop the sizeof(char) because array new already allocates the type in question rather than malloc-style bytes.

Second, just use the C++ std::string class. Not having to do string handling yourself is worth the efficiency penalty... long as you're using C++ anyway...

Name: Anonymous 2007-09-10 22:08 ID:Dp5Jfevu

maybe he's writing the class as an excersise. So there is little point to use the standard. let him reinvent the wheel.

Name: Anonymous 2007-09-10 22:59 ID:Heaven

wot = new int[10 * sizeof int]
amirite

Name: Anonymous 2007-09-10 23:52 ID:XebsU8Hy

>>11
Then let him fucking figure it out on his own as he should and not bitch and moan about his own incompetence here while begging for help from his Personal Army(tm).

Name: Anonymous 2007-09-11 0:10 ID:lU4F1D0n


#include <string>

class Myclass {
    public:
        Myclass (string s) {name = s;};
        ~Myclass ();
        string Myclass ::getName(return name;);
    protected:
        string name;
};


error C2146: syntax error : missing ';' before identifier 'name'

LOL WUT?

how do i put a string in a class?

this is bs. C has so many understandable ways of doing things :/

Name: Anonymous 2007-09-11 0:12 ID:Heaven


        string Myclass ::getName(return name;);

Name: Anonymous 2007-09-11 0:18 ID:Heaven

>>14
std::string

Name: Anonymous 2007-09-11 0:58 ID:lU4F1D0n

>>16
ah, thanks!

Name: Anonymous 2007-09-11 1:37 ID:FclbgyED

>>16
std::aids

Name: Anonymous 2007-09-11 2:28 ID:5GYgZE+H

To be C++ compliant, one should use std::string. But for some crappy reason I can't remember well (something to do with GNU or GPL license or something) some compilers will link statically to the standard library. This gives 400KB+ executable file size for a fucking Hello World. As far as I know this is the case for the GNU c++ compiler. Borland c++ and Visual c++ compiler both provide a dynamic standard library dll (libstdc++.dll or something)

Name: Anonymous 2007-09-11 2:30 ID:1cMh3ula

>>19
Not only do you get massive executables, but you also have to install a 2gb IDE just to compile them.

Name: Anonymous 2007-09-11 2:45 ID:FclbgyED

>>20
erm, dude, he was saying that the xbawks hueg 2gb IDEs make smaller executables than gcc.

Name: Anonymous 2007-09-11 5:09 ID:Heaven

icc or gtfo

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
$

Name: Anonymous 2007-09-11 7:47 ID:Heaven

needs moar -W -Wall -Wshadow -Wcast-aligned -O3 -Os -ffasth-math -fomit-pointer -funroll-loops

Name: Anonymous 2007-09-11 8:04 ID:5GYgZE+H

>>23
Me was talking about Windows environment. Because Linux is GNU, maybe they can link dynamically. By doing the same in windows I get 487676 Bytes hello.exe

Name: Anonymous 2007-09-11 9:05 ID:bCN3WPHN

>>25
Wasn't MinGW's point to link statically or something like that?

Name: Anonymous 2007-09-11 11:32 ID:1fd632Jb

>>23
Dude, you forgot to strip your executable. No fair comparing sizes when like half is symbol tables and so forth.

Name: Anonymous 2007-09-11 11:55 ID:5GYgZE+H

stripping only cut it to 270KB while >>23 not stripping it in Linux gets 8KB

Name: Anonymous 2007-09-11 12:18 ID:1fd632Jb

Yeah, I meant that in Linux stripping it would bring the exe down to some 5-odd KiB...

Still, that's what one gets by linking standard libraries statically. Win32's dynamic library system is rather the polar opposite to state of the art.

Name: Anonymous 2007-09-11 17:19 ID:JNhXe5jX

MinGW doesn't use msvcrt.dll I think

Name: Anonymous 2007-09-11 19:17 ID:Zy4fi+kf

>>30
you don't understand mingw

Name: Anonymous 2010-11-25 9:18

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