1

The question is pretty straightforward. To further clarify, what exactly is the difference between Foo1 and Foo2 in the code below in terms of the way they are declared (e.g. one using class Foo1 { ... }; and the other using typedef class { ... } Foo2;)?

class Foo1 {
public:
    void bar() { }
};

typedef class {
public:
    void bar() { }
} Foo2;

int main()
{
    Foo1 f1;
    f1.bar();
    Foo2 f2;
    f2.bar();
    return 0;
}
3
  • 1
    The differences are: the second one is confusing to C++ programmers, and you cannot instantiate any instances at the declaration. Nov 1, 2011 at 17:15
  • @MooingDuck - why didn't you post it as an answer?
    – littleadv
    Nov 1, 2011 at 17:20
  • @littleadv: probably because confusion is subjective... I know quite a few C++ programmers that would not be confused (some of them even understand the difference) Nov 1, 2011 at 17:22

2 Answers 2

6

The difference is subtle. In the first case you are creating a class with name Foo1, while in the second case you are creating an annonymous class and using a typedef to provide an aliased name Foo2.

The third option would be:

typedef class Foo3 {
public:
   void bar() {}
} Foo3;

That would create a class named Foo3 and create an alias Foo3 to refer to it.

The subtleties are in how identifiers are handled in the language. When an identifier is present in the code the compiler will perform lookup to know what it means. The lookup will check in each scope, first in the global identifier space where most symbols (excluding user defined types) are defined, if it fails to locate the identifier there it will then look in the user-defined identifier space. User defined types belong to the second identifier space, while typedef-ed names are present in the first group. Only if both lookups fail, the compiler will move on to the next enclosing scope.

To provide a simple test case where the differences are notable:

class A {};
typedef class {} B;
typedef class C {} C;
void A();              // correct: no collision
//void B();            // error, identifier B already used with a different meaning!
//void C();            //   "
namespace test {
   void B();
   void C();
   void f() {
      class A a;       // creates variable of type ::A
      A();             // calls function ::A
      B();             // calls function test::B()
      //class B b;     // error: B does not denote a user-defined type name
      C();             // calls function test::C()
      class C c;       // creates variable of type ::C
   }
}
6
  • I'm curious, why are typedefs in the first group?
    – Pubby
    Nov 1, 2011 at 17:25
  • @Pubby8: This comes from C. In C, the compiler does not check the user defined types unless the programmer explicitly requests it: struct A a would only lookup inside the user defined types, while A() will only lookup in the general identifier scope. In C the purpose of typedef is to create an alias in the other identifier space so that you do not need to tell the compiler that you are referring to a type in all usages: typedef struct A {} A; A a; will find A as the typedef-ed name, and users can skip adding the struct in front of every use of the type. Nov 1, 2011 at 17:29
  • @DavidRodríguez-dribeas It's a little more subtle in the second case: "If the typedef declaration defines an unnamed class (or enum), the first typedef-name declared by the declaration to be that class type (or enum type) is used to denote the class type (or enum type) for linkage purposes only." (§7.1.3/9. And don't ask me exactly what is meant by "for linkage purposes only".) Nov 1, 2011 at 17:35
  • @JamesKanze: As I understood it some time ago it means that lookup will perform as stated above, but because name mangling needs a name for the type, and the member functions it has to pick up the name of the first typedef. I believe there used to be another corner case around template instantiations, where again the first typedef name will be used to identify what instantiation to trigger typedef struct {} A; typedef A B; std::vector<B> v1; std::vector<A> v2; both instantiate the equivalent of std::vector<A>, but I thought those were a bit beyond a basic description of what they do... Nov 1, 2011 at 17:42
  • @DavidRodríguez-dribeas Your understanding sounds as reasonable as anything else. But whether the type has a name or not does have some impact: if you don't give it a name (either directly or via typedef), then there are some things you can't do with it, like instantiate a template, or refer to it in a different source. Nov 1, 2011 at 18:24
-1

I believe that the second was necessary for C, but is really unnecessary for C++. Internally I belive that the second one is creating a unnamed class, and then creating a typedef for that, while the first is just creating the class.

I don't think that the typedef style class declaration is needed in c++ libraries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.