473,785 Members | 2,895 Online
Bytes | Software Development & Data Engineering Community
+ 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 SOMEVALUEWITHFO O = 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 SOMEVALUEWITHFO O
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
SOMEVALUEWITHFO O, how should I design the program?

Thanks for any advice.

Mar 7 '07 #1
3 1681
On 7 Mar, 08:59, "mimi" <cainiaodelixi. ..@gmail.comwro te:
1.Instead of using macro, I would like to use static const variables
in such situations.

class Foo {
public:
static const int SOMEVALUEWITHFO O = 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 SOMEVALUEWITHFO O
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
SOMEVALUEWITHFO O, 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.comwro te:
1.Instead of using macro, I would like to use static const variables
in such situations.

class Foo {
public:
static const int SOMEVALUEWITHFO O = 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 SOMEVALUEWITHFO O
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
SOMEVALUEWITHFO O, 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_erro r("re-instantiating Setter");
return 0;
}

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

private:
static Setter* instance_;
};

Setter* Setter::instanc e_ = 0;
int Constant::value _ = 0;

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

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

int main()
{
Setter::instanc e()->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.comwro te:
<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 SOMEVALUEWITHFO O
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
SOMEVALUEWITHFO O, 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_erro r("re-instantiating Setter");
return 0;
}

static void set (int n) {Constant::setV alue(n);}
nooo!

Don't make Setter::set() static
private:
static Setter* instance_;
};

Setter* Setter::instanc e_ = 0;
int Constant::value _ = 0;

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

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

int main()
{
Setter::instanc e()->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
2332
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. These are some questions:- 7. design and implement a class binsearch for a binary search tree.it includes search,remove and add options.make suitable assumption. 8.explai how pointers to functions can be declared in c++.under what conditions can...
5
1885
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 really simple, though... For instance, CTRL-w should just launch ScreenDump(). I can run the FN by calling it this way... dim y as variant y = screendump() But I cannot get the CTRL-w to launch it the way it did in Access 2.0.
193
9648
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 speak only of features in the spirit of C; something like object-orientation, though a nice feature, does not belong in C. Something like being able to #define a #define would be very handy, though, e.g: #define DECLARE_FOO(bar) #define...
9
2942
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
1986
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 gnulib */ #define va_end(AP) Even if we dont consider what va_end do,but doesnt it cause some name confliction as a result of its two appearances?
4
1855
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 it has now, after 15k lines of code resulted in a royal mess! It's my hope to ask some specific questions with scenario examples and that some of you might offer a little guidance or general suggestions. 1) string...
12
6539
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 B() -nothing, *warning ISO C99 requires rest arguments to be used*
18
1705
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 name?
16
2774
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 true. So, could someone look at this code, and help me understand why I get this error. #include <stdio.h>
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10148
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8972
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.