what's this?

B

bakzam

#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)
 
J

John Harrison

#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)

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
 
C

comp.lang.superman

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?
 
K

Kai-Uwe Bux

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?

Try:

typedef int * int_ptr;

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


Best

Kai-Uwe Bux
 
C

comp.lang.superman

Try:

typedef int * int_ptr;

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

}

Best

Kai-Uwe Bux

Goodness! This works! It seems it is due to C++ grammar. Or is it?
 
B

Bo Persson

(e-mail address removed) wrote:
:: On Jun 25, 1:57 am, John Harrison <[email protected]>
:: wrote:
::: (e-mail address removed) 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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top