[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

How to put const into function pointer?

Immortal Nephi

9/9/2008 9:25:00 PM

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
6 Answers

Victor Bazarov

9/9/2008 10:10:00 PM

0

Immortal_Nephi@hotmail.com wrote:
> 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;
> }
>
> Nephi

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

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Immortal Nephi

9/9/2008 11:44:00 PM

0

On Sep 9, 5:09 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> Immortal_Ne...@hotmail.com wrote:
> >    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;
> > }
>
> > 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.

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

contactmayankjain@gmail.com

9/10/2008 1:18:00 AM

0

On Sep 10, 4:44 am, Immortal_Ne...@hotmail.com wrote:
> On Sep 9, 5:09 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
>
>
>
> > Immortal_Ne...@hotmail.com wrote:
> > >    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;
> > > }
>
> > > 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.
>
> 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

James Kanze

9/10/2008 8:52:00 AM

0

On Sep 10, 12:09 am, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> Immortal_Ne...@hotmail.com wrote:
> > 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.

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

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

James Kanze

9/10/2008 8:55:00 AM

0

On Sep 10, 3:18 am, "contactmayankj...@gmail.com"
<contactmayankj...@gmail.com> wrote:

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

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.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Victor Bazarov

9/10/2008 6:03:00 PM

0

Immortal_Nephi@hotmail.com wrote:
> [..]
> Can you please say if it is ok to place "Obj obj;" inside main()
> function or global scope?

If you place it in the 'main' function, you will have to pass it as the
argument to any of the functions where it's needed. If it's global, the
functions can gain access to it directly. It's up to you to decide what
type of access you want.

> "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 not entirely sure what you're trying to explain here. You want the
object to survive between different runs of the program? Then you need
to read about "persistence". Essentially you want to store the object
somewhere (like a file on disk) and read it from that file when you
start again. Once more, I am not sure if that's what you're asking about.

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

It's OK to place 'void', it's just a bad habit. It's a question of
style, nothing more. There is a good book on style, I believe Herb
Sutter co-authored it with somebody. It's titled "Exceptional C++
Style" or something like that. Look it up. Although I am not sure that
'void' between parentheses is mentioned in that book. :-)

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask