473,405 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

What is going on here?

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
10 2080
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

52
by: Tony Marston | last post by:
Several months ago I started a thread with the title "What is/is not considered to be good OO programming" which started a long and interesting discussion. I have condensed the arguments into a...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
9
by: andrew | last post by:
Hi, I posted this message recently, but received no response, so I figured I'd try again. I can't find anybody who can tell me what's going on here. I'm having a problem with fieldsets in IE...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
8
by: Midnight Java Junkie | last post by:
Dear Colleagues: I feel that the dumbest questions are those that are never asked. I have been given the opportunity to get into .NET. Our organization has a subscription with Microsoft that...
63
by: Jake Barnes | last post by:
In the course of my research I stumbled upon this article by Alex Russel and Tim Scarfe: http://www.developer-x.com/content/innerhtml/default.html The case is made that innerHTML should never...
18
by: ben.carbery | last post by:
Hi, I have just written a simple program to get me started in C that calculates the number of days since your birthdate. One thing that confuses me about the program (even though it works) is...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Languageâ€, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
6
by: LurfysMa | last post by:
I am working on an electronic flashcard application. I have one version up and running using Visual Basic (6.0) and Access 2000. My long-range plans are to put it up on a website and sell...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.