🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

C++ Workshop - Static Properties and Function Pointers(Ch. 15)

Started by
7 comments, last by Code Zombie 15 years, 11 months ago

Welcome to the GDNet C++ Workshop – Ch. 15

For a complete introduction to this workshop, please look here. Workshop Overview This workshop is designed to aid people in their journey to learn beginning C++. This workshop is targeted at highly motivated individuals who are interested in learning C++ or who have attempted to learn C++ in the past, but found that without sufficient support and mentoring they were unable to connect all the pieces of this highly complex but powerful programming language. This is a 'guided' self-teaching C++ workshop. Each student is responsible for taking the time to read the material and learn the information. The community and tutors that arise out of this workshop are here for making the learning process run more smoothly, but are not obligated to baby-sit a person's progress. Because everyone will be working from the same textbook (Teach Yourself C++ in 21 days 5th Ed.), students may find it easier to get answers to the specific questions they might have. There is no minimum age requirement, and there is no previous programming experience required. Additionally, this workshop does not attempt to defend C++ as a language, nor does it attempt to demonstrate that C++ is either more or less useful then other programming languages for any particular purpose. People who intend to start a discussion about the differences between C++ and ANY other languages (except as are relevant to a particular discussion), are encouraged to do so elsewhere. This workshop is for educational, not philosophical discussions. Quizzes & Exercises Each week will have quizzes and exercises posted in the weekly threads. Please try and answer them by yourself. As well, please DO NOT post the answers to Quizzes and Exercises within this thread. Once it becomes acceptable to post the answers to quizzes and exercises, an additional thread will be created each week specifically for the purpose of posting quiz answers. If you try with reasonable effort but are unable to answer the questions or complete the exercises, feel free to post a clarification question here on the thread. Tutors, myself, or others will do the best we can to point you in the right direction for finding the answer.

Chapter 15 – Special Classes and Functions

Introduction Hello once again! This week we'll be covering what may be considered some of the more advanced concepts when working with C++ functions. These include pointers to functions and pointers to member functions. Additionally, we'll look at static member data which is shared across all instances of a class. All in all, not a complicated set of topics, but I'll be posting the project description for Project 2 later this week. As a result, we'll keep this week light, with around 25 pages. I know many of you are eager to jump right in to the next project. Please remember to use OPINION and WARNING tags whenever applicable. As well, feel free to post your own insights, and review questions or exercises beginning Wednesday or Thursday. Outline of the Reading - Chapter 15
  1. Sharing Data Among Objects of the Same Type: Static Member Data
  2. Using Static Member Functions
  3. Pointers to Functions
  4. Pointers to Member Functions

Good Luck!

[Edited by - jwalsh on May 30, 2007 1:08:02 PM]
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Advertisement
I've used member functions for a while but I didn't truly have an understanding of how they worked until I read this article Member Function Pointers and the Fastest Delegates. I'd recommend this as additional reading after you've read the chapter and completed the excersizes.

-= Dave
FWIW, I've never actually used raw member function pointers -- I always just wrap them up into a functor.
Raw member function pointers can be a bear. They have an odd syntax, and are wholly incompatible with regular function pointers or even member pointers from a different class.

Still, I've found uses for them before.
Greetings All!

It's once again QUIZ TIME!!! That's right, listed below are a set of quiz questions to help you test your knowledge and understanding of the material contained in chapters 15.

In addition to the questions and exercises below, make sure that as you're reading the book you enter the examples into your compiler, build the program, and run the executable. I know this is a time consuming process, but the repeat use of keywords, syntax, and semantics will help ingrain the information into your long-term memory. My advice is to create a simple "driver" project with a function main. As you read, enter the examples into function main, test it, and then erase it for use again in the next example.

PLEASE DO NOT POST THE ANSWERS TO THESE QUESTIONS OR EXERCISES. If you are unable to answer these questions, please ask for assistance, but DO NOT POST THE ANSWERS. Any question which is not marked with [Extra Credit] can be answered by reading your textbook. Questions which are marked [Extra Credit] either have been answered in the thread previously, or can be answered by doing a bit of research.

I will create an answer thread for these questions immediately, so that people will have a chance to get the answers more quickly.

Chapter 15 Quiz

1. What are static member variables?
2. When you declare a static member variable within a class, is storage space set aside for the variable within the object?
3. Do static member functions exist within the scope of the object? Do you need an object to call a static member function?
4. Can you put non-static member variables in a static member function? Why or why not?
5. What is a function pointer?
6. What does the standard prototype look like for a function pointer?
7. Are the parentheses around the function name required for a function pointer? Why or why not?
8. What’s the syntax for declaring a pointer to a member function?
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Chapter 15:
1. What are static member variables?
A) Static member variables are variables that are shared amoung all instances of a class. They are a compromise between global date, which is available to all parts of your program, and member data, which is usually availableonly to each object.

2. When you declare a static member variable within a class, is storage space set aside for the variable within the object?
A) No, because the static member doesn't belong to an object, it belongs to the class and it's shared among all instances of that particular class.

3. Do static member functions exist within the scope of the object? Do you need an object to call a static member function?
A) You do not need an object of a class to call a static member function since they do not exist in an object but rather in the scope of the class.

4. Can you put non-static member variables in a static member function? Why or why not?
A) No, you can't, the reason for this is that static member functions don't have a this pointer and member data variables are accessed by the this pointer.

5. What is a function pointer?
A) It is a constant pointer to the function.

6. What does the standard prototype look like for a function pointer?
A) return value (*pFunc) (parameter(s));

7. Are the parentheses around the function name required for a function pointer? Why or why not?
A) Yes, the reason is that the precedence around the parameter(s) is higher then the indirection operator (*), without the parentheses.

8. What’s the syntax for declaring a pointer to a member function?
A) return value (class name::*pFunc)(parameter(s));
look like good/correct answers to me
Really this chapter sounded very familiar, I think I've gone over some of this (or most of this) in previous programming experiences.
--Dbproguy - My Blog - Tips, Opinions and Reviews about C++, Video Games, and Life
Thanks for showing me the syntax for declaring a pointer to a member function, to be honest i thought you could not have a pointer to a member function I'am so glad you can man, i love pointers to functions they are like the best part of the c/c++ language in my opinion and i'am glad they did not drop their use out of object oriented programming. but i got a question, does the pointer have to be within the class? or can it be outside and can you declare a typedef with that pointer and used it as a custom type? or does the function have to be static in order to do this? in that case why not just call the function without the pointer?

also if you can use them outside, does the compiler call the constructor when you assign one to a member function because the member function might assign a value to one of the member varibles and if they are not declared wouldn't that cause an error?

This topic is closed to new replies.

Advertisement