i m not able to do this question...

a) Create a class[class CStest] of which only a single object can be made. In the class make three private member functions with same signature(function names - fn1,fn2,fn3). The three functions when called set the value of the class member variable [ int x] accordingly( eg: fn1 sets x=1; fn2 sets x=2; fn3 sets x=3). Create a method that returns the function pointer of one of the three functions (fn1,fn2,fn3) depending on a parameter passed to it (1 for fn1, 2 for fn2, 3 for fn3). Make a public member function println to display the value of member variable x.

b) Create a program to use above class. Create an object of class [CStest] and then get the pointer of function fn3. invoke function fn3 from the obtained function pointer. invoke println method of CStest class from the object. End the program releasing any allocated resources.
Those are instructions. Why can't you follow them?
specifically i m not able to do this part
Create a method that returns the function pointer of one of the three functions (fn1,fn2,fn3) depending on a parameter passed to it (1 for fn1, 2 for fn2, 3 for fn3)
If you write some code, we can help you improve/fix it. But we work with nothing.
problem is tht m not able to do anything abt this i can make the class with given details but after that i don't know what to do.
i don't ven know how to return a function pointer and how do i use it.
Did you consider a google search for what a function pointer is?
yes i googled abt function pointer visited many sites,
bt still not getting wht to do....
Create a class[class CStest] of which only a single object can be made.
You need to check out Singleton, that's probably what you're expected to use.

In the class make three private member functions with same signature(function names - fn1,fn2,fn3).
That sounds easy enough.

The three functions when called set the value of the class member variable [ int x] accordingly( eg: fn1 sets x=1; fn2 sets x=2; fn3 sets x=3).
That's easy enough too.

Create a method that returns the function pointer of one of the three functions (fn1,fn2,fn3) depending on a parameter passed to it (1 for fn1, 2 for fn2, 3 for fn3).
Take a look at this: http://www.parashift.com/c++-faq/pointers-to-members.html

Make a public member function println to display the value of member variable x.
This uses the pointer above.

Try it and let us know if you're still stuck.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
typedef void (*fun_ptr)();

fun_ptr(*)() method(int a)
{
      switch (a)
      {
            case 1:
                  return fn1;
                  break;
            case 2:
                  return fn2;
                  break;
            case 3:
                  return fn3;
                  break;
            }
      }
That doesn't demonstrate a pointer to a member function.
Topic archived. No new replies allowed.