Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

c pointer to class.function

Status
Not open for further replies.

princeshri

IS-IT--Management
Jun 22, 2001
48
GB
Hi,

if a class was declared as

Code:
class1 class;

how can i get a c function pointer to a public function within variable class such as

Code:
class.function();

without using a c stub like,

Code:
void classfunction(void)
{
    class.function;
}

any pointers appreciated ;-) .


Shri
 
Hello,

Example:
File: add.cpp
---------- cut here
#include <iostream.h>

class c {
private:
int a,b;
public:
void set_a(int s1)
{
a=s1;
}
void set_b(int s1)
{
b=s1;
}
int sum(void)
{
return (a+b);
}
};

void main(void)
{
class c x;
x.set_a(1);
x.set_b(2);
cout << &quot;The sum of 1+2 = &quot; << x.sum();
}
---------- cut here
Wouter Dijkslag

 
alas, my question is more complicated than that.

i've got a c dll that expects a c function pointer like

Code:
 void (*funcpointer)(void)

so, from your example, i need a function pointer to x.sum().

please also note that the pointer has to reference x.sum() and not c.sum()

 
Hi,

I am sorry, I never did this before, in C there exist no classes, they are a C++ feauture, so you are mixing languages and I have no idea how to add a language to another one, sorry :-/

Greetings, Wouter Dijkslag

 
#include<iostream.h>
class ttttt
{
public:
void x(){cout<<&quot;xxx&quot;<<endl;}
};
int main()
{
ttttt c;
void (ttttt::*y)();
(c.*y)();
return 0;
}
John Fill
1c.bmp


ivfmd@mail.md
 
I am a bit confused by the question. I think you want a method pointer... here is a snip of code that i keep to remind me of how to do this. The notation is a bit weird looking but here it is:


I hope this helps a bit

Matt

/****************************************************/

#include <iostream.h>

class c1{
public:
c1::c1(){}
void sample1();
void sample2();
void sample3();
void sample4();
void sample5();
void sample6();

};

void c1::sample1(){cout<<&quot;sample1\n&quot;;}
void c1::sample2(){cout<<&quot;sample2\n&quot;;}
void c1::sample3(){cout<<&quot;sample3\n&quot;;}
void c1::sample4(){cout<<&quot;sample4\n&quot;;}
void c1::sample5(){cout<<&quot;sample5\n&quot;;}
void c1::sample6(){cout<<&quot;sample6\n&quot;;}

class c2{
public:
c2::c2( void(c1::*ptr)() ):method_ptr(ptr){};
void setmethod(void(c1::*ptr)());
void fire();
private:

void (c1::*method_ptr)();
};


void c2::fire(){c1 temp;(temp.*method_ptr)();}
void c2::setmethod(void(c1::*ptr)()){method_ptr=ptr;}


int main()
{
c2 mc2(&c1::sample1);

mc2.fire();
mc2.setmethod(&c1::sample2);
mc2.fire();
mc2.setmethod(&c1::sample3);
mc2.fire();
mc2.setmethod(&c1::sample4);
mc2.fire();
mc2.setmethod(&c1::sample5);
mc2.fire();
mc2.setmethod(&c1::sample6);
mc2.fire();

return 0;
}





 
Zyrenthian,

thanx for your comment. I do however, need the c1 object in c2 in your example to be static and dont need more than one copy of c2.

Therefore, the best way forward for me is probably to define a static c1 in c2 and make all the methods static so that a local variable does not need to be defined each time it is to be called.

Basically, it looks like a wrapper class needs to be written.


Thanx for your help :)


Shri
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top