Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of typedefing a class in C++?

Tags:

c++

typedef

I've seen code like the following frequently in some C++ code I'm looking at:

typedef class SomeClass SomeClass;

I'm stumped as to what this actually achieves. It seems like this wouldn't change anything. What do typedefs like this do? And if this does something useful, is it worth the extra effort?

like image 778
Neil Williams Avatar asked Jun 04 '09 21:06

Neil Williams


2 Answers

It prevents code like this from compiling:

class SomeClass { ... };
int SomeClass;

This is perfectly legal C++, though it's terrible. If you do this, then any references to a bare SomeClass refer to the variable. To refer to the class, you need to explicitly say class SomeClass at each usage. If you create a typedef:

class SomeClass { ... };
typedef class SomeClass SomeClass;
int SomeClass;

Then the compiler flags the definition of int SomeClass as an error, as it rightly should be.

like image 88
Adam Rosenfield Avatar answered Sep 20 '22 01:09

Adam Rosenfield


See this previous answer to a related question. It's a long quote from a Dan Saks article that explains this issue as clearly as anything I've come across:

Difference between 'struct' and 'typedef struct' in C++?

The technique can prevent actual problems (though admittedly rare problems).

It's a cheap bit of insurance - it's zero cost at runtime or in code space (the only cost is the few bytes in the source file), but the protection you get is so small that it's uncommon to see someone use it consistently. I have a 'new class' snippet that includes the typedef, but if I actually code up a class from scratch without using the snippet, I almost never bother (or is it remember?) to add the typedef.

So I'd say I disagree with most of the opinions given here - it is worth putting those typedefs in, but not enough that I'd give anyone (including myself) grief about not putting them in.

I've been asked for an example of how not having a class name typedef'ed can result in unexpected behavior - here's an example lifted more or less from the Saks article:

#include <iostream>
#include <string>

using namespace std;

#if 0   // change to #if 1 to get different behavior
        // imagine that this is buried in some header
        // and even worse - it gets added to a header 
        //     during maintenance...
string foo()
{
    return "function foo... \n";
}
#endif

class foo
{
public:
    operator string() {
        return "class foo...\n";
    }
};

int main()
{
    string s = foo();

    printf( "%s\n", s.c_str());
    return 0;
}

When the function declaration is made visible, the behavior of the program silently changes because there is no name conflict between the function foo and the class foo.

However, if you include a "typedef class foo foo;" you'll get a compile time error instead of a silent difference in behavior.

like image 37
Michael Burr Avatar answered Sep 24 '22 01:09

Michael Burr