typedef question.

This is a discussion on typedef question. within the C++ Programming forums, part of the General Programming Boards category; I've read that you can use typedef to create a alias of a datatype, as example: typedef long miles; // ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Location
    Norway
    Posts
    3

    typedef question.

    I've read that you can use typedef to create a alias of a datatype, as example:
    typedef long miles; // define miles as an alias for long

    but then I saw this code:

    typedef QWidget * (*createModule_t)(QWidget *, const char *, const QXmlAttributes *);
    typedef void (*destroyModule_t)(QWidget *);

    what happens in these 2 lines?
    (QXmlAttributes and QWidget are classes)
    Last edited by mikalv; 06-23-2008 at 04:40 PM.

  2. #2

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    The syntax for declaring typedefs is just like initializing objects.

    Code:
    // Pointer to function returning int:
    int (*ptr)();
    
    // Alias of 'pointer to a function returning int':
    typedef int(*ptr_t)();
    So in your example, createModule_t is a datatype equivalent to a pointer to a function taking 3 parameters and returning a QWidget*.

    destoryModule_t is a datatype equivalent to a pointer to a function taking a QWidget* and returning void.

  3. #3
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,898
    WNDPROC and THREAD_ENTRY_POINT are some good examples of this.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  4. #4
    Registered User
    Join Date
    Jun 2008
    Location
    Norway
    Posts
    3
    so that's means that it's a way to make custom datatypes?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,616
    Somewhat, though it is more like a way to give new names to existing types.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with ZwQueryDirectoryFile for my Driver.
    By taDo in forum Windows Programming
    Replies: 9
    Last Post: 11-27-2008, 07:54 AM
  2. Error: redefinition of typedef xxxx
    By Rocha in forum C Programming
    Replies: 2
    Last Post: 11-24-2008, 08:19 AM
  3. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  4. Help...typedef struct...confusing...
    By darkchild in forum C Programming
    Replies: 1
    Last Post: 01-23-2007, 07:03 AM
  5. question about typedef
    By volk in forum C++ Programming
    Replies: 8
    Last Post: 05-30-2003, 10:53 PM

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