473,466 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Some questions about macro and function design

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
3 1662
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Vibhajha | last post by:
Hi friends, My sister is in great problem , she has this exam of C++ and she is stuck up with some questions, if friends like this group provides some help to her, she will be very grateful....
5
by: MLH | last post by:
I have problems getting the CTRL-d, CTRL-w and other 'hotkey combo' assignments to work in Access 97. I just imported the autoexec macro directly from Access 2.0, so its no surprise. Some are...
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
9
by: Dixie | last post by:
How can I open a macro so it can be edited in code from a button click? Assuming I can, will this be able to be done in an mde application. dixie
3
by: zhangyue.zl | last post by:
Before I thought C is simple and convient , but now I dont think so.There is really some ugly thing in C.Today I see some macro declaration like this: void va_end (va_list); /* Defined in...
4
by: Steve | last post by:
I have read a couple articles online, read my Jesse Liberty book but I am still confused as to just what the best practices are for using exceptions. I keep changing how I'm working with them and...
12
by: Laurent Deniau | last post by:
I was playing a bit with the preprocessor of gcc (4.1.1). The following macros expand to: #define A(...) __VA_ARGS__ #define B(x,...) __VA_ARGS__ A() -nothing, *no warning* A(x) -x ...
18
by: Bob Nelson | last post by:
Was the ``L_tmpnam'' macro given that mixed case name by a deliberate design decision? Is it the only macro expanding to an integral constant expression that has lowercase characters in the...
16
by: mdh | last post by:
I have asked a few questions about Macros...and think what I have been missing ...and which all who have replied clearly understand...is that this is a Pre-processor action. Hopefully the above is...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.