Pointer to Member Function

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2008
Posts: 128
Reputation: kenji is an unknown quantity at this point 
Solved Threads: 9
kenji's Avatar
kenji kenji is offline Offline
Junior Poster

Pointer to Member Function

 
0
  #1
Oct 21st, 2008
I have a question regarding pointers to member functions, this is the first time that I have come across them and I have no idea how to write the function for it.

This is the function prototype:
const char* getData() const;
I understand what it is and how to code the function I am just unclear about how to write the function definition so far all I can come up with on the internet is either too complex or doesnot answer the question.

This is what I have tried
char* Account::getData() const{
	return data;
}
And she said "Let there be light" and on the seveth day Windows booted.
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,364
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Pointer to Member Function

 
0
  #2
Oct 21st, 2008
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 128
Reputation: kenji is an unknown quantity at this point 
Solved Threads: 9
kenji's Avatar
kenji kenji is offline Offline
Junior Poster

Re: Pointer to Member Function

 
0
  #3
Oct 21st, 2008
Well I read both the links and have a better idea about them.

Iv updated my code as well but I get an error which basically says that it can't find the prototype or the variable being returned.

  1. const char Account::*getData() const{
  2. return data;
  3. }
And she said "Let there be light" and on the seveth day Windows booted.
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Pointer to Member Function

 
0
  #4
Oct 21st, 2008
Originally Posted by kenji View Post
Well I read both the links and have a better idea about them.

Iv updated my code as well but I get an error which basically says that it can't find the prototype or the variable being returned.

  1. const char Account::*getData() const{
  2. return data;
  3. }
Post all of your code please so we can investigate the issue @_@
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 128
Reputation: kenji is an unknown quantity at this point 
Solved Threads: 9
kenji's Avatar
kenji kenji is offline Offline
Junior Poster

Re: Pointer to Member Function

 
0
  #5
Oct 21st, 2008
I'm not sure whether I can as it is for an assignment, but here it is anyways:
  1. /*
  2. ***********************************************
  3. * Creating class prototypes, and declaring *
  4. * functions. *
  5. ***********************************************
  6. */
  7.  
  8. /*
  9. ***********************************************
  10. * Class Declaration and Definition *
  11. ***********************************************
  12. */
  13. class Account{
  14. //Declaring class member variable
  15. char customer[251]; //251 including null byte
  16. char accountNumber[16]; //16 including null byte
  17. int balance;
  18.  
  19. /*
  20. ***********************************************
  21. * Function Declaration and Definition *
  22. ***********************************************
  23. */
  24. public:
  25. Account(); //Default Constructor
  26. Account(char c[], char num[], int b); //Constructor with 3 arguments
  27. void init(char c[], char num[], int b);
  28. int changeCustomerInfo(char c[]);
  29. void changeAccountNumber(char num[]);
  30. void changeBalance(int b);
  31. void getLastName(char st[]);
  32. void getFirstName(char st[]);
  33. void getCity(char st[]);
  34. void getPhoneNumber(char st[]);
  35. const char* getCustomer() const;
  36. const char* getAccountNumber() const;
  37. int getBalance() const;
  38. };


  1. //declaring char* getCustomer()
  2. const char Account::*getCustomer() const{
  3. return customer;
  4. }
  5.  
  6. //declaring char* getAccountNumber()
  7. const char Account::*getAccountNumber() const{
  8. return accountNumber;
  9. }
  10.  
  11. //declaring int getBalance() const
  12. int Account::getBalance() const{
  13. return balance;
  14. }
Last edited by kenji; Oct 21st, 2008 at 9:37 pm. Reason: Code was too long, this is the part thats giving me trouble.
And she said "Let there be light" and on the seveth day Windows booted.
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 128
Reputation: kenji is an unknown quantity at this point 
Solved Threads: 9
kenji's Avatar
kenji kenji is offline Offline
Junior Poster

Re: Pointer to Member Function

 
0
  #6
Oct 21st, 2008
Hey guys me again, this is both a bump to the thread and a update to my code which looks like it works but is still giving me errors.

[code=CPLUSPLUS]
//declaring char* getCustomer()
const char (Account:getCustomer)() const{
return &Account::customer;
}
[\code]
And she said "Let there be light" and on the seveth day Windows booted.
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Pointer to Member Function

 
0
  #7
Oct 22nd, 2008
Disregard this post.
Last edited by Alex Edwards; Oct 22nd, 2008 at 12:46 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Pointer to Member Function

 
0
  #8
Oct 22nd, 2008
Whoops, my last post was pretty retarded.

I think I see the issue now.

Your header file and implementation file aren't speaking the same language.

You have to modify your header file such that your function declares a ptmf that returns a const char and accepts zero arguments.

Since it is a ptr, you shouldn't try to initialize it outside of the scope of the class, but during preinitialization before the an object of type Account is even created.
Last edited by Alex Edwards; Oct 22nd, 2008 at 12:46 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Pointer to Member Function

 
0
  #9
