Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-10-2003, 07:28 AM   #1 (permalink)
Kportertx
Registered User
 
Join Date: Apr 2003
Posts: 11
Kportertx is on a distinguished road
pointer to function with class?

PAcct is a pointer to a class, the class is points to can change.
in total there are 3 classes PAcct can point to, one base and 2 derived classes.

ok here is the problem I can delcare a array of pointers to functions like this
void (* Transaction[3])() ={Deposit ,Withdrawl,ShowAll}; //declaring an array of pointer to functions

but deposit withdrawl and show all are all in a class so how i need to access them is like this

void (* Transaction[3])() ={PAcct->Deposit ,PAcct->Withdrawl,PAcct->ShowAll}; //declaring an array of pointer to functions

but when I do this I get an compile error that says this

error C2440: 'initializing' : cannot convert from 'void' to 'void (__cdecl *)(void)'
Expressions of type void cannot be converted to other types

[code]C:\Documents and
Kportertx is offline   Reply With Quote
Old 04-10-2003, 07:58 AM   #2 (permalink)
saline
I am red.
 
saline's Avatar
 
Join Date: Feb 2003
Location: Cleveland, OH
Posts: 139
saline is on a distinguished road
This post seems somehow... unfinished. You started to say something more and then it cut you off for some reason. Eitherway I've got an idea, sort of.

My experience has been when it says "cannot convert from a to b" it's a casting error of some kind. You're trying to reconcile two different kinds of data (like an int and a string) and the compiler has no idea what to do.

I don't know how likely that is in this case just becuse you're accessing functions and not assigning anything.

Because of this I might be concerned with the following, when you set the pointer to point to deposit for the first part of the array it may try and change to withdrawl when you set the second part of the array. Modifying the pointer within the declaration may mess with the declaration in a bad way.

Are all of the functions you listed (deposit, withdrawl and showall) in the same class either derived or base?

Also just to clarify you're trying to make an array of pointers to different functions so you can possible do something like the following:

Code:
transaction[0](100) // deposits 100 dollars into an account.
I have to get some lunch so maybe I'll explain a little more what I'm trying to say later.

good luck
__________________
http://home.cwru.edu/~cak19

It's my homepage with odd little bits of javascript.
saline is offline   Reply With Quote
Old 04-10-2003, 08:16 AM   #3 (permalink)
Kportertx
Registered User
 
Join Date: Apr 2003
Posts: 11
Kportertx is on a distinguished road
More source

I read your reply and decided to give more source
btw what I cut off on, it was an accidental paste.

int DoIt(cBankAcct *PAcct)
{
int x=1,t;
void (* Transaction[3])() ={PAcct->Deposit ,PAcct->Withdrawl,PAcct->ShowAll}; //declaring an array of pointer to functions

while(t!=4)
{
t = TransType();
if(t==4) continue;
//(*Transaction[t-1])(); //goto function i, same as switch i

cout << "\n\nWould you like to exit Transactions? Yes(1) No(0)\n";
cin >> x;
system("CLS");
if(x==1) break; //exit loop if the user does not want to make another transaction


}
return 1;
}

//////////////////////////////////////////class dec
////////////////////The other classes are derived from this

#include<string.h>
#include<stdlib.h>
#include<iomanip.h> //#include<iostream.h>

class cBankAcct
{
protected:
char name[50],acctnum[15];
float balance;
public:
cBankAcct(char n[],char a[],float f);
virtual void Deposit();//float &);
virtual void Withdrawl();//float &);
virtual void ShowAll();
};

////////////////////////////////////////class code
////////////////////////The rest or the classes are similar

#include "BankAcct.h"

cBankAcct::cBankAcct(char n[],char a[],float b) //get bs data
{
strcpy(name,n);
strcpy(acctnum,a);
balance = b;
}


void cBankAcct:eposit()//float &d)
{
float d;

cout << "Please enter the amount of deposit.\n>> ";
cin >> d;

balance+=d;
ShowAll();
}

void cBankAcct::Withdrawl()//float &w)
{
float w;

cout << "Please enter the amount of Withdrawl.\n>> ";
cin >> w;

if(w<=balance)
balance-=w;
else
{
cout << "***Insufficient funds!!!!***" << endl; //if not enough funds tell 'em
system("pause");
}
ShowAll();
}

void cBankAcct::ShowAll()
{
system("CLS");
cout << "\t\tSavings Account Information\n";
cout << "Customer Name: " << name << "\tAccount Number: " << acctnum << endl;
cout.precision(2);
cout.setf(ios::showpoint | ios::fixed);
cout << "Current Balance = " << balance << endl;
}
Kportertx is offline   Reply With Quote
Old 04-10-2003, 10:18 AM   #4 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
you have a couple of problems here.
first, you are trying to create a function pointer to a non-static class member function. this is bad and should probably never be done. the problem is that the function you'd be calling (if you called it successfully) would have an invalid "this" pointer, which can cause unexpected behavior.
compounding that, you are trying to link to a virtual function. since virtual functions are dynamically linked (the function address is looked up in the virtual function table on every call), you cannot directly use the address of this function.

if you want to do this the way you are currently doing it, i would implement functions like these (as static functions):

Code:
static cBankAcct::doDeposit(cBankAcct *PAcct)
{
  PAcct->Deposit();
}
then you can do:

Code:
void (* Transaction[3])(cBankAcct *) ={cBankAcct::doDeposit, cBankAcct::doWithdrawl, cBankAcct::doShowAll};
joe_bruin is offline   Reply With Quote
Old 04-10-2003, 11:06 AM   #5 (permalink)
Unicorn
Registered User
 
Unicorn's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 11
Unicorn is on a distinguished road
I hope I understand the problem correctly.

The anatomy of a pointer to a (virtual) member function in Base looks like this:
Code:
return_type (Base::*)(param_type);
If you want to store a pointer to a member function, and call it later on, you must invoke it on an object:
Code:
class Base
{
public:
    virtual void Foo () { cout << "Foo in Base" << endl; }
};

class Derived : public Base
{
public:
    virtual void Foo () { cout << "Foo in Derived" << endl; }
};

int main ()
{
    typedef void(Base::*FunctionPtr)();

    FunctionPtr foo_function = &Base::Foo;

    Base Obj1;
    Derived Obj2;

    (Obj1.*(foo_function))();
    (Obj2.*(foo_function))();

    return 0;
}
Unicorn is offline   Reply With Quote
Old 04-11-2003, 05:12 AM   #6 (permalink)
Kportertx
Registered User
 
Join Date: Apr 2003
Posts: 11
Kportertx is on a distinguished road
Talking Thanks

Well after that last reply it didnt take me very long to get it working. Thanks alot.
Kportertx is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
operate overloading member function in C# sureshkumar_kc MS Technologies ( ASP, VB, C#, .NET ) 2 10-15-2004 02:36 AM
to put data methods inside class or not? sde Java 2 05-25-2004 04:09 PM
trouble calling auxiliary function in a class brtklt Standard C, C++ 0 07-18-2002 05:22 PM


All times are GMT -8. The time now is 04:25 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting