Well, first off, it might be a good idea to use proper naming and some conventions:
stuff
template parameter doesn't really tell the user what is it doing. Consider using something like Element
or TElement
. Or T
if you're not in a creative mood.
safearray
is not really legible, consider using something that can help reader distinguish the words, e.g. safe_array
, SafeArray
Then, it would be good to work on some indentation. You can either do
void SomeMethod() {
// code here
}
or
void SomeMethod()
{
// code here
}
Next, your idea of what is constructor is wrong. The basic idea is that constructor is a method that has no return type and has the same name as containing class, e.g.
class SomeClass
{
private: // private marks the internal state of the class
// it is inaccessible from the outside of the class
int someValue;
// it might be a good idea to store the size of the array here
// so the user cannot modify the size
public:
SomeClass()
{
someValue = 0;
}
SomeClass(int value)
{
someValue = value;
}
};
And then you obviously call constructor by using the type:
void SomeMethod()
{
SomeClass cl1; // creates an instance of SomeClass on stack calling the
// parameter-less constructor
SomeClass cl2(7); // creates an instance of SomeClass on stack with
// the parametrized constructor
SomeClass* cl3 = new SomeClass(12); // creates an instance of SomeClass on heap
// with the parametrized constructor
delete cl3; // remember to delete everything that has been created with new
}
If you need to create an array of something, you would do
size_t size = 6; // your required size comes here, e.g. constructor parameter
int* array = new int[size];
Having template parameter T, you would obviously need to do
T* array = new T[size];
So this was really a fast and not very precise walkthrough on this stuff. If I were you, I'd take the recommended books in comments and start learning from scratch. This level of assignment is crazy for your level of skill and won't give you any deeper understanding of programming or C++ at all.
PS. I'm a student :)