string to int

Im trying to writn stirng to int funtion for my string class. Its for learnig purpose. but i cant seem to find the correct thing to add into atoi.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class String {
private:
	char *ptr;
public:
	String();
	String(char *s);
	String(const String &src);
	String cti(const String &src) {return atoi();}
	~String();

	String& operator=(const String &src) {cpy(src.ptr); return *this;}
	String& operator=(char *s) {cpy(s); return *this;}

	String operator+(char *s);
	operator char*() {return ptr; }
	int operator==(const String &other);
	bool operator<(const String &other);
	bool operator>(const String &other);

	void cat(char *s);
	void cpy(char *s);
};


i think its good enough for inlinig but i might be wrong. so any help plz.
atoi(src.ptr)
And cti should be static.

i think its good enough for inlinig but i might be wrong.
?
I think you may want to remove the parameter to cti and use ptr for the atoi call rather than making it static.
Last edited on
well i chose cti (convert to int). ok i added src.ptr but now how can i call the function.
The result of cti is supposed to be int?

int cti() {return atoi(ptr);}

Better:

int cti() const {return (ptr != 0) ? atoi(ptr) : 0;}
Last edited on
1 IntelliSense: identifier "cti" is undefined
vastrolorde wrote:
1 IntelliSense: identifier "cti" is undefined
So what? Do you have a question?
i have forgottne how to call my cti function
i have forgottne how to call my cti function

If you have it according to my interface you call it like that:

1
2
3
4
char c_str[] = "123"; // This because in String it is 'char *' and not 'const char *'
String s(c_str); // c_str may be modified at this point?
...
int x = s.cti(); // x is 123 
Topic archived. No new replies allowed.