Some questions about macro and function design

Discussion in 'C++' started by mimi, Mar 7, 2007.

  1. mimi

    mimi Guest

    1.Instead of using macro, I would like to use static const variables
    in such situations.

    class Foo {
    public:
    static const int SOMEVALUEWITHFOO = 1;
    }

    int main()
    {
    Foo test;
    return 0;
    }

    My first question is: Is using static const variables instead of macro
    appreciated?

    2. How to define a global variable which is initialized by the
    parameter of main, and gurantee its constness by technology?
    Take the program in question 1 for example.If I want SOMEVALUEWITHFOO
    to be initialized by the parameters of main, and its value should
    never be changed, and every object of Foo has the same value of
    SOMEVALUEWITHFOO, how should I design the program?

    Thanks for any advice.
     
    mimi, Mar 7, 2007
    #1
    1. Advertising

  2. On 7 Mar, 08:59, "mimi" <> wrote:
    > 1.Instead of using macro, I would like to use static const variables
    > in such situations.
    >
    > class Foo {
    > public:
    > static const int SOMEVALUEWITHFOO = 1;
    >
    > }
    >
    > int main()
    > {
    > Foo test;
    > return 0;
    >
    > }
    >
    > My first question is: Is using static const variables instead of macro
    > appreciated?


    Yes, very much.

    > 2. How to define a global variable which is initialized by the
    > parameter of main, and gurantee its constness by technology?
    > Take the program in question 1 for example.If I want SOMEVALUEWITHFOO
    > to be initialized by the parameters of main, and its value should
    > never be changed, and every object of Foo has the same value of
    > SOMEVALUEWITHFOO, how should I design the program?


    Don't know if this is possible, if all classes should share the value
    it has to be a static member, if it has to be unchangeable it has to
    be const. Since it's static it's initialized before main is run and
    since it's const it can't be changed after.

    It might be possible to make something like a singleton, where you
    have a static non-const variable and only use a method to access it,
    but I don't know.

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Mar 7, 2007
    #2
    1. Advertising

  3. Erik Wikström wrote:
    > On 7 Mar, 08:59, "mimi" <> wrote:
    > > 1.Instead of using macro, I would like to use static const variables
    > > in such situations.
    > >
    > > class Foo {
    > > public:
    > > static const int SOMEVALUEWITHFOO = 1;
    > >
    > > }
    > >
    > > int main()
    > > {
    > > Foo test;
    > > return 0;
    > >
    > > }
    > >
    > > My first question is: Is using static const variables instead of macro
    > > appreciated?

    >
    > Yes, very much.
    >
    > > 2. How to define a global variable which is initialized by the
    > > parameter of main, and gurantee its constness by technology?
    > > Take the program in question 1 for example.If I want SOMEVALUEWITHFOO
    > > to be initialized by the parameters of main, and its value should
    > > never be changed, and every object of Foo has the same value of
    > > SOMEVALUEWITHFOO, how should I design the program?

    >
    > Don't know if this is possible, if all classes should share the value
    > it has to be a static member, if it has to be unchangeable it has to
    > be const. Since it's static it's initialized before main is run and
    > since it's const it can't be changed after.
    >
    > It might be possible to make something like a singleton, where you
    > have a static non-const variable and only use a method to access it,
    > but I don't know.


    like this?

    #include <stdexcept>
    #include <iostream>


    class Constant
    {
    public:
    static int value() {return value_;}

    private:
    Constant() {}
    ~Constant() {}
    static void setValue(int n) {value_ = n;}

    static int value_;

    friend class Setter;
    };

    class Setter
    {
    public:
    static Setter* instance()
    {
    if (!instance_)
    {
    instance_ = new Setter();
    return instance_;
    }
    std::cerr << "re-instantiating Setter" << std::endl;
    throw std::logic_error("re-instantiating Setter");
    return 0;
    }

    static void set (int n) {Constant::setValue(n);}

    private:


    static Setter* instance_;
    };

    Setter* Setter::instance_ = 0;
    int Constant::value_ = 0;

    void good()
    {
    std::cout << Constant::value() << std::endl;
    }

    void bad()
    {
    Setter::instance()->set(2);
    std::cout << Constant::value() << std::endl;
    }

    int main()
    {
    Setter::instance()->set(1);
    good();
    bad();
    return 0;
    }


    --
    Nick Keighley
     
    Nick Keighley, Mar 7, 2007
    #3
  4. Nick Keighley wrote:
    > Erik Wikström wrote:
    > > On 7 Mar, 08:59, "mimi" <> wrote:


    <snip>

    > > > 2. How to define a global variable which is initialized by the
    > > > parameter of main, and gurantee its constness by technology?
    > > > Take the program in question 1 for example.If I want SOMEVALUEWITHFOO
    > > > to be initialized by the parameters of main, and its value should
    > > > never be changed, and every object of Foo has the same value of
    > > > SOMEVALUEWITHFOO, how should I design the program?

    > >
    > > Don't know if this is possible, if all classes should share the value
    > > it has to be a static member, if it has to be unchangeable it has to
    > > be const. Since it's static it's initialized before main is run and
    > > since it's const it can't be changed after.
    > >
    > > It might be possible to make something like a singleton, where you
    > > have a static non-const variable and only use a method to access it,
    > > but I don't know.

    >
    > like this?
    >
    > #include <stdexcept>
    > #include <iostream>
    >
    >
    > class Constant
    > {
    > public:
    > static int value() {return value_;}
    >
    > private:
    > Constant() {}
    > ~Constant() {}
    > static void setValue(int n) {value_ = n;}
    >
    > static int value_;
    >
    > friend class Setter;
    > };
    >
    > class Setter
    > {
    > public:
    > static Setter* instance()
    > {
    > if (!instance_)
    > {
    > instance_ = new Setter();
    > return instance_;
    > }
    > std::cerr << "re-instantiating Setter" << std::endl;
    > throw std::logic_error("re-instantiating Setter");
    > return 0;
    > }
    >
    > static void set (int n) {Constant::setValue(n);}


    nooo!

    Don't make Setter::set() static



    > private:
    >
    >
    > static Setter* instance_;
    > };
    >
    > Setter* Setter::instance_ = 0;
    > int Constant::value_ = 0;
    >
    > void good()
    > {
    > std::cout << Constant::value() << std::endl;
    > }
    >
    > void bad()
    > {
    > Setter::instance()->set(2);
    > std::cout << Constant::value() << std::endl;
    > }
    >
    > int main()
    > {
    > Setter::instance()->set(1);
    > good();
    > bad();
    > return 0;
    > }
    >
    >
    > --
    > Nick Keighley
     
    Nick Keighley, Mar 7, 2007
    #4
    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. Dead RAM
    Replies:
    20
    Views:
    1,136
    John Harrison
    Jul 14, 2004
  2. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    592
    Jack Klein
    Sep 21, 2003
  3. sounak

    to get macro name from macro value

    sounak, Nov 22, 2005, in forum: C Programming
    Replies:
    17
    Views:
    515
    Mark McIntyre
    Nov 22, 2005
  4. Patrick Kowalzick
    Replies:
    5
    Views:
    492
    Patrick Kowalzick
    Mar 14, 2006
  5. Mike Manilone

    macro inside macro

    Mike Manilone, Oct 3, 2011, in forum: C Programming
    Replies:
    8
    Views:
    476
    Mike Manilone
    Oct 6, 2011
Loading...

Share This Page