How to put const into function pointer?

I

Immortal_Nephi

This Run() function is function pointer. It contains three
functions. How can you put const in Run() function? It should guard
against modifying Run()'s function pointer array.
Do you know what do const before void mean? For example -- const
void foo() {}.

class Obj
{
public:
Obj();
~Obj();

static void (*Run[3])(void);

private:
static void F1(void);
static void F2(void);
static void F3(void);

static unsigned int A;
static unsigned int B;
static unsigned int C;
};

unsigned int Obj::A = 0;
unsigned int Obj::B = 0;
unsigned int Obj::C = 0;

Obj::Obj() {}

Obj::~Obj() {}

void (*Obj::Run[3])(void) =
{
Obj::F1,
Obj::F2,
Obj::F3
};

void Obj::F1(void) { A += 1; }
void Obj::F2(void) { B += 1; }
void Obj::F3(void) { C += 1; }

int main()
{
Obj obj;

obj.Run[0]();
obj.Run[1]();
obj.Run[2]();

return 0;
}

Nephi
 
I

Immortal_Nephi

   This Run() function is function pointer.  It contains three
functions.  How can you put const in Run() function?  It should guard
against modifying Run()'s function pointer array.

So, you need the elements of the array to be const, right?  You should
tell your compiler so.
   Do you know what do const before void mean?  For example -- const
void foo() {}.

It doesn't mean much, I can tell you that.


class Obj
{
public:
   Obj();
   ~Obj();
   static void (*Run[3])(void);

     static void (* const Run[3])();

Please don't use "void" between parentheses.  While it's allowed for C
compatibility reasons, it's a bad habit, IMNSHO.  If you mean to have
nothing there, don't put anything.  An empty argument list should be
just that, empty.

And, no, you aren't supposed to write "(nothing)" or "(empty)"... ;-)






private:
   static void F1(void);
   static void F2(void);
   static void F3(void);
   static unsigned int A;
   static unsigned int B;
   static unsigned int C;
};
unsigned int Obj::A = 0;
unsigned int Obj::B = 0;
unsigned int Obj::C = 0;
Obj::Obj() {}
Obj::~Obj() {}
void (*Obj::Run[3])(void) =

void (* const Obj::Run[3])() =




{
   Obj::F1,
   Obj::F2,
   Obj::F3
};
void Obj::F1(void) { A += 1; }
void Obj::F2(void) { B += 1; }
void Obj::F3(void) { C += 1; }
int main()
{
   Obj obj;
   obj.Run[0]();
   obj.Run[1]();
   obj.Run[2]();
   return 0;
}

V

I didn't test your code, but that should be it.

Hi, V...I did try, but C++ Compiler failed to compile. I could have
overlooked it. Thanks for your tip to fix const function pointer
array. C++ Compiler did compile successfully.

Can you please say if it is ok to place "Obj obj;" inside main()
function or global scope? "Obj" is a lifetime throughout program so
all classes and functions can access "Obj" directly. It is ideal if I
want to use only one instance in this throughout program.

You can run program (such as .exe) three times. This program has
three separated memory. "Obj" has its own copy in global scope
throughout program. It would be nice to create static library or
dynamic linked library so "Obj" can be reused each throughout program.

What do you think?

I am sorry to place void between (). I am not aware of C writing. I
need to learn all C++ rules. How can I get rules?

Thanks...V

Nephi
 
C

contactmayankjain

So, you need the elements of the array to be const, right?  You should
tell your compiler so.
It doesn't mean much, I can tell you that.
class Obj
{
public:
   Obj();
   ~Obj();
   static void (*Run[3])(void);
     static void (* const Run[3])();
Please don't use "void" between parentheses.  While it's allowed for C
compatibility reasons, it's a bad habit, IMNSHO.  If you mean to have
nothing there, don't put anything.  An empty argument list should be
just that, empty.
And, no, you aren't supposed to write "(nothing)" or "(empty)"... ;-)
private:
   static void F1(void);
   static void F2(void);
   static void F3(void);
   static unsigned int A;
   static unsigned int B;
   static unsigned int C;
};
unsigned int Obj::A = 0;
unsigned int Obj::B = 0;
unsigned int Obj::C = 0;
Obj::Obj() {}
Obj::~Obj() {}
void (*Obj::Run[3])(void) =
void (* const Obj::Run[3])() =
{
   Obj::F1,
   Obj::F2,
   Obj::F3
};
void Obj::F1(void) { A += 1; }
void Obj::F2(void) { B += 1; }
void Obj::F3(void) { C += 1; }
int main()
{
   Obj obj;
   obj.Run[0]();
   obj.Run[1]();
   obj.Run[2]();
   return 0;
}
Nephi
V

I didn't test your code, but that should be it.

Hi, V...I did try, but C++ Compiler failed to compile.  I could have
overlooked it.  Thanks for your tip to fix const function pointer
array.  C++ Compiler did compile successfully.

Can you please say if it is ok to place "Obj obj;" inside main()
function or global scope?  "Obj" is a lifetime throughout program so
all classes and functions can access "Obj" directly.  It is ideal if I
want to use only one instance in this throughout program.

You can run program (such as .exe) three times.  This program has
three separated memory.  "Obj" has its own copy in global scope
throughout program.  It would be nice to create static library or
dynamic linked library so "Obj" can be reused each throughout program.

Using dynamic library is always a better choice as this saves your
memory and your library
is loaded only when it required.
 
J

James Kanze

So, you need the elements of the array to be const, right?
You should tell your compiler so.
It doesn't mean much, I can tell you that.

And it's not really placed where it should be: const normally
follows what it modifies (although as you say, the idea of a
function which returns nothing, but doesn't allow you to modify
that nothing, is pretty useless).
class Obj
{
public:
Obj();
~Obj();
static void (*Run[3])(void);
static void (* const Run[3])();

You should have mentionned the basic principle here: const
follows what it to be constant. In this case, the pointers are
to be constant, so the const is inserted after the *. (The case
of arrays is a bit special, since you can't have a constant
array. The syntax doesn't allow it directly, and even with
typedef, at least in C, the rule is that if the const is applied
to the array, it is not the array that is const, but each of the
elements. But I don't think that this makes any difference in
practice.)
 
J

James Kanze

Using dynamic library is always a better choice as this saves
your memory and your library is loaded only when it required.

Nonsense. In this case, whether he uses a static library or a
dynamically linked object doesn't change anything. Generally,
however, dynamical linking increases the memory footprint of the
executable, precisely because (despite the name under Windows),
you're not dynamically linking a library, but an object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top