Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > c++ function pointer

Reply
Thread Tools

c++ function pointer

 
 
Marco
Guest
Posts: n/a
 
      10-28-2005
Hi,

I would like implement a class with a method Draw(),
..I would like that method Draw() change with another method when the
state of class change.
I think to use function pointer to do it, but I don't know how use this
in a class?
Can someone do a example?

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-28-2005
Marco wrote:
> I would like implement a class with a method Draw(),


class a_class {
void Draw();
};

> .I would like that method Draw() change with another method when the
> state of class change.


What does it mean "method Draw() change"? How can a method change?

> I think to use function pointer to do it, but I don't know how use this
> in a class?


I don't think there is a need of any pointer. Just implement your 'Draw'
so that it checks the "state of class" and behaves accordingly:

void a_class:raw() {
if (state_didnt_change) // whatever that means
; // do something here
else // the state has changed
; // do something different
}

> Can someone do a example?


I can do a example alright. Just give me the example and I'll do it.

V
 
Reply With Quote
 
 
 
 
n2xssvv g02gfr12930
Guest
Posts: n/a
 
      10-28-2005
Marco wrote:
> Hi,
>
> I would like implement a class with a method Draw(),
> .I would like that method Draw() change with another method when the
> state of class change.
> I think to use function pointer to do it, but I don't know how use this
> in a class?
> Can someone do a example?
>

Marco,

Why not create a base class with the Draw() method being declared as
virtual and use a pointer to the base class. The actual Draw() function
can then be defined in descendant classes, (see below).

class Shape
{
public:
virtual void Draw(void);
.
.
.
};

class Square : public Shape
{
public:
void Draw(void);
.
.
.
}

class Circle : public Shape
{
public:
void Draw(void);
.
.
.
}

As for a pointer to a member function see below.

{
void (Shape::*pFnc)(void) = &Shape:raw
Shape Sample;
(Sample.*pFn)(); // Will call Shape:raw() function
}

JFJB
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      10-28-2005
"Marco" writes:

> I would like implement a class with a method Draw(),
> .I would like that method Draw() change with another method when the
> state of class change.
> I think to use function pointer to do it, but I don't know how use this
> in a class?
> Can someone do a example?


Do you know about polymorphism in C++? Perhaps that will help you do what
you want. If you want to stick with the function pointer approach, here is
a sample of the rather nasty syntax. But note that the function pointer is
in main and not in the class. But there is a way to get access to the state
variable should you wish to. I can show you how to get it too, but first I
think you should knowingly reject the polymorphic approach.

#include <iostream>

class C
{
public:
void draw();
private:
int state; // igonred in this test program
};
//--------------
void C::draw()
{
std::cout << "draw called\n";
}
//==================
int main()
{
C object;
typedef void( C::*PMF) (); // for clarity
PMF pmf; // pointer to member function
pmf = &C::draw; // select desired member function
(object.*pmf) (); // call it

std::cin.get();
}


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-28-2005
* Marco:
>
> I would like implement a class with a method Draw(),
> .I would like that method Draw() change with another method when the
> state of class change.
> I think to use function pointer to do it, but I don't know how use this
> in a class?
> Can someone do a example?


Use a pointer to an instance of a class with a virtual member function.


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
HappyHippy
Guest
Posts: n/a
 
      10-28-2005
Marco wrote:
> Hi,
>
> I would like implement a class with a method Draw(),
> .I would like that method Draw() change with another method when the
> state of class change.
> I think to use function pointer to do it, but I don't know how use this
> in a class?
> Can someone do a example?
>


You can do something like that:

#include <iostream>


class CMYClass
{
public:
enum E_STATE
{
E_ST1,
E_ST2,
E_ST3
};
CMYClass():
m_pDrawFnc(&CMYClass::draw_S1)
{
}

void draw()
{
(this->*m_pDrawFnc)();
}

void changeState(E_STATE eState)
{
switch(eState)
{
case E_ST1:
m_pDrawFnc = &CMYClass::draw_S1;
return;

case E_ST2:
m_pDrawFnc = &CMYClass::draw_S2;
return;

case E_ST3:
m_pDrawFnc = &CMYClass::draw_S3;
return;

default:
//invalid argument!
return;
}
}

private:
void draw_S1()
{
std::cout<<"draw_S1"<<std::endl;
}
void draw_S2()
{
std::cout<<"draw_S2"<<std::endl;
}
void draw_S3()
{
std::cout<<"draw_S3"<<std::endl;
}

typedef void (CMYClass::*draw_fnc)();

E_STATE m_eState;
draw_fnc m_pDrawFnc;
};

int main()
{
CMYClass obj;
obj.draw();
obj.changeState(CMYClass::E_ST2);
obj.draw();
obj.changeState(CMYClass::E_ST3);
obj.draw();

return 0;
}
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      10-28-2005
"Marco" writes:

> I would like implement a class with a method Draw(),
> .I would like that method Draw() change with another method when the
> state of class change.
> I think to use function pointer to do it, but I don't know how use this
> in a class?
> Can someone do a example?


I think I was seduced by your talk of a function pointer in my earlier post.
As I read your post literally, and ignoring your speculation, it seems you
simply want something like this.

#include <iostream>

class C
{
public:
C() {state = 0;}
void draw();
void change_state() { state = 1;}
void draw1() {std::cout << "draw1 called\n";}
void draw2() {std::cout << "draw2 called\n";}
private:
int state;
};
//---------------------
void C::draw()
{
if (state == 0)
draw1();
else
draw2();
}
//=================
int main()
{
C c;
c.draw1();
c.change_state();
c.draw();

std::cin.get();
}


 
Reply With Quote
 
Marco
Guest
Posts: n/a
 
      10-28-2005
Thanks it's right what I need.

 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      10-28-2005
"osmium" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> C c;
> c.draw1();


Oops! Shoule be c.draw();

> c.change_state();
> c.draw();



 
Reply With Quote
 
Andrej Hristoliubov
Guest
Posts: n/a
 
      10-29-2005
?
>
> I can do a example alright. Just give me the example and I'll do it.
>
> V

Vitya is right again!!!

Do listen to Victor, I neither had seen this in production not
Victor's code. And I know Victor for quite some time now (Vitya is it
25 or 30 years in total is the duration of our acquaintance and
bestfriendship?).


hi to babulya....

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
function pointer and pointer to function prashant.khade1623@gmail.com C Programming 3 04-11-2008 07:30 AM
diffrence between "function pointer" and "pointer to a function" murgan C Programming 6 12-21-2005 06:01 AM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
function pointer and member function pointer question glen stark C++ 2 10-10-2003 01:41 PM



Advertisments