Online Book Reader

Home Category

Professional C__ - Marc Gregoire [145]

By Root 1202 0
So it is perfectly valid, given the above definitions, to write the following, because they are not just “compatible” types, they are the same type:

p1 = p2;

p2 = p1;

The most common use of typedefs is to provide manageable names when the real type declarations become too unwieldy. This situation commonly arises with templates. For example, Chapter 1 introduces the std::vector from the standard library. To declare a vector of strings, you need to declare it as std::vector. It’s a templated class, and thus requires you to specify the template parameters anytime you want to refer to the type of this vector. Templates are discussed in detail in Chapter 19. For declaring variables, specifying function parameters, and so on, you would have to write std::vector:

void processVector(const std::vector& vec) { /* omitted */ }

int main()

{

std::vector myVector;

return 0;

}

Code snippet from Typedefs\Typedefs.cpp

With a typedef, you can create a shorter, more meaningful, name:

typedef std::vector StringVector;

void processVector(const StringVector& vec) { /* omitted */ }

int main()

{

StringVector myVector;

return 0;

}

Code snippet from Typedefs\Typedefs.cpp

typedefs can include the scope qualifiers. The preceding example shows this by including the scope std for StringVector.

The STL uses typedefs extensively to provide shorter names for types. For example, string is actually a typedef that looks like this:

typedef basic_string string;

typedefs for Function Pointers

The most convoluted use of typedefs is when defining function pointers. While function pointers in C++ are uncommon (being replaced by virtual methods), there are needs to obtain function pointers in certain cases.

Perhaps the most common example of this is when obtaining a pointer to a function in a dynamic link library. The following example obtains a pointer to a function in a Microsoft Windows DLL. Details of Windows DLLs are outside the scope of this book on platform-independent C++, however, it is so important to Windows programmers that it is worth discussing and it is a good example to explain the details of function pointers in general.

Consider a Dynamic Link Library (DLL) that has a function called MyFunc(). You would like to load this library only if you need to call MyFunc(). This at run time loading of the library is done with the Windows LoadLibrary() kernel call:

HMODULE lib = ::LoadLibrary(_T("library name"));

The result of this call is what is called a “library handle” and will be NULL if there is an error.

Before you can load the function from the library, you need to know the prototype for the function. Suppose the following is the prototype for the MyFunc() function which returns an integer and accepts three parameters: a Boolean, an integer and a C-style string.

int __stdcall MyFunc(bool b, int n, const char* p);

The __stdcall is a Microsoft-specific directive to specify how parameters are passed to the function and cleaned up.

You can now use a typedef to define a short name (MyFuncProc) for a pointer to a function with the preceding prototype.

typedef int (__stdcall *MyFuncProc)(bool b, int n, const char* p);

Note that the typedef name MyFuncProc is embedded in the middle of the syntax. It is clear from this example that these kinds of typedefs are rather convoluted. Unfortunately, this is the syntax required by the standard. The following section will show you a new and cleaner C++11 solution to this problem.

Having successfully loaded the library and defined a short name for the function pointer, you can get a pointer to the function in the library as follows:

MyFuncProc MyProc = ::GetProcAddress(lib, "MyFunc");

If this fails, MyProc will be NULL. If it succeeds, you can call the loaded function as follows:

MyProc(true, 3, "Hello world");

A C programmer might think that you need to dereference the function pointer before calling it as follows:

(*MyProc)(true, 3, "Hello world");

This was true decades ago, but now, every C and C++ compiler

Return Main Page Previous Page Next Page

®Online Book Reader