What is going on here?

J

JoeC

I have been programming for a while and I have seen this syntax before
and I copied this from a book but the book didn't explain what is going
on here.

class engine{
protected:
static engine* pengine;

Than later:

if(initialize(hin)){
if(!engine::gengine()->init(CmdShow)){return false;}

while(TRUE){


The engine object has its own kind as a pointer then later it is called
by the above command. I do believe this is OO programming but I don't
understand what is going on and what small programs I would write or
projects tha=t would help me learn this.
 
I

Ian Collins

JoeC said:
I have been programming for a while and I have seen this syntax before
and I copied this from a book but the book didn't explain what is going
on here.

class engine{
protected:
static engine* pengine;
What an unfortunate name, reads like penguin!
Than later:

if(initialize(hin)){
if(!engine::gengine()->init(CmdShow)){return false;}

This would be a syntax error, you have declared "pengine" as a pointer
and here you are attempting to invoke operation () on it.
while(TRUE){
Why mix pseudo booleans with bool?
The engine object has its own kind as a pointer then later it is called
by the above command. I do believe this is OO programming but I don't
understand what is going on and what small programs I would write or
projects tha=t would help me learn this.

What specifically don't you understand? I don't think your problem is
anything to do with OO programming, more an case of not understanding
some C++ basics. Which book are you learning from?
 
J

Jerry Coffin

[ ... ]
This would be a syntax error, you have declared "pengine" as a pointer
and here you are attempting to invoke operation () on it.

I don't see where it's used pengine at all. The only use of operator()
that I see is on whatever genengine() returns -- and he hasn't shown us
that at all.
 
I

Ian Collins

Jerry said:
[ ... ]
This would be a syntax error, you have declared "pengine" as a pointer
and here you are attempting to invoke operation () on it.

I don't see where it's used pengine at all. The only use of operator()
that I see is on whatever genengine() returns -- and he hasn't shown us
that at all.
Oops, see it was an unhelpful name!
 
J

Jerry Coffin

Jerry said:
[ ... ]
if(initialize(hin)){
if(!engine::gengine()->init(CmdShow)){return false;}
This would be a syntax error, you have declared "pengine" as a pointer
and here you are attempting to invoke operation () on it.

I don't see where it's used pengine at all. The only use of operator()
that I see is on whatever genengine() returns -- and he hasn't shown us
that at all.
Oops, see it was an unhelpful name!

Quite true -- even after he finds and fixes the bug, the code is (IMO)
ripe for some rewriting to use better names.
 
L

Laurent D.A.M. MENTEN

JoeC said:
I have been programming for a while and I have seen this syntax before
and I copied this from a book but the book didn't explain what is going
on here.

class engine{
protected:
static engine* pengine;

Than later:

if(initialize(hin)){
if(!engine::gengine()->init(CmdShow)){return false;}
while(TRUE){


The engine object has its own kind as a pointer then later it is called
by the above command. I do believe this is OO programming but I don't
understand what is going on and what small programs I would write or
projects tha=t would help me learn this.

This is a really common construct: there is a static pointer to the one
and only instance of the class that contains it, I guess there is also a
private or protected constructor so that user cannot instantiate the
class, and a static method (here gengine() is its name) that returns the
static pointer.

As being a member of engine, the static member is allowed to be
constructed but any access from ouside of the class is forbidden.. this
ensure that programmers always access the same and single instance of
engine.
 
J

JoeC

Ian said:
What an unfortunate name, reads like penguin!


This would be a syntax error, you have declared "pengine" as a pointer
and here you are attempting to invoke operation () on it.

Why mix pseudo booleans with bool?


What specifically don't you understand? I don't think your problem is
anything to do with OO programming, more an case of not understanding
some C++ basics. Which book are you learning from?
Here let me post more code. I got this from a book.
class engine{
protected:
static engine* pengine;
HINSTANCE hin;
HWND hwnd;
TCHAR szClass[32];
TCHAR sc[32];
WORD icon, iconsm;
int wth, hght;
int delay;
bool sleep;

public:
engine(HINSTANCE, LPTSTR, LPTSTR, WORD, WORD, int, int);
virtual ~engine();
bool init(int);
LRESULT event(HWND, UINT, WPARAM, LPARAM);
static engine* gengine(){return pengine;}
HINSTANCE instance(){return hin;}
HWND window(){return hwnd;}
void window(HWND h){hwnd = h;}
LPSTR title(){return sc;}
WORD gicon(){return icon;}
WORD giconsm(){return iconsm;}
int width(){return wth;}
int height(){return hght;}
int gdelay(){return delay;}
void frate(int rate){delay = 1000 /rate;}
bool gsleep(){return sleep;}
void gsleep(bool sl){sleep = sl;}
};


bool initialize(HINSTANCE hin){
e = new engine(hin, TEXT("Window!"), TEXT("Happy
Holloween."),IDI_Icon, IDI_Icons,800 ,480);
if(e == NULL){return false;}
e->frate(10);

ghin = hin;

return true;

engine *engine::pengine = NULL;

int WINAPI WinMain(HINSTANCE hin, HINSTANCE phin, PSTR CmdLine,
int CmdShow){
MSG msg;
static int Trigger = 0;
int TickCount;

if(initialize(hin)){
if(!engine::gengine()->init(CmdShow)){return false;}

while(TRUE){
 
J

JoeC

Laurent said:
This is a really common construct: there is a static pointer to the one
and only instance of the class that contains it, I guess there is also a
private or protected constructor so that user cannot instantiate the
class, and a static method (here gengine() is its name) that returns the
static pointer.

As being a member of engine, the static member is allowed to be
constructed but any access from ouside of the class is forbidden.. this
ensure that programmers always access the same and single instance of
engine.


Thanks, I am confused, what are some good references or links I can read
up on this. If this is common and I am confused then it is something I
should learn about, it might help me.
 
I

Ian Collins

JoeC said:
Here let me post more code. I got this from a book.

The best advice I can offer is to spend some time learning standard C++
and then once you are happy with the basics, move on to platform
specific stuff like windows programming.

You should also do some background reading on design patterns, for
example the singleton pattern would help you understand what might be
going on in the code you posted.
 
K

Kai-Uwe Bux

JoeC said:
Here let me post more code. I got this from a book.

I have a hard time believing that. Either you copied it wrong or you should
post the title of the book so that everybody can stay away from it.

class engine{
protected:
static engine* pengine;
HINSTANCE hin;
HWND hwnd;
TCHAR szClass[32];
TCHAR sc[32];
WORD icon, iconsm;
int wth, hght;
int delay;
bool sleep;

public:
engine(HINSTANCE, LPTSTR, LPTSTR, WORD, WORD, int, int);
virtual ~engine();
bool init(int);
LRESULT event(HWND, UINT, WPARAM, LPARAM);
static engine* gengine(){return pengine;}
HINSTANCE instance(){return hin;}
HWND window(){return hwnd;}
void window(HWND h){hwnd = h;}
LPSTR title(){return sc;}
WORD gicon(){return icon;}
WORD giconsm(){return iconsm;}
int width(){return wth;}
int height(){return hght;}
int gdelay(){return delay;}
void frate(int rate){delay = 1000 /rate;}
bool gsleep(){return sleep;}
void gsleep(bool sl){sleep = sl;}
};

That doesn't look too good. The abundance of getter and setter methods is a
sure give-away of poor design.

bool initialize(HINSTANCE hin){
e = new engine(hin, TEXT("Window!"), TEXT("Happy
Holloween."),IDI_Icon, IDI_Icons,800 ,480);
if(e == NULL){return false;}

The code above shows some serious misunderstanding about new. It assumes
that new could return 0. This is not the case. If new is unable to allocate
memory or to construct the object as required, it will throw instead of
returning at all. If you want a 0 return instead, you need to use the
nothrow version of new.

e->frate(10);

ghin = hin;

What is ghin? Looks like an undeclared identifier.
return true;

So this function news an object, sets the local pointer variable e to point
there and then returns without telling anybody about e and without deleting
e? That looks like a leak.

engine *engine::pengine = NULL;

If this really is the only place where engine::pengine is set, you have a
serious problem down the road.

int WINAPI WinMain(HINSTANCE hin, HINSTANCE phin, PSTR CmdLine,
int CmdShow){
MSG msg;
static int Trigger = 0;
int TickCount;

if(initialize(hin)){
if(!engine::gengine()->init(CmdShow)){return false;}

Here you are dereferencing engine::pengine through the call to gengine(). If
pengine still equals 0, you have undefined behavior.



At first sight, I was thinking that you may have come across the singleton
pattern. However, I cannot confirm that from the more code you posted.
Please note that the most important parts would be the places where pengine
is set, where the pointee of pengine is created, and where it is destroyed
(if at all). If you post those parts, it may become aparent what is going
on. So far, we have to assume that pengine==NULL, which seems a little
unlikely.


Best

Kai-Uwe Bux
 
J

James Kanze

[ ... ]
This would be a syntax error, you have declared "pengine" as a pointer
and here you are attempting to invoke operation () on it.
I don't see where it's used pengine at all. The only use of
operator() that I see is on whatever genengine() returns --
and he hasn't shown us that at all.

The only use of () is on engine::gengine, and on the init
later. It's just a guess, but could pengine and gengine mean
pointer to engine and get engine? He did show that pengine was
static, and the syntax above suggests that gengine is a static
function. All in all, this looks like some variant of the
singleton idiom, but with very non-standard names.
 
J

James Kanze

[...]
I have a hard time believing that. Either you copied it wrong
or you should post the title of the book so that everybody can
stay away from it.

The problems don't look like the sort that could be due to
copying errors. And I'm sure that there are some pretty bad
books out there.

[...]
The code above shows some serious misunderstanding about new.
It assumes that new could return 0. This is not the case. If
new is unable to allocate memory or to construct the object as
required, it will throw instead of returning at all.

Or maybe just age. When I learned C++, operator new did return
a null pointer when it failed. The code is obviously Windows,
and I think VC++ did continue returning a null pointer until
quite recently.

For the rest, of course, the code is very definitly Windows, and
not really C++, and it doesn't look like very good Windows
programming at that. So as you say, a book to stay away from.
 
J

JoeC

Ian said:
The best advice I can offer is to spend some time learning standard C++
and then once you are happy with the basics, move on to platform
specific stuff like windows programming.

You should also do some background reading on design patterns, for
example the singleton pattern would help you understand what might be
going on in the code you posted.

The point is I would like to lean what is going on in the code I posted.
I have been programming for a while. I am completely self taught
and I only what I know from reading in books and what answers I can get
to questions. I do try to learn more advanced programming techniques
then try to use them in my own projects. I am trying to figure out what
is going on and how this could benefit me in other projects.
 
J

JoeC

Kai-Uwe Bux said:
JoeC said:
Here let me post more code. I got this from a book.

I have a hard time believing that. Either you copied it wrong or you should
post the title of the book so that everybody can stay away from it.

class engine{
protected:
static engine* pengine;
HINSTANCE hin;
HWND hwnd;
TCHAR szClass[32];
TCHAR sc[32];
WORD icon, iconsm;
int wth, hght;
int delay;
bool sleep;

public:
engine(HINSTANCE, LPTSTR, LPTSTR, WORD, WORD, int, int);
virtual ~engine();
bool init(int);
LRESULT event(HWND, UINT, WPARAM, LPARAM);
static engine* gengine(){return pengine;}
HINSTANCE instance(){return hin;}
HWND window(){return hwnd;}
void window(HWND h){hwnd = h;}
LPSTR title(){return sc;}
WORD gicon(){return icon;}
WORD giconsm(){return iconsm;}
int width(){return wth;}
int height(){return hght;}
int gdelay(){return delay;}
void frate(int rate){delay = 1000 /rate;}
bool gsleep(){return sleep;}
void gsleep(bool sl){sleep = sl;}
};

That doesn't look too good. The abundance of getter and setter methods is a
sure give-away of poor design.

bool initialize(HINSTANCE hin){
e = new engine(hin, TEXT("Window!"), TEXT("Happy
Holloween."),IDI_Icon, IDI_Icons,800 ,480);
if(e == NULL){return false;}

The code above shows some serious misunderstanding about new. It assumes
that new could return 0. This is not the case. If new is unable to allocate
memory or to construct the object as required, it will throw instead of
returning at all. If you want a 0 return instead, you need to use the
nothrow version of new.

e->frate(10);

ghin = hin;

What is ghin? Looks like an undeclared identifier.
return true;

So this function news an object, sets the local pointer variable e to point
there and then returns without telling anybody about e and without deleting
e? That looks like a leak.

engine *engine::pengine = NULL;

If this really is the only place where engine::pengine is set, you have a
serious problem down the road.

int WINAPI WinMain(HINSTANCE hin, HINSTANCE phin, PSTR CmdLine,
int CmdShow){
MSG msg;
static int Trigger = 0;
int TickCount;

if(initialize(hin)){
if(!engine::gengine()->init(CmdShow)){return false;}

Here you are dereferencing engine::pengine through the call to gengine(). If
pengine still equals 0, you have undefined behavior.



At first sight, I was thinking that you may have come across the singleton
pattern. However, I cannot confirm that from the more code you posted.
Please note that the most important parts would be the places where pengine
is set, where the pointee of pengine is created, and where it is destroyed
(if at all). If you post those parts, it may become aparent what is going
on. So far, we have to assume that pengine==NULL, which seems a little
unlikely.


Best

Kai-Uwe Bux


This code is from Game Programming in 24 Hours by Michael Morrison. I
copied the code shortening some of the words. I did use this code and
his basic engine for a few projects. I didn't like the engine too much
because I didn't understand it well and decided not to create a windows
object for my later projects. I have limited experience in programming
and it is hard for me to tell good code from bad especially in larger
programs.

This was a construct that I have not seen in any other book so I asked
about it. I have written some fairly large programs that run well.
Because I work in isolation, I have limited influences of other
programmers.

If this is bad code thanks for the help. It complied and rand and it
allowed me to animate some graphics around the screen.
 
J

JoeC

James said:
[...]
I have a hard time believing that. Either you copied it wrong
or you should post the title of the book so that everybody can
stay away from it.

The problems don't look like the sort that could be due to
copying errors. And I'm sure that there are some pretty bad
books out there.

[...]
The code above shows some serious misunderstanding about new.
It assumes that new could return 0. This is not the case. If
new is unable to allocate memory or to construct the object as
required, it will throw instead of returning at all.

Or maybe just age. When I learned C++, operator new did return
a null pointer when it failed. The code is obviously Windows,
and I think VC++ did continue returning a null pointer until
quite recently.

For the rest, of course, the code is very definitly Windows, and
not really C++, and it doesn't look like very good Windows
programming at that. So as you say, a book to stay away from.

--
James Kanze (GABI Software) email:[email protected]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


Thanks. It is hard to find windows/C++ books let alone good ones. I
also learn from regular C++ book then just integrate that programming
with what I know about windows. My question was not about windows but
the C++ programming method used. The code may be bad but I was asking
about the specific lines and how they can help me with other projects
that is have a static pointer inside the object of itself then how it
was initialized. If it is bad code I am sorry but back to the original
question.
 
K

Kai-Uwe Bux

JoeC said:
[snip]
At first sight, I was thinking that you may have come across the
singleton pattern. However, I cannot confirm that from the more code you
posted. Please note that the most important parts would be the places
where pengine is set, where the pointee of pengine is created, and where
it is destroyed (if at all). If you post those parts, it may become
aparent what is going on. So far, we have to assume that pengine==NULL,
which seems a little unlikely.
[snip]

This code is from Game Programming in 24 Hours by Michael Morrison. I
copied the code shortening some of the words. I did use this code and
his basic engine for a few projects. I didn't like the engine too much
because I didn't understand it well and decided not to create a windows
object for my later projects. I have limited experience in programming
and it is hard for me to tell good code from bad especially in larger
programs.

This was a construct that I have not seen in any other book so I asked
about it.

As I said above: first, I thought you may have encountered the singleton
pattern. However, the code you posted subsequently did neither confirm nor
refute that hypothesis since it did not contain the parts where pengine is
set (apart from some 0-initialization).

Assuming that it was meant to be a singleton, I would have expected code
like this:


class Singleton {
public:

static Singleton* Instance() {
static Singleton* result = new Singleton;
return ( result );
}

protected:
Singleton() {}
Singleton(const Singleton&);
Singleton& operator= (const Singleton&);
};


The point of a singleton is that there shall be one and only one instance of
a class, created upon first use and uniformly accessible throughout the
program. I do not know of any convincing examples as to when a singleton
pattern is actually a good idea; usually I do not see a logical necessity
for the class to prevent multiple instantiation and it suffices entirely to
only create a single object if you don't need several (keep it simple!).
Even though (or because) the singleton is possibly the most frequently
misused pattern, it is very important and you definitely should read about
it if you don't know it already.

I have written some fairly large programs that run well.
Because I work in isolation, I have limited influences of other
programmers.

This news group surely helps me a lot to cross check my ideas.

If this is bad code thanks for the help. It complied and rand and it
allowed me to animate some graphics around the screen.

I did not mean to insult you or to denigrate your work. I merely felt the
need to point out that the code has some serious shortcomings and should
not be used as a guideline for programming. The most devastating criticism,
however, came from yourself when you said:

I didn't like the engine too much because I didn't understand it well

Seems like it wasn't written well enough to be understood well.


As for the construct you found, maybe you can find where in the code the
variable pengine is set to a non-zero value. If so, you may be able to tell
whether we have a singleton before us, or not.


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top