From: forums_mp on
For starters the emphasis is on 'global' variables common to muliple
translation units

Coding standard states that global variables should be defined as
static variables' within a class at public scope. One instance of
this class should exist and the recommendation is to use the singleton
design patten. IOW:

//common_data_b.h
# ifndef COMMON_DATA_B_H
# define COMMON_DATA_B_H

#include <iostream>
class common_data_b {
public :
static int const a = 5 ;
static const double pi ;
static
common_data_b& intance() {
static common_data_b cd_b;
return cd_b;
}
};
#endif

//common_data_b.cpp
const double common_data_b::pi = 3.141;

OK!! But what's the difference if I did:

//common_data_a.h
# ifndef COMMON_DATA_A_H
# define COMMON_DATA_A_H

#include <iostream>
class common_data_a {
public :
static int const a = 5;
static const double pi ;
};
///common_data_a.cpp
const double common_data_a::pi = 3.141 ;

#endif


//later foo.h
class foo {
public :
void do_work_a() { std::cout << common_data_a::pi << std::endl; }
void do_work_b() { std::cout << common_data_b::intance().pi <<
std::endl; }
};
//bar.h etc

int main()
{
foo f;
f.do_work_a() ;
f.do_work_b() ;
std::cin.get();
}

I'm not seeing the value added surrounding the singleton design
pattern. In my view the common_data_a (alternate) approach suffices.
Am I wrong?

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Mathias Gaunard on
On 13 f�v, 03:40, forums...(a)hotmail.com wrote:
> For starters the emphasis is on 'global' variables common to muliple
> translation units
>
> Coding standard states that global variables should be defined as
> static variables' within a class at public scope. One instance of
> this class should exist and the recommendation is to use the singleton
> design patten.

I don't know where you get those bad recommendations from.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Martin B. on
On 13.02.2010 04:40, forums_mp(a)hotmail.com wrote:
> For starters the emphasis is on 'global' variables common to muliple
> translation units
>
> Coding standard states that global variables should be defined as
> static variables' within a class at public scope. One instance of
> this class should exist and the recommendation is to use the singleton
> design patten. IOW:
>

You did *not* define global variables, but global constants. Depending
on the type, constness and usage of the global objects, different
approaches might make sense.

> (....)
> static int const a = 5 ;
> static const double pi ;
> static
> common_data_b& intance() {
> static common_data_b cd_b;
> return cd_b;
> }
>(....)
> OK!! But what's the difference if I did:
> (....)
> class common_data_a {
> public :
> static int const a = 5;
> static const double pi ;
> };
>(....)
> I'm not seeing the value added surrounding the singleton design
> pattern. In my view the common_data_a (alternate) approach suffices.
> Am I wrong?
>

In you case I would do:
namespace my_consts {
static int const a = 5;
static double const pi = 3.141;
}

The singleton approach makes sense when your global object (be it a
const or a variable) should only be constructed if used:

class MyConsts {
const LargeInteger a;
const RealNumber pi;
MyConsts()
: a("5000100200300400500600700800900")
, pi("3.14159265358979323846264338327950288")
{ }
public:
MyConsts const& instance() {
// Threading issues aside, this will only call the ctor of MyConsts
// the first time instance() is called:
static MyConsts obj;
return obj;
}
};

br,
Martin

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Paul Bibbings on
forums_mp(a)hotmail.com writes:

> For starters the emphasis is on 'global' variables common to muliple
> translation units
>
> Coding standard states that global variables should be defined as
> static variables' within a class at public scope. One instance of
> this class should exist and the recommendation is to use the singleton
> design patten. IOW:
>
> //common_data_b.h
> # ifndef COMMON_DATA_B_H
> # define COMMON_DATA_B_H
>
> #include <iostream>
> class common_data_b {
> public :
> static int const a = 5 ;
> static const double pi ;
> static
> common_data_b& intance() {
> static common_data_b cd_b;
> return cd_b;
> }
> };
> #endif

Having not come across this `idiom' before, I have to say that at the
outset I'm a little unsure about it too. I'm trying to figure (albeit
with a very tired head on) what the advantage of the singleton pattern
gives here, in returning a reference to a single static instance of an
object whose only purpose is to expose two public static data members,
access to which do not require instantiation of common_data_b at all.
Or am I just /too/ tired today?

Regards

Paul Bibbings

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: forums_mp on
On Feb 13, 6:24 pm, "Martin B." <0xCDCDC...(a)gmx.at> wrote:

>
> You did *not* define global variables, but global constants.
OK! I'll buy that.


> In you case I would do:
> namespace my_consts {
> static int const a = 5;
> static double const pi = 3.141;
>
> }

True but of course - if memory serves - this violates the ODR rule.
Every translation unit that include either (common_data_a.h or
common_data_b.h) file will get a/their own copy of 'a' and 'PI'

>
> The singleton approach makes sense when your global object (be it a
> const or a variable) should only be constructed if used:
>
> class MyConsts {
> const LargeInteger a;
> const RealNumber pi;
> MyConsts()
> : a("5000100200300400500600700800900")
> , pi("3.14159265358979323846264338327950288")
> { }
> public:
> MyConsts const& instance() {
> // Threading issues aside, this will only call the ctor of MyConsts
> // the first time instance() is called:
> static MyConsts obj;
> return obj;
> }
>
> };
Agreed!


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]