| | |
Pointer to Member Function
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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:
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
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
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
"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
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.
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.
C++ Syntax (Toggle Plain Text)
const 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
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
const char Account::*getData() const{ return data; }
I'm not sure whether I can as it is for an assignment, but here it is anyways:
C++ Syntax (Toggle Plain Text)
/* *********************************************** * Creating class prototypes, and declaring * * functions. * *********************************************** */ /* *********************************************** * Class Declaration and Definition * *********************************************** */ class Account{ //Declaring class member variable char customer[251]; //251 including null byte char accountNumber[16]; //16 including null byte int balance; /* *********************************************** * Function Declaration and Definition * *********************************************** */ public: Account(); //Default Constructor Account(char c[], char num[], int b); //Constructor with 3 arguments void init(char c[], char num[], int b); int changeCustomerInfo(char c[]); void changeAccountNumber(char num[]); void changeBalance(int b); void getLastName(char st[]); void getFirstName(char st[]); void getCity(char st[]); void getPhoneNumber(char st[]); const char* getCustomer() const; const char* getAccountNumber() const; int getBalance() const; };
C++ Syntax (Toggle Plain Text)
//declaring char* getCustomer() const char Account::*getCustomer() const{ return customer; } //declaring char* getAccountNumber() const char Account::*getAccountNumber() const{ return accountNumber; } //declaring int getBalance() const int Account::getBalance() const{ return balance; }
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
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
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]
[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
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
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.
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.
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.
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
c++ Syntax (Toggle Plain Text)
// Account.h #ifndef ACCOUNT_H #define ACCOUNT_H /* *********************************************** * Creating class prototypes, and declaring * * functions. * *********************************************** */ /* *********************************************** * Class Declaration and Definition * *********************************************** */ class Account{ //Declaring class member variable char customer[251]; //251 including null byte char accountNumber[16]; //16 including null byte int balance; void _init(); /* *********************************************** * Function Declaration and Definition * *********************************************** */ public: Account(); //Default Constructor Account(char c[], char num[], int b); //Constructor with 3 arguments ~Account(); void init(char c[], char num[], int b); int changeCustomerInfo(char c[]); void changeAccountNumber(char num[]); void changeBalance(int b); void getLastName(char st[]); void getFirstName(char st[]); void getCity(char st[]); void getPhoneNumber(char st[]); const char* getCustomer() const; // returns const char*, no arguments const char* getAccountNumber() const; // returns const char*, no arguments const char* (Account::*cus_ptmf)() const; // ptr to Account member function that returns const char* and accepts 0 args const char* (Account::*acc_ptmf)() const; // ptr to Account member function that returns const char* and accepts 0 args int getBalance() const; }; #include "Account.cpp" #endif
c++ Syntax (Toggle Plain Text)
// Account.cpp #ifdef ACCOUNT_H #include <iostream> using std::cout; using std::cin; using std::endl; Account::Account(){ _init(); } Account::~Account(){} void Account::_init(){ cus_ptmf = &Account::getCustomer; acc_ptmf = &Account::getAccountNumber; } //declaring char* getCustomer() const char* Account::getCustomer() const{ cout << "getCustomer() method called O_O!" << endl; return customer; } //declaring char* getAccountNumber() const char* Account::getAccountNumber() const{ cout << "getAccountNumber() method called $_$!" << endl; return accountNumber; } //declaring int getBalance() const int Account::getBalance() const{ return balance; } #endif
c++ Syntax (Toggle Plain Text)
// driverprogram.cpp #include <iostream> #include "Account.h" using std::cin; using std::cout; using std::endl; int main(){ Account ac; (ac.*(ac.cus_ptmf))(); // dereferencing the pointers and giving them zero arguments (ac.*(ac.acc_ptmf))(); // note the ptmf have to be qualified with an existing account object! cin.ignore(); cin.get(); return 0; }
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
•
•
Join Date: Oct 2008
Posts: 40
Reputation:
Solved Threads: 6
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.
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.
![]() |
Similar Threads
- C function needs to call a member function (C++)
- Deleting this pointer (C++)
- struct and member function pointer (C)
- How to pass a function pointer as an argument? (C++)
- Passing a Function, function pointer (C++)
- Abstract Class member function problem (C++)
- pointer/reference ? issues ... need help with simple code (C++)
Other Threads in the C++ Forum
- Previous Thread: Prime Number
- Next Thread: my program-calculator
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






