How to Build a Structure Template in C++ - dummies

How to Build a Structure Template in C++

By John Paul Mueller, Jeff Cogswell

Structure templates have many interesting uses, such as creating a data repository that doesn’t depend on a particular type. The StructureTemplate example here shows one such use.

#include <iostream>
using namespace std;
template<typename T>
struct Volume
{
    T height;
    T width;
    T length;
    Volume()
    {
        height = 0;
        width = 0;
        length = 0;
    }
    T getvolume()
    {
        return height * width * length;
    }
    T getvolume(T H, T W, T L)
    {
        height = H;
        width = W;
        length = L;
        return height * width * length;
    }
};
int main()
{
    Volume<int> first;
    cout << "First volume: " << first.getvolume() << endl;
    first.height = 2;
    first.width = 3;
    first.length = 4;
    cout << "First volume: " << first.getvolume() << endl;
    Volume<double> second;
    cout << "Second volume: " << second.getvolume(2.1, 3.2, 4.3) << endl;
    cout << "Height: " << second.height << endl;
    cout << "Width: " << second.width << endl;
    cout << "Length: " << second.length << endl;
    return 0;
}

In this case, the structure contains height, width, and length data values that the code can use to determine volume. The structure includes a constructor to initialize the values, so even if someone calls getvolume() without initializing the structure, nothing bad will happen.

The structure allows independent access of each of the data values. You can set or get them as needed.

The getvolume() function is overloaded. You can call it with or without input values. The code in main() tests the structure thoroughly. Here’s what you see as output from this example:

First volume: 0
First volume: 24
Second volume: 28.896
Height: 2.1
Width: 3.2
Length: 4.3

You can use structures for another interesting purpose. The C++ standard says you can’t create a typedef template. For example, the following code produces an error when you try to compile it:

template<typename T>
typedef map<string, T> MyDef;

When you try to compile this code in Code::Blocks, you see the following error:

error: template declaration of 'typedef'

However, you can define a typedef within a structure template. The StructureTemplate2 example code here shows how.

#include <iostream>
#include <map>
using namespace std;
template<typename T>
struct MyDef
{
    typedef map<string, T> Type;
};
int main()
{
    MyDef<string>::Type marriages;
    marriages["Tom"] = "Suzy";
    marriages["Harry"] = "Harriet";
    cout << marriages["Tom"] << endl;
    cout << marriages["Harry"] << endl;
    return 0;
}

This example overcomes the C++ limitations by placing the typedef within the struct, MyDef. The same structure can hold any number of typedef entries.

Using a typedef in this manner makes it easier to work with map. All you need to worry about is the value type — the key type is already defined as string.