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;
}

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.

const char Account::*getData() const{
	return data;
}

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.

const char Account::*getData() const{
	return data;
}

Post all of your code please so we can investigate the issue @_@

I'm not sure whether I can as it is for an assignment, but here it is anyways:

/*
***********************************************
*	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; 
};
//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;
}

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.

//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]

Edited 3 Years Ago by happygeek: fixed formatting

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.

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.

// 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
// 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
// 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

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.

This article has been dead for over six months. Start a new discussion instead.