curiosity singleton pattern?



 DEVELOP > c-Plus-Plus > curiosity singleton pattern?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Chris Forone"
Date: 01 Aug 2007 04:06:52 AM
Object: curiosity singleton pattern?
hello group,
cant understand the following:
Scene* Scene::sole(0); // in Scene.cpp
class Scene // singleton in Scene.h
{
public:
static Scene* Get()
{
//return sole ? sole : new (std::nothrow) Scene;
return sole; // for testing purposes only
}
int Print(void) // normaly only with valid objects?!
{
return reinterpret_cast<unsigned int>(sole);
// other values also possible (return 10;)
}
protected:
Scene();
~Scene();
private:
static Scene* sole;
};
int main(void)
{
std::cout << Scene::Get() << std::endl;
std::cout << Scene::Get()->Print() << std::endl;
}
returns
0
0
non static func Print is called by nullpointer?! have gcc 4.1 and linux os.
thanks for your answers
sincerely yours
chris
.

User: "Alf P. Steinbach"

Title: Re: curiosity singleton pattern? 01 Aug 2007 04:15:06 AM
* Chris Forone:

hello group,

cant understand the following:

Scene* Scene::sole(0); // in Scene.cpp

class Scene // singleton in Scene.h
{
public:
static Scene* Get()
{
//return sole ? sole : new (std::nothrow) Scene;
return sole; // for testing purposes only
}

int Print(void) // normaly only with valid objects?!
{
return reinterpret_cast<unsigned int>(sole);
// other values also possible (return 10;)
}

protected:
Scene();
~Scene();

private:
static Scene* sole;
};

int main(void)
{
std::cout << Scene::Get() << std::endl;
std::cout << Scene::Get()->Print() << std::endl;
}

returns

0
0

What else did you expect, and why?
Have you tried, like, removing the out-commenting in Scene::Get?
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.
User: "Chris Forone"

Title: Re: curiosity singleton pattern? 01 Aug 2007 04:18:36 AM
Alf P. Steinbach schrieb:

What else did you expect, and why?

some kind of runtime error... can access nonstatic func with NO VALID
object?!

Have you tried, like, removing the out-commenting in Scene::Get?

of course, normal behavior (address of valid object)
.


User: "Ian Collins"

Title: Re: curiosity singleton pattern? 01 Aug 2007 04:14:23 AM
Chris Forone wrote:

hello group,

cant understand the following:

Scene* Scene::sole(0); // in Scene.cpp

class Scene // singleton in Scene.h
{
public:
static Scene* Get()
{
//return sole ? sole : new (std::nothrow) Scene;

Why is this line commented out?

return sole; // for testing purposes only
}

int Print(void) // normaly only with valid objects?!

We don't bother with the (void) in C++, just use ().

{
return reinterpret_cast<unsigned int>(sole);
// other values also possible (return 10;)
}

protected:
Scene();
~Scene();

private:
static Scene* sole;
};

int main(void)
{
std::cout << Scene::Get() << std::endl;
std::cout << Scene::Get()->Print() << std::endl;
}

returns

0
0

non static func Print is called by nullpointer?! have gcc 4.1 and linux os.

Why not? Don't forget that the object isn't used to call the member
function, which is just a plain old C function with an extra this
parameter, but is passed to the the function as the this parameter. In
your case, this isn't used.
--
Ian Collins.
.
User: "Chris Forone"

Title: Re: curiosity singleton pattern? 01 Aug 2007 04:21:48 AM
Ian Collins schrieb:

//return sole ? sole : new (std::nothrow) Scene;


Why is this line commented out?

for testing operator new not successful


return sole; // for testing purposes only
}

int Print(void) // normaly only with valid objects?!


We don't bother with the (void) in C++, just use ().

sorry... ;-)

non static func Print is called by nullpointer?! have gcc 4.1 and linux os.

Why not? Don't forget that the object isn't used to call the member
function, which is just a plain old C function with an extra this
parameter, but is passed to the the function as the this parameter. In
your case, this isn't used.

sure?
.
User: "Ian Collins"

Title: Re: curiosity singleton pattern? 01 Aug 2007 04:32:19 AM
Chris Forone wrote:

Ian Collins schrieb:

non static func Print is called by nullpointer?! have gcc 4.1 and
linux os.

Why not? Don't forget that the object isn't used to call the member
function, which is just a plain old C function with an extra this
parameter, but is passed to the the function as the this parameter. In
your case, this isn't used.

sure?

Yes.
--
Ian Collins.
.
User: "Chris Forone"

Title: Re: curiosity singleton pattern? 01 Aug 2007 04:32:54 AM
Ian Collins schrieb:

Chris Forone wrote:

Ian Collins schrieb:


non static func Print is called by nullpointer?! have gcc 4.1 and
linux os.

Why not? Don't forget that the object isn't used to call the member
function, which is just a plain old C function with an extra this
parameter, but is passed to the the function as the this parameter. In
your case, this isn't used.

sure?


Yes.

thanks a lot
.

User: "James Kanze"

