Hello Community,
It seems, that this time I met my 'master' in this problem. Meaning, I do not have any clue how to approach this problem, and here is why.
-
Problem statement:
"In this chapter, the section Specialized Templates within Section 16.4 describes how to design templates that are specialized for one particular data type."
This sounds like there is a code example given in Section 16.4. That, if having paid even a little attention as to what is written, I should know how to do it. To remedy this suspicion upfront, here is the part that describes how it is done. First, the section contains 10 lines of text. Here cited is only the important part:
"For example, the declaration of a specialized version of the SimpleVector class might start like this:
class SimpleVector<char *>
The compiler would know that this version of the SimpleVector class is intended for the char * data type. Anytime an object is defined of the type SimpleVector<char *>, the compiler will use this template to generate the code."
-
The problem statement then goes on to say, (my own words: with this thurough knowledge):
"The section introduces a method for specializing a version of the SimpleVector class template so it will work with strings. Complete the specialization for both the SimpleVector and SearchableVector templates."
-
Lovely, isn't it? With a single line of code, that I don't even know how to use, it is my task now to specialize 2 template classes, consisting of more than 500 lines of code, so they exclusively work with that data type.
After trying for several hours of little progress working something out, trying things, I decided to start with the already finished classes, one of them at least, from scratch. Here is the code (not separated in header files) and driver program.
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 56 57 58
|
#include <iostream>
using std::cin;
using std::cout;
#include <iostream>
// The template class
template <class T>
class SimpleVector
{
private:
T *aptr;
size_t arrSize;
public:
SimpleVector();
SimpleVector(size_t size);
};
template <class T>
SimpleVector<T>::SimpleVector()
{}
template <class T>
SimpleVector<T>::SimpleVector(size_t sizeVal) : arrSize(sizeVal)
{}
// The specialized template working with c-strings
template<>
class SimpleVector<char *>
{
private:
char *value;
size_t arrSize;
public:
SimpleVector<char *>::SimpleVector(char *v) : value(v)
{
std::cout << value << std::endl;
}
SimpleVector<char *>::SimpleVector(size_t sizeVal) : arrSize(sizeVal)
{}
};
int main()
{
const size_t SIZE = 2952;
char *vSen { "「はじめからはじめるがよい。そして最後にくるまでつづけるのじゃ。そうしたらとまれ」" };
char *vTrans{ "Begin at the beginning. Go on till you come to the end. Then stop." };
SimpleVector<char *> l(SIZE);
l = vSen;
l = vTrans;
return 0;
}
|
This works, but I have not the slightest clue if this is even right, meaning the constructor in the specialized class's constructor, ... So, are there any mistakes in there so far?
-
Also, here is part of the original class, which shall be modified, to give you an impression of what still need to be done, and how to approach modyfing the parts of that class in the specialized class:
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
|
#ifndef SIMPLE_VECTOR_H_
#define SIMPLE_VECTOR_H_
#include <cstdlib>
#include <iostream>
#include <new>
template <class T>
class SimpleVector
{
private:
T *aptr;
int arraySize;
int maxCapacity;
void memError();
void subError();
template <class TPrint>
friend std::ostream &operator << (std::ostream &, const SimpleVector<TPrint> &);
public:
SimpleVector();
SimpleVector(int);
SimpleVector(const SimpleVector &);
~SimpleVector();
// Mutator functions
void push_back(const T &);
void pop_back();
// Accessor functions
int getArraySize() const
{ return arraySize; }
T getElementAt(int);
T &operator [](const int &);
};
|
-
If you happen to have read this far, thank you if you did, I ask only for general guidance, a 'For Dummies' explanation in pseudocode, 'simple' function definitions in the specialized class, information of that nature.
[Edit:] It does not compile in cppshell, but works and does not give any warnings in VS, with Error-Level 4 set in the debugger.