what's this?

Discussion in 'C++' started by bakzam, Jun 24, 2007.

  1. bakzam

    bakzam Guest

    #include<iostream>

    struct zaman{
    float p;
    };

    typedef int (*func)(zaman, int);

    int main(){
    func(-1); //Hmmm. what's this?
    std::getchar();
    return 0;
    }

    why is func(-1) working?


    Actually, I found this (equivalent code) in DON BOX's book

    --------------------
    typedef HRESULT (*INTERFACE_FINDER) (void *pThis, DWORD dwData, REFIID
    riid, void **ppv);

    //pseudo-function to indicate entry is just an offset
    #define ENTRY_IS_OFFSET INTERFACE_FINDER(-1)
     
    bakzam, Jun 24, 2007
    #1
    1. Advertisements

  2. func is a type, INTERFACE_FINDER is a type, so they can't be function
    calls. Instead they're casts, -1 cast to a function pointer.

    john
     
    John Harrison, Jun 24, 2007
    #2
    1. Advertisements

  3. bakzam

    Ian Collins Guest

    wrote:

    Please don't multi-post on Usenet.
     
    Ian Collins, Jun 24, 2007
    #3
  4. I am sorry, that's why I deleted it. So, if it is a cast why doesn't
    this work in c++

    int* (-1);

    but func(-1); works?
     
    comp.lang.superman, Jun 24, 2007
    #4
  5. bakzam

    Kai-Uwe Bux Guest

    Try:

    typedef int * int_ptr;

    int main ( void ) {
    int_ptr(-1);
    }


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jun 24, 2007
    #5
  6. Goodness! This works! It seems it is due to C++ grammar. Or is it?
     
    comp.lang.superman, Jun 24, 2007
    #6
  7. bakzam

    Bo Persson Guest

    wrote:
    :: On Jun 25, 1:57 am, John Harrison <>
    :: wrote:
    ::: wrote:
    :::: #include<iostream>
    :::
    :::: struct zaman{
    :::: float p;
    :::: };
    :::
    :::: typedef int (*func)(zaman, int);
    :::
    :::: int main(){
    :::: func(-1); //Hmmm. what's this?
    :::: std::getchar();
    :::: return 0;
    :::: }
    :::
    :::: why is func(-1) working?
    :::
    :::: Actually, I found this (equivalent code) in DON BOX's book
    :::
    :::: --------------------
    :::: typedef HRESULT (*INTERFACE_FINDER) (void *pThis, DWORD dwData,
    :::: REFIID riid, void **ppv);
    :::
    :::: //pseudo-function to indicate entry is just an offset
    :::: #define ENTRY_IS_OFFSET INTERFACE_FINDER(-1)
    :::: --------------
    :::
    :::: Isn't INTERFACE_FINDER(-1) a call to a function? How is it an
    :::: offset?
    :::
    ::: func is a type, INTERFACE_FINDER is a type, so they can't be
    ::: function calls. Instead they're casts, -1 cast to a function
    ::: pointer.
    :::
    ::: john
    ::
    :: I am sorry, that's why I deleted it. So, if it is a cast why
    :: doesn't this work in c++
    ::
    :: int* (-1);
    ::
    :: but func(-1); works?

    Because int* is two tokens, and breaks the syntax (which is just
    crazy).

    If you try

    typedef int* ip;

    ip(-1);

    it compiles (but doesn't do much).


    In C++ you should really use the cast syntax

    static_cast<int*>(-1)

    because then the compiler will tell you that it really doesn't work.
    Neither does ip(-1), of course.


    Bo Persson
     
    Bo Persson, Jun 24, 2007
    #7
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.