Qualified typedef

This is a discussion on Qualified typedef within the C++ Programming forums, part of the General Programming Boards category; Hello all, I recently came across an example of a class-qualified typedef being used like this: Code: #include <iostream> using ...

  1. #1
    DL1
    DL1 is offline
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99

    Qualified typedef

    Hello all,

    I recently came across an example of a class-qualified typedef being used like this:

    Code:
    #include <iostream>
    using namespace std;
    
    class X {};
    
    typedef X* C;
    
    template<class T> class A
    {
        public:
        typedef int C;
        typename A::C d; // <---HERE
    };
    
    int main()
    {
        A<X> a;
        a.d = 23;
        cout << a.d;
    }
    (It was here: Help - IBM Mac OS X Compilers)

    I was a bit surprised by this, since I have never seen a class typedef being used like this before. Can a typedef be qualified in the way any other name can?

    Also, is there any reason to do this other than for illustrative purposes, as in this example showing how typename is used? Given that the class typedef hides the global typedef, I'd have written C d; rather than typename A::C d;!

    Thanks.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    This is for mostly for illustration purposes. But when you get into nasty template programming, this is quite useful.
    For example, we can specialize class A and depending on the type T, the typedef contained inside may be a completely different type!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Typedef names are names like any others and thus can be qualified.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    DL1
    DL1 is offline
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    But in that case, wouldn't C d; in class scope refer to the right type for the particular specialization in questiion?

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Yeah it would. So it's an illustration rather than a good example.
    It's different outside the class, of course.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    DL1
    DL1 is offline
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    OK.

    Thanks to both of you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 08:22 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 06:35 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21