473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

c++ function pointer

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?

Oct 28 '05 #1
10 1460
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::Draw() {
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
Oct 28 '05 #2
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);
Oct 28 '05 #3
"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();
}
Oct 28 '05 #4
* 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?
Oct 28 '05 #5
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;
}
Oct 28 '05 #6
"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();
}
Oct 28 '05 #7
Thanks it's right what I need.

Oct 28 '05 #8
"osmium" <r1********@comcast.net> wrote in message
news:3s************@individual.net...
C c;
c.draw1();
Oops! Shoule be c.draw();
c.change_state();
c.draw();

Oct 28 '05 #9
?

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

Oct 29 '05 #10

On Fri, 28 Oct 2005 15:48:27 -0400
HappyHippy <ka******@mail.ru> wrote:
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;
}


I think the resolution is still not adequate.
If the collection of states you need is greater than E_STATE, what should you do?
I think using a integer variable or array is simple, every bit of the variable represents a state or simplely the value of the variable represents states

Oct 30 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
2
by: sushil | last post by:
+1 #include<stdio.h> +2 #include <stdlib.h> +3 typedef struct +4 { +5 unsigned int PID; +6 unsigned int CID; +7 } T_ID; +8 +9 typedef unsigned int (*T_HANDLER)(void); +10
27
by: Marlene Stebbins | last post by:
I am experimenting with function pointers. Unfortunately, my C book has nothing on function pointers as function parameters. I want to pass a pointer to ff() to f() with the result that f() prints...
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
54
by: John | last post by:
Is the following program print the address of the function? void hello() { printf("hello\n"); } void main() { printf("hello function=%d\n", hello); }
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
20
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to...
10
by: Richard Heathfield | last post by:
Stephen Sprunk said: <snip> Almost. A function name *is* a pointer-to-function. You can do two things with it - copy it (assign its value to an object of function pointer type, with a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.