Oct 22nd, 2008
Since it's too late to edit, I'll post what I think is a solution to your problem though I might be a bit off.

  1.  
  2. // Account.h
  3.  
  4. #ifndef ACCOUNT_H
  5. #define ACCOUNT_H
  6. /*
  7. ***********************************************
  8. * Creating class prototypes, and declaring *
  9. * functions. *
  10. ***********************************************
  11. */
  12.  
  13. /*
  14. ***********************************************
  15. * Class Declaration and Definition *
  16. ***********************************************
  17. */
  18. class Account{
  19. //Declaring class member variable
  20. char customer[251]; //251 including null byte
  21. char accountNumber[16]; //16 including null byte
  22. int balance;
  23. void _init();
  24. /*
  25. ***********************************************
  26. * Function Declaration and Definition *
  27. ***********************************************
  28. */
  29. public:
  30. Account(); //Default Constructor
  31. Account(char c[], char num[], int b); //Constructor with 3 arguments
  32. ~Account();
  33. void init(char c[], char num[], int b);
  34. int changeCustomerInfo(char c[]);
  35. void changeAccountNumber(char num[]);
  36. void changeBalance(int b);
  37. void getLastName(char st[]);
  38. void getFirstName(char st[]);
  39. void getCity(char st[]);
  40. void getPhoneNumber(char st[]);
  41. const char* getCustomer() const; // returns const char*, no arguments
  42. const char* getAccountNumber() const; // returns const char*, no arguments
  43. const char* (Account::*cus_ptmf)() const; // ptr to Account member function that returns const char* and accepts 0 args
  44. const char* (Account::*acc_ptmf)() const; // ptr to Account member function that returns const char* and accepts 0 args
  45. int getBalance() const;
  46. };
  47.  
  48. #include "Account.cpp"
  49.  
  50. #endif

  1. // Account.cpp
  2.  
  3. #ifdef ACCOUNT_H
  4.  
  5. #include <iostream>
  6.  
  7. using std::cout;
  8. using std::cin;
  9. using std::endl;
  10.  
  11. Account::Account(){
  12. _init();
  13. }
  14.  
  15. Account::~Account(){}
  16.  
  17. void Account::_init(){
  18.  
  19. cus_ptmf = &Account::getCustomer;
  20. acc_ptmf = &Account::getAccountNumber;
  21.  
  22. }
  23.  
  24.  
  25. //declaring char* getCustomer()
  26. const char* Account::getCustomer() const{
  27. cout << "getCustomer() method called O_O!" << endl;
  28. return customer;
  29. }
  30.  
  31.  
  32.  
  33. //declaring char* getAccountNumber()
  34. const char* Account::getAccountNumber() const{
  35. cout << "getAccountNumber() method called $_$!" << endl;
  36. return accountNumber;
  37. }
  38.  
  39.  
  40. //declaring int getBalance() const
  41. int Account::getBalance() const{
  42. return balance;
  43. }
  44.  
  45. #endif

  1.  
  2. // driverprogram.cpp
  3.  
  4. #include <iostream>
  5. #include "Account.h"
  6.  
  7. using std::cin;
  8. using std::cout;
  9. using std::endl;
  10.  
  11. int main(){
  12.  
  13. Account ac;
  14.  
  15. (ac.*(ac.cus_ptmf))(); // dereferencing the pointers and giving them zero arguments
  16. (ac.*(ac.acc_ptmf))(); // note the ptmf have to be qualified with an existing account object!
  17.  
  18. cin.ignore();
  19. cin.get();
  20.  
  21. return 0;
  22. }

PTMF are messy, and it's fairly redundant to declare a PTMF of a class that accepts its own type unless you plan on making effective use of the PTMF by qualifying them with different classes of the same type.

In case the comments weren't enough, I'll explain my logic.

In the header file I added function declaration that return your global arrays, because if the ptmf is going to call a method that returns a const char* and accepts zero arguments that is from the Account class, I really have to provide a function that I can point to in order to make use of the PTMF with that particular signature.

So I declared the 2 methods and along with them the 2 PTMF's.

In the implementation file I defined only the methods, because the PTMF's are still just pointers (so although they look like methods, they're really just globally scoped members). I initialized them by defining a private _init method and doing their initialization there. The reason for this is to make initializing both easy for multiple Constructor definitions.

The Driver Program is just there to test the logic of the program. I admit I had some trouble making this example because I had to (redundantly) qualify the PTMF with an object of type Account and also be able to qualify an existing PTMF that returns a const char* and accepted zero arguments (hence the pointers themselves were used from the same object).

It's a lot of unnecessary indirection. Even if you found a good use for a PTMF, it would most likely be from a utility file or utility class that has special use of another utility class...

Hopefully this was helpful @_@

-Alex
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 40
Reputation: seanhunt is an unknown quantity at this point 
Solved Threads: 6
seanhunt seanhunt is offline Offline
Light Poster

Re: Pointer to Member Function

 
0
  #10
Oct 22nd, 2008
This is actually C, not technically C++, btw. You mention pointer to member functions but so far all I see is a standard function declaration and definiton. No function pointers...could you clarify?

Function pointers are a standard way to deal with "callbacks", that is, they hold the address of a block of executable code instead of just normal data.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC