my own string

i'm creating an custom string...I'm bulding it up from simpler to complex...I'm doing the cosntructos now, but I have got stucked doing the constructor which get a literal string....because I should have to move the string literal to the T* ptr.....here I leave my code...thanks!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 
#include <iostream>
#include <stdio.h>
#include <cstring>

template<typename C>
class String{
private:
	static const int short_max = 15;
	int size;
	char* ptr;
	
public:
	String():size(0),ptr(nullptr){};
	String(int s){
		size(s);
		ptr = new C [s];
		for(int i = 0;i <=size;i++){
			ptr[i]= '\0';
		}
		
	}
	String(C& c){
		size(c.size);
		ptr = new C [c.size];
		for(int i = 0;i <=c.size;i++){
			ptr[i] = c.ptr[i];
		}
		
	}
	String(const std::string&& str){ //here is the problem
		size = str.length();
		ptr = new C [size];
		
		}
	
	
	C& operator[](int n){return ptr[n];}
	
	String& operator+=(C c){
		size_t len = strlen(ptr);     
		ptr[len + 1] = c;
		return *this;
	}
	
};


int main(){
	
	return 0;
}


I have kind of found a solution, but it's not working..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
String(const std::string&& str){ //here is the problem
		size = str.length();
		ptr = new C [size];
		std::stringstream line(str);
		C unit;
		int cont = 0;
		while(line>>unit){
			ptr[cont] = unit;
			cont++;
		}
			ptr[cont] = '\0';
		
	}


but declaring line it apparently has an incomplete type...but the constructor os stringstream for c++11 allows to get a string literal ( a move in fact)....
Definitely It's related with the String call here is my main...i mean the call....
1
2
3
4
5
6
7
8

int main(){
	
	String<char> my{"this is not working"};
	
	return 0;
}
Topic archived. No new replies allowed.