Archived

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

functions and asterisks

This topic is 5672 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
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
thanks guys for the info. the information that i've read on pointers didn't really go into how they're used in return values from a function, and I just needed someone to clear up that the fuction is just returning a pointer to the return variable.

Thanks,
Justin

p.s. anonymous poster (at 4 June 2002 4:19:37 PM, not just a moment ago), its people like you that ruin forums like this. don't just go into threads and try to tell people that they're stupid. I wanted you to answer my question, not tell me that i don't how to declare functions, asshole.

[edited by - ham2233 on June 4, 2002 4:38:51 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by ham2233
functions and asterisks


So, you know what a pointer is? So why did you write “functions and asterisks” and not something like “function pointers”, or “pointer to a function”.

I doubt you know a lot about pointers.


Share this post


Link to post
Share on other sites
i wrote that because i know what pointers are, but had never seen them in the context of function return values. my c++ is self-taught, and the resources i used only superficially talked about pointers to variables in the body of a function. Now I know that, in the context of functions, the asterisk is signifying the pointer to the returned value.

Thanks

Share this post


Link to post
Share on other sites
quote:
Original post by ham2233
p.s. anonymous poster (at 4 June 2002 4:19:37 PM, not just a moment ago), its people like you that ruin forums like this. don''t just go into threads and try to tell people that they''re stupid. I wanted you to answer my question, not tell me that i don''t how to declare functions, asshole.

[edited by - ham2233 on June 4, 2002 4:38:51 PM]


Chill out asshole!
He DID answer your question, sheese!



Share this post


Link to post
Share on other sites
yeah, and so did two other people before him without saying:
No you don''t know what pointers are. Lose the ego and learn something.

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

there are much more gracious and benign ways of answering a question.

Share this post


Link to post
Share on other sites
It''s pretty clear that you''re working slightly above your ability level. (See Here for another example.) The AP could have been more tactful in stating this, but his points were accurate. You probably haven''t been working with pointers for very long, or you''d have stumbled across this already.

It''s the common belief on these boards that you should have a good grounding in the basics before trying to work through a book like ''OpenGL Game Programming.''

I''d recommend that you back up and work through a good C++ book. (check one out from the library if you don''t have one and can''t afford it.) You''ll find that it will end up taking you a lot less time to learn C, _then_ learn game programming. Learning both at the same time is simply too many new things at once.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You are all stupid. I own everyone here with my QBasic skillz

Share this post


Link to post
Share on other sites
I think it''s a good idea to learn game programming, before fully grasping the language at hand. I thought I knew the ins and outs of C/C++...but after reading Advanced 3D Game-Programming, I realized I was missing a lot. Now I think I''m an expert...I could be wrong though...hehehe

oh..and swearing at someone who''s blunt with you...that''s the last thing you want to do

Share this post


Link to post
Share on other sites
I think the anonymous poster has got us, though. We don't even match up to his mad qbasic skills. On that note:

I understand your point, cheesegrater. Certainly I am working above my ability level. That's how people learn. For me, the most effective way to learn programming languages/techniques is by working with them, not memorizing text books.

You're right, I haven't been working with pointers for very long. But that doesn't mean that i don't know what i'm doing with the whole language. I just happened that in all that I had learned thus far in my c++ excursions, I hadn't yet encountered pointers as return values.

I appreciate your insight, and I agree that a good grounding in programming is necessary. I have been programming for 10 years, however, and the way I learn programming works fine.

In regard to your reference to another one of my posts:
Many c++ books don't go into windows programming, so the first windows programming I encountered was in OpenGL Game Programming. It is actually a relatively good resource for learning basic windows programming, but, as in other sections of the book, it sometimes leaves out things that may seem intuitive. The fact that I didn't just know to specifically set my project in Visual C++ as a win32 app was an honest mistake. It seems to me that problems like that are one of the main reasons that these message boards exist.

Justin


[edited by - ham2233 on June 4, 2002 6:48:14 PM]

Share this post


Link to post
Share on other sites