Title: Re: curiosity singleton pattern? 02 Aug 2007 02:16:37 AM
On Aug 1, 11:32 am, Ian Collins <ian-n...@hotmail.com> wrote:

Chris Forone wrote:

Ian Collins schrieb:

non static func Print is called by nullpointer?! have gcc 4.1 and
linux os.

Why not? Don't forget that the object isn't used to call the member
function, which is just a plain old C function with an extra this
parameter, but is passed to the the function as the this parameter. In
your case, this isn't used.

sure?

Yes.

And yet you're wrong. That might be the behavior of the
compiler you're currently using, but it's not guaranteed, and
it's not the behavior of every compiler.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.
User: "Ian Collins"

Title: Re: curiosity singleton pattern? 02 Aug 2007 02:36:36 AM
James Kanze wrote:

On Aug 1, 11:32 am, Ian Collins <ian-n...@hotmail.com> wrote:

Chris Forone wrote:

Ian Collins schrieb:

non static func Print is called by nullpointer?! have gcc 4.1 and
linux os.


Why not? Don't forget that the object isn't used to call the member
function, which is just a plain old C function with an extra this
parameter, but is passed to the the function as the this parameter. In
your case, this isn't used.


sure?


Yes.


And yet you're wrong. That might be the behavior of the
compiler you're currently using, but it's not guaranteed, and
it's not the behavior of every compiler.

OK, which one is different?
--
Ian Collins.
.
User: "James Kanze"

Title: Re: curiosity singleton pattern? 03 Aug 2007 02:40:53 AM
On Aug 2, 9:36 am, Ian Collins <ian-n...@hotmail.com> wrote:

James Kanze wrote:

On Aug 1, 11:32 am, Ian Collins <ian-n...@hotmail.com> wrote:

Chris Forone wrote:

Ian Collins schrieb:

non static func Print is called by nullpointer?! have gcc 4.1 and
linux os.

Why not? Don't forget that the object isn't used to call the member
function, which is just a plain old C function with an extra this
parameter, but is passed to the the function as the this parameter. =

In

your case, this isn't used.

sure?

Yes.

And yet you're wrong. That might be the behavior of the
compiler you're currently using, but it's not guaranteed, and
it's not the behavior of every compiler.

OK, which one is different?

The Green Hills compiler was, when I used it. (I'm not sure
what Green Hills is up to now.) And although I never used it,
from what I understand, the Centerline compiler used "fat"
pointers, and doubtlessly caught such errors as well. It
caught practically all pointer errors.
Way back when, the C committee made a great effort to specify
the language in such a way that all, or almost all, pointer
violations could be tested at runting, and result in run-time
errors, rather than random crashes or wrong results later. The
performance impact for this is fairly large, so it tends not be
done in commercial compilers, but given the improvements in
machine performance and the importance of avoiding such errors
in software connecting to the network, I wouldn't be surprised
if it didn't start appearing, at least as an option.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.




User: "James Kanze"

Title: Re: curiosity singleton pattern? 02 Aug 2007 02:15:11 AM
On Aug 1, 11:21 am, Chris Forone <4...@gmx.at> wrote:

Ian Collins schrieb:>> //return sole ? sole : new (std::nothrow) Sc=

ene;

Why is this line commented out?

for testing operator new not successful

return sole; // for testing purposes only
}
int Print(void) // normaly only with valid objects?!
non static func Print is called by nullpointer?! have gcc
4.1 and linux os.

Why not? Don't forget that the object isn't used to call the member
function, which is just a plain old C function with an extra this
parameter, but is passed to the the function as the this parameter. In
your case, this isn't used.

sure?

That's doubtlessly the most frequent implementation---it's
obvious, easy to implement, and on modern processors, very
efficient. It's certainly not guaranteed, however, and I've
used compilers where the your code would result in a runtime
error of some sort, and it's not too hard to imagine cases where
such code would cause other data to be overwritten.
Don't count on what an implementation "typically" does. The
next release of the compiler might do something different.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.



User: "James Kanze"

Title: Re: curiosity singleton pattern? 02 Aug 2007 02:11:28 AM
On Aug 1, 11:06 am, Chris Forone <4...@gmx.at> wrote:

cant understand the following:
Scene* Scene::sole(0); // in Scene.cpp
class Scene // singleton in Scene.h
{
public:
static Scene* Get()
{
//return sole ? sole : new (std::nothrow) Scene;
return sole; // for testing purposes only
}
int Print(void) // normaly only with valid objects?!
{
return reinterpret_cast<unsigned int>(sole);
// other values also possible (return 10;)
}
protected:
Scene();
~Scene();
private:
static Scene* sole;
};
int main(void)
{
std::cout << Scene::Get() << std::endl;
std::cout << Scene::Get()->Print() << std::endl;
}
returns
0
0
non static func Print is called by nullpointer?! have gcc 4.1
and linux os.

It's undefined behavior. Anything might happen. In practice,
on a modern general purpose machine, you will either get the
above results, or some sort of runtime error. Possibly
depending on the options you've passed to the compiler.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER