CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Nov 2005
    Posts
    102

    [c++] typedef class

    If i have the following class:

    Code:
    class cBlaat
    {
    public:
       void SomeFunc();
    };
    
    typedef class cBlaat * LPBLAAT;
    Then what does that last line mean?? What could be the reason for this..??
    "typedef class cBlaat * LPBLAAT"

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: [c++] typedef class

    It just makes LPBLAAT an alias for the type cBlaat*. The class keyword in the typedef is unnecessary.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: [c++] typedef class

    [ Moved thread ]

  4. #4
    Join Date
    Nov 2005
    Posts
    102

    Re: [c++] typedef class

    Ok thanks, then i have another question about a class. Suppose i have the following code:

    Code:
    class cBlaat
    {
    public:
       DoSomething();
    };
    
    cBlaat * blaat;  //doesn't work
    
    int main()
    {
    blaat->DoSomething();
    return 0;
    }
    Why doesn't "cBlaat * blaat" work when i declare it outside of a function, but does it work when i declare it as "cBlaat blaat" ??
    I just don't get what's so different about it...

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: [c++] typedef class

    Quote Originally Posted by vivendi
    Why doesn't "cBlaat * blaat" work when i declare it outside of a function, but does it work when i declare it as "cBlaat blaat" ??
    It works, but looking at your sample code (which will not even compile), you probably tried to dereference the pointer when it did not point to an object (in this case, it is a null pointer).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: [c++] typedef class

    Quote Originally Posted by vivendi View Post
    Why doesn't "cBlaat * blaat" work when i declare it outside of a function, but does it work when i declare it as "cBlaat blaat" ??
    I just don't get what's so different about it...
    To expand on what laserlight said...

    The first
    Code:
    cBlaat *blaat;
    only declares a pointer to a cBlaat object - it does not create an instance of the object - you only have an invalid pointer at this point.

    The second
    Code:
    cBlaat blaat;
    actually creates an instance of the cBlaat object - so you actually have one of those objects existing and can operate on it.

    Hope that helps.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  7. #7
    Join Date
    Jan 2009
    Posts
    1,689

    Re: [c++] typedef class

    Quote Originally Posted by laserlight View Post
    It works, but looking at your sample code (which will not even compile), you probably tried to dereference the pointer when it did not point to an object (in this case, it is a null pointer).
    No it's not, it's an undefined pointer, unless my terminology of what a null pointer is is wrong. I think a null pointer is this:
    Code:
    cBlaat * blaat = NULL;

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: [c++] typedef class

    Quote Originally Posted by ninja9578
    No it's not, it's an undefined pointer, unless my terminology of what a null pointer is is wrong.
    Notice that it has static storage duration, hence it is zero initialised.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Oct 2008
    Location
    Singapore
    Posts
    195

    Re: [c++] typedef class

    Quote Originally Posted by laserlight View Post
    Notice that it has static storage duration, hence it is zero initialised.
    Yes, but I think it should compile (at least after looking at it, I don't see any compilation error). Runtime error? yes.

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: [c++] typedef class

    Quote Originally Posted by rohshall
    Yes, but I think it should compile (at least after looking at it, I don't see any compilation error).
    What's the return type of cBlaat::DoSomething()?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  11. #11
    Join Date
    Jan 2009
    Posts
    1,689

    Re: [c++] typedef class

    The function is also used, but never defined.

    Remember that
    Code:
    class foo {
       void DoSomething(void);
    };
    
    void foo::DoSomething(void){
       std::cout << "Hello World" << std::endl;
    }
    
    foo * bar;
    
    main(){
       bar -> DoSomething();
    }
    Is undefined behavior, but stable on almost every compiler.

    Also, why do statically declared pointers initialize to zero? Seems like a waste of a mov command to me. I always do this
    Code:
    foo * bar = 0;
    If I want to know if the pointer has been initialized or not later. If I omit the zero, I don't care what it is.
    Last edited by ninja9578; February 22nd, 2010 at 09:07 AM.

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: [c++] typedef class

    Quote Originally Posted by ninja9578 View Post
    Also, why do statically declared pointers initialize to zero? Seems like a waste of a mov command to me.
    If it were not initialized to 0, then the compiler is broken. The rule that globals and statics are initialized to 0 goes back to 'C' programming.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured