Broad Network


C++ typedef Specifier

Specifiers in C++ - Part 7

Forward: In this part of the series, I explain the C++ typedef Specifier.

By: Chrysanthus Date Published: 25 Aug 2012

Introduction

This is part 7 of my series, Specifiers in C++. In this part of the series, I explain the C++ typedef Specifier. The typedef specifier is used to create a typedef name. A typedef name is a synonym for one object type or a compound (composite) object type. It is the programmer who chooses the typedef name. The main syntax to create a typedef-name is:

    typedef existingType(s) typedef-name;

Here, typedef, is a reserved word. This syntax is modified in different circumstances.

The rest of this tutorial explains examples.

A Typedef Name for Single Object Type
In the following program, int, and INTer mean the same thing. Remember, C++ is case sensitive.

#include <iostream>
using namespace std;

typedef int INTer;

int main()
    {
        INTer myInt = 14;
        cout << myInt;

        return 0;
    }

Note how the typedef-name has been created in the global scope. Also note that in main, it is the INTer typedef-name that has been used to declare (define) the interger, myInt.

Typedef Name from a Composite Simple Type
Take for example, the simple composite type, “unsigned long int”. It can be used with the type-name, unint. Remember, the type name is the name, you the coder chooses. The following program illustrates this:

#include <iostream>
using namespace std;

typedef unsigned long int unint;

int main()
    {
        unint myInt = 5;
        cout << myInt;

        return 0;
    }

The typedef-name acts like an object type and has the advantage that it can replace several consecutive object types (composite).

Type Name for Classes
Comparing a class with a fundamental object (simple object type), a class name is like the object type, while the instantiated object is the object. In the following simple object type statement, the object type is, int and the object is myInt:

    int myInt = 12;

To have a synonym type name for a class type, the syntax is:

    typedef class Class-name Typedef-name;

This syntax is a modified form from the syntax given above. Note the use and position of the reserved word, class, in this syntax. The following example illustrates the use of typedef-name for the type, class:

#include <iostream>
using namespace std;

class Calculator
    {
        public:
        int num1;
        int num2;

        Calculator(int ident1, int ident2)
            {
                num1 = ident1;
                num2 = ident2;
            }

        int add ()
            {
                int sum = num1 + num2;
                return sum;
            }
    };

typedef class Calculator Calcu;

int main()
    {
        Calcu myObject(2,3);
        int result = myObject.add();
        cout << result;

        return 0;
    }

In this program, Calcu, is the typedef-name for “class Calculator”.

You can give a typedef-name while you are defining (describing) the class. The syntax is:

    typedef class Class-name { /* ... class description … */ } typedef-name;

The following program illustrates this:

#include <iostream>
using namespace std;

typedef class Calculator
    {
        public:
        int num1;
        int num2;

        Calculator(int ident1, int ident2)
            {
                num1 = ident1;
                num2 = ident2;
            }

        int add ()
            {
                int sum = num1 + num2;
                return sum;
            }
    } Calcu;


int main()
    {
        Calcu myObject(2,3);
        int result = myObject.add();
        cout << result;

        return 0;
    }

Here, the typedef name is, Calcu. It stands for the class name, Calculator.

Type Name for Struct
The struct is a class that does not have methods (member functions). However, watch out for the difference between instantiating a struct and the typedef-name at the end of the struct. The syntax for the typedef-name for the struct is similar to that of the class; it is:

    typedef struct Struct-name Typedef-name;

The following code illustrates this:

#include <iostream>
using namespace std;

int main()
    {
        struct MyTable {const char *name; int age; float salary; const char *HQ;};

        typedef struct MyTable TheTabl;

        TheTabl Emp1;
        Emp1.name = "Mary Jones";
        Emp1.age = 35;
        Emp1.salary = 3856.42;
        Emp1.HQ = "Phd";

        TheTabl Emp2;
        Emp2.name = "John Carlson";
        Emp2.age = 36;
        Emp2.salary = 2124.32;
        Emp2.HQ = "Msc";

        cout << Emp2.age;

        return 0;
    }

The typedef-name is, TheTabl; it stands for the struct name, MyTable.

You can give a typedef-name while you are defining (describing) the struct. The syntax is:

    typedef struct Struct-name { /* ... struct description … */ } typedef-name;

The following program illustrates this:

#include <iostream>
using namespace std;

int main()
    {
        typedef struct MyTable {const char *name; int age; float salary; const char *HQ;} TheTabl;

        TheTabl Emp1;
        Emp1.name = "Mary Jones";
        Emp1.age = 35;
        Emp1.salary = 3856.42;
        Emp1.HQ = "Phd";

        TheTabl Emp2;
        Emp2.name = "John Carlson";
        Emp2.age = 36;
        Emp2.salary = 2124.32;
        Emp2.HQ = "Msc";

        cout << Emp2.age;

        return 0;
    }

Typedef-name and Pointers
The syntax to create a typedef-name pointer type, is unfortunately similar to that of creating the normal pointer. It is:

    typedef type *typedef-name-pointer;

Normally, a pointer will be created similar to the following example:

    float hisFloat;
    float *myFltPtr;
    myFltPtr = &hisFloat;

A typedef-name pointer behaves in the same way as an ordinary pointer. A typedef-name pointer will be created similar to the following example:

        float hisFloat;
        typedef float *TydefFltPtr;

        TydefFltPtr ptr;
        ptr = &hisFloat;

The typedef-name pointer is a type name. So you have to use it to create the pointer, as in the statement:

        TydefFltPtr ptr;

Then you can assign the address to the pointer, as in:

        ptr = &hisFloat;

The following program illustrates this in an example:

#include <iostream>
using namespace std;

int main()
    {
        float hisFloat = 2.5;
        typedef float *TydefFltPtr;

        TydefFltPtr ptr;
        ptr = &hisFloat;

        cout << *ptr;

        return 0;
    }

Note: C++ does not have a pointer type. This section shows how a pointer type can be created, using the typedef specifier, in the lines:

        typedef float *TydefFltPtr;

        TydefFltPtr ptr;
        ptr = &hisFloat;

TydefFltPtr is a pointer type and ptr is the pointer object created from it. TydefFltPtr is similar to “int” as an object type with the myInt object created from int, like in:

    int myInt;

More than one Type-name in One Statement
You can create more than one type name in one statement. The following example is copied from the specification:

    typedef int MILES, *KLICKSP;

Here, MILES has been made an alternative name (typedef-name) for int; KLICKSP has been made a pointer type for an int.

Typedef-name for Templates
I am writing a whole series on template and containers. I address this issue, there.

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message