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. 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
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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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.
|
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...
|
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
|
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?
| |
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...
|
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*
|
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?
|
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>
|
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...
|
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...
| |
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |