Function templates and rule of five

I have the following code:

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
class ruleOfFive {
	char* aString;
    public:
	ruleofFive(const char* arg) : aString(new char[std:strlen(arg) + 1]) {
		std::strcpy(aString, arg);
	}
	~ruleOfFive() {
		delete[] aString;
	}
	ruleOfFive(const ruleOfFive& other) {
		aString = new char[std::strlen(other.aString) + 1]);
		std::strcpy(aString, other.aString);
		
	}
	ruleOfFive& operator=(const ruleOfFive& other) {
		if (this != &other) {
			delete[] aString;
			aString = new char[std::strlen(other.aString) + 1]);
			std::strcpy(aString, other.aString);
		}
		return *this;
	}
	ruleOfFive& operator=(const ruleOfFive&& other) {
		if (this != &other) {
			delete[] aString;
			aString = other.aString;
			other.aString = nullptr;
		}
		return *this;
	}
};


How can I convert this into a template? Would adding template <class ruleOfFive> at the top be sufficient?

What are you trying to accomplish by making it a template?
It's for a test that I have in 20 minutes, actually. It will be on the rule of five and function templates, and I just assumed we'll have to do the rule of five in a template?
The rule of five has nothing specifically to do with templates. It's a rule that is applicable to classes in general, not just class templates.

To answer your initial question, template <class ruleOfFive> would not work because the template parameter can't have the same name as the class template. You need to use a different name, e.g. template <class T>, but if T is never used inside ruleOfFive it would be kind of pointless.
Last edited on
Topic archived. No new replies allowed.