By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,720 Members | 2,096 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,720 IT Pros & Developers. It's quick & easy.

What is going on here?

P: n/a
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.
Aug 5 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
JoeC wrote:
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?

--
Ian Collins.
Aug 5 '07 #2

P: n/a
In article <5h*************@mid.individual.net>, ia******@hotmail.com
says...

[ ... ]
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 5 '07 #3

P: n/a
Jerry Coffin wrote:
In article <5h*************@mid.individual.net>, ia******@hotmail.com
says...

[ ... ]
>> 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!

--
Ian Collins.
Aug 5 '07 #4

P: n/a
In article <5h*************@mid.individual.net>, ia******@hotmail.com
says...
Jerry Coffin wrote:
In article <5h*************@mid.individual.net>, ia******@hotmail.com
says...

[ ... ]
> 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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 6 '07 #5

P: n/a
JoeC wrote:
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.
Aug 6 '07 #6

P: n/a
Laurent D.A.M. MENTEN wrote:
JoeC wrote:
>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.

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.
Aug 6 '07 #7

P: n/a
JoeC wrote:
Ian Collins wrote:
>>
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.
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.

--
Ian Collins.
Aug 6 '07 #8

P: n/a
On Aug 6, 1:09 am, Jerry Coffin <jcof...@taeus.comwrote:
In article <5hn0poF3jvqe...@mid.individual.net>, ian-n...@hotmail.com
says...
[ ... ]
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.
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
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

Aug 6 '07 #9

P: n/a
Ian Collins wrote:
JoeC wrote:
>Ian Collins wrote:
>>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.

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.
Aug 6 '07 #10

P: n/a
JoeC wrote:
Kai-Uwe Bux wrote:
>JoeC wrote:
>>Ian Collins wrote:
JoeC wrote:
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){
>
[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
Aug 7 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.