By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Some questions about macro and function design

P: n/a
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.

Mar 7 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 7 Mar, 08:59, "mimi" <cainiaodelixi...@gmail.comwrote:
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

Mar 7 '07 #2

P: n/a

Erik Wikström wrote:
On 7 Mar, 08:59, "mimi" <cainiaodelixi...@gmail.comwrote:
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

Mar 7 '07 #3

P: n/a
Nick Keighley wrote:
Erik Wikström wrote:
On 7 Mar, 08:59, "mimi" <cainiaodelixi...@gmail.comwrote:
<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
Mar 7 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.