Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

ham2233

functions and asterisks

This topic is 5892 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

just a c++ question. In the book i''m working out of, they use an asterisk in a function definition in a class definition: class CInputSystem { //other stuff here CKeyboard *GetKeyboard() { return m_pKeyboard; } //other stuff } what does this mean/do? how is it different from a standard function definition? thanks, justin

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by ham2233
just a c++ question. In the book i''m working out of, they use an asterisk in a function definition in a class definition:

class CInputSystem
{
//other stuff here
CKeyboard *GetKeyboard() { return m_pKeyboard; }
//other stuff
}

what does this mean/do? how is it different from a standard function definition?

thanks,
justin





You''re working with classes but haven''t a faintest idea what a pointer is? Burn that book you''re using and buy something better.

Share this post


Link to post
Share on other sites
The line:

CKeyboard *GetKeyboard() { return m_pKeyboard; }

defines a function called GetKeyboard that takes no args and returns a pointer to a CKeyboard object.

w/o the * the function would return a CKeyboard object itself.

zin

zintel.com - 3d graphics & more or less

Share this post


Link to post
Share on other sites
I know what pointers are. Most of my interaction with them has been with variables though and pointers to variables. I''m just wondering why you''d need a pointer to a function. What use is this? How does it work?

Thanks

Share this post


Link to post
Share on other sites
when you define a function like:

int * foo() { //do stuff }

it means that funciton foo will return a pointer to an int. it doesn''t mean pointer to a function..

you should re-do the chapters on pointers and look at the sub-section on returning pointers from functions. if the book doesn''t have that sub-section get another book

-me

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
No you don''t know what pointers are. Lose the ego and learn something.

You also obviouslt aren''t clear on how a function is declared.

And, to answer your question: it''s not a pointer to a function, it''s a function returning a pointer.

Share this post


Link to post
Share on other sites
quote:
Original post by ham2233
I know what pointers are. Most of my interaction with them has been with variables though and pointers to variables. I''m just wondering why you''d need a pointer to a function. What use is this? How does it work?

Thanks




Well, there are MANY places where you''d need a pointer to a function. You really can''t write a windows program without sending a pointer to your WndProc function to Windows. And if you use class inheritance with virtual functions, then you are always using function pointers, even if you don''t explicitly write the code.

But I won''t go into too much detail about function pointers, because the example you gave simply IS NOT a pointer to a function. It is a function that returns a pointer to a CKeyboard object. You can return pointers just like you return ints or floats.

This is why I prefer the syntax:
CKeyboard* GetKeyboard() { return m_pKeyboard; }

to how they have it in the book:
CKeyboard *GetKeyboard() { return m_pKeyboard; }

But either are correct.

--TheMuuj

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You say your knowledge of pointers is mainly of declaring them as variables. Well, much as a function might return an int or float value (no doubt after doing some wonderful calculation on the arguments you sent it) a function can return a pointer to a variable or object.
This may be faster than sending the whole object, and uses less space as the object will not needlessly be copied to another place in memory.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!