How to put const into function pointer?

Discussion in 'C++' started by Immortal_Nephi@hotmail.com, Sep 9, 2008.

  1. Guest

    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
     
    , Sep 9, 2008
    #1
    1. Advertising

  2. Guest

    On Sep 9, 5:09 pm, Victor Bazarov <> wrote:
    > 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
     
    , Sep 10, 2008
    #2
    1. Advertising

  3. Guest

    On Sep 10, 4:44 am, wrote:
    > On Sep 9, 5:09 pm, Victor Bazarov <> wrote:
    >
    >
    >
    > > 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
     
    , Sep 10, 2008
    #3
  4. James Kanze Guest

    On Sep 10, 12:09 am, Victor Bazarov <> wrote:
    > 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:
    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, Sep 10, 2008
    #4
  5. James Kanze Guest

    On Sep 10, 3:18 am, ""
    <> 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:
    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, Sep 10, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. coala
    Replies:
    3
    Views:
    376
    coala
    Sep 6, 2006
  2. coala
    Replies:
    1
    Views:
    594
    Victor Bazarov
    Sep 6, 2006
  3. Replies:
    11
    Views:
    1,109
  4. Javier
    Replies:
    2
    Views:
    569
    James Kanze
    Sep 4, 2007
  5. Disc Magnet
    Replies:
    1
    Views:
    631
    Ian Collins
    May 6, 2010
Loading...

Share This Page