Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6748951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:37:49+00:00 2026-05-26T12:37:49+00:00

The question is pretty straightforward. To further clarify, what exactly is the difference between

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:37:49+00:00Added an answer on May 26, 2026 at 12:37 pm

    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
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Pretty straightforward question: On web apps they force the title of the popup to
My question is pretty straightforward: You are an executable file that outputs Access granted
The question is pretty straightforward. How do I convert a char[] to a char*
Pretty straightforward question. I use InnoDB for everything else, for a couple of reasons.
This is a pretty straightforward architectural question, however it's been niggling at me for
So pretty straightforward question. I have a c# .dll with a whole lot of
pretty straightforward question: Is there a good web services solution for Joomla1.5+? I've been
Pretty straightforward question here: is the enumeration order in a for loop guaranteed to
Pretty straightforward question. I'm building an app which reads data from a SQL Server
That's a pretty straightforward question isn't it? I'm just after a flat list of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.