Stop porting C code to C++ (was: Re: Howto dipslay a text with SDL?)

Maybe off-topic but…,

although I am a C++ programmer for years, I have something against porting C
code to C++ code. Because something was originally written in C, means that
it was designed from a procedural point of view. It you wrap a class around
it, does NOT make it proper OOP. Because most API’s have some kind of
initialize method, like SDL_Init for example. Using OOP you cannot call this
function from a constructor, because then it would be called every time. So,
in the end you’ll end up with some static methods (read: functions) and
static members (read: global-variables). As a result you get:

  1. probably more code
  2. unnecessary complexity
  3. Unnatural feeling

Don’t get me wrong, this was NOT a flame! I’m just ‘warning’ you. For
example: I used to have some singletons in my project, a Video object, a
Soundsystem object and some resource-management objects. I ‘stripped’ the
class-code and made them API’s, and guess what? It feels so much natural
(sorry, second time) when calling a function. So, I made myself a new
"rule": when I only need one instance, I don’t make a class, just a set of
functions. I use objects though, for my game objects like tiles, animations,
enemies and such. Because those kind of object makes you think in objects,
so that in feels very comfortable and logic to do so…

Kind regards (I mean it),

Patrick.> ----- Original Message -----

From: kai.blin@med.uni-tuebingen.de (Kai Blin)
To:
Sent: Thursday, August 15, 2002 6:31 AM
Subject: Re: [SDL] Howto dipslay a text with SDL?

On Wednesday 14 August 2002 23:29, Jacek Pop?awski wrote:

On Wed, Aug 14, 2002 at 02:41:47PM +0200, Jean wrote:

I mean how can I write a text in my sdl window?

Please seach for “Sfont”.

If you’re doing C++, you might want to have a look at SoFont, too. It’s
not
quite up to date, and I can’t seem to reach the developers, but I’m just
trying to port SFont to C++ again, and SoFont is a great help. (Not that
the
source was documented or something :wink:

Cheers, Kai


Kai Blin Linux system administrator Tel: Ring-86592
Allgemeine Chirurgie Universitaetsklinikum Tuebingen

Baseball is a skilled game. It’s America’s game - it, and high taxes.
– The Best of Will Rogers


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Maybe off-topic but…,

although I am a C++ programmer for years, I have something against porting
C code to C++ code. Because something was originally written in C, means
that it was designed from a procedural point of view. It you wrap a class
around it, does NOT make it proper OOP. Because most API’s have some kind
of initialize method, like SDL_Init for example. Using OOP you cannot call
this function from a constructor, because then it would be called every
time. So, in the end you’ll end up with some static methods (read:
functions) and static members (read: global-variables). As a result you
get:

  1. probably more code
  2. unnecessary complexity
  3. Unnatural feeling

I know. But I’m switching the code to use STL objects instead of arrays like
SFont uses them (Without checking for overflows). The alternative I’d see was
securing SFont, which would also give me more code. Second (I’m not sure if
you know how SFont works), SFont uses some really crazy struct and some
ersatz overloading to work with multiple fonts. This is something that could
be done more elegantly by c++ function overloading or even classes.

Don’t get me wrong, this was NOT a flame! I’m just ‘warning’ you. For

Sure. At least it would be poor flaming, as this is actually a good tip. :wink:

example: I used to have some singletons in my project, a Video object, a
Soundsystem object and some resource-management objects. I ‘stripped’ the
class-code and made them API’s, and guess what? It feels so much natural
(sorry, second time) when calling a function. So, I made myself a new
"rule": when I only need one instance, I don’t make a class, just a set of
functions. I use objects though, for my game objects like tiles,
animations, enemies and such. Because those kind of object makes you think
in objects, so that in feels very comfortable and logic to do so…

Yup. The desire to use multiple fonts was what made me looking at SoFont,
actually. Your warning is actually very good for most cases, but I think I
just fit into your exception.

Kind regards (I mean it),

Thanks :slight_smile:

KaiOn Thursday 15 August 2002 18:08, Patrick Kooman wrote:


Kai Blin Linux system administrator Tel: Ring-86592
Allgemeine Chirurgie Universitaetsklinikum Tuebingen

Possessions increase to fill the space available for their storage.
– Ryan

initialize method, like SDL_Init for example. Using OOP you cannot call this
function from a constructor, because then it would be called every time. So,

To prevent what you are talking about is a simple check of a flag in the
constructor to see if SDL_Init has already been performed. If not,
perform SDL_Init and set the flag so you dont have to do it again.

Don’t get me wrong, this was NOT a flame! I’m just ‘warning’ you. For
example: I used to have some singletons in my project, a Video object, a
Soundsystem object and some resource-management objects. I ‘stripped’ the
class-code and made them API’s, and guess what? It feels so much natural
(sorry, second time) when calling a function.

Thats an opinion. Using C++ or C, in my opinion, is more of a personal
preference rather than picking one over the other due to capabilities.
I think your comments may be misinterpreted as saying “C is better
than C++” and that could be considered a flame.

well…while the difference in execution speed betweeen C and C++ is for the
most part very little, there is some inherant differences between them
however. For instance, in C programs, your structured data is stored in a
single chunk of data accessed through pointers that reside in in the data
segment(global vars) or on the stack(local vars). In C++ however, your
classes arent just one contiguous chunk of memory, but rather a table of
pointers, and if your using class inheritance, in ram you class could even
be h a table of pointers to pointers or a table of tables of tables of
pointers and so on, making for slower variable referance time and code
execution because of memory referancing. Also, when you program in C, your
designing programs to be organized optimaly to be understood by the
computer, but when your programming in C++ and other OOP languages, you are
designing your programs to be organized optimaly to be understood by the
PROGRAMMER. At the risk of this verging on flame, i try to avoid OOP
because while it is extremely popular, i think it is designed for ease of
use by the programmer, whereas for a language to be most efficient, it
should be designed with the CPU in mind. There is of course the issue of
rapid application developement and readability, but those things aside, i
think C inherantly makes faster running programs than C++.> ----- Original Message -----

From: stanley.brown@zimmer.com (Stanley E Brown)
To:
Sent: Friday, August 16, 2002 8:38 AM
Subject: Re: Stop porting C code to C++ (was: Re: [SDL] Howto dipslay a text
with SDL?)

initialize method, like SDL_Init for example. Using OOP you cannot call
this

function from a constructor, because then it would be called every time.
So,

To prevent what you are talking about is a simple check of a flag in the
constructor to see if SDL_Init has already been performed. If not,
perform SDL_Init and set the flag so you dont have to do it again.

Don’t get me wrong, this was NOT a flame! I’m just ‘warning’ you. For
example: I used to have some singletons in my project, a Video object, a
Soundsystem object and some resource-management objects. I 'stripped’
the

class-code and made them API’s, and guess what? It feels so much natural
(sorry, second time) when calling a function.

Thats an opinion. Using C++ or C, in my opinion, is more of a personal
preference rather than picking one over the other due to capabilities.
I think your comments may be misinterpreted as saying “C is better
than C++” and that could be considered a flame.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

I agree that C is faster. I also think it is much simpler than C++. To
get the
benefits of objects, I use a single self describing data structure for
all of my “objects”.
It also has the advantage of allowing object manipulators to work for
ANY object.
For instance, I have 1 sort routine that is available for all data
structures.

In reference to the SDL and OpenGL libraries, they are much easier to
use as C libs
than with C++ wrappers, IMHO.

Matt

Atrix Wolfe wrote:>well…while the difference in execution speed betweeen C and C++ is for the

most part very little, there is some inherant differences between them
however. For instance, in C programs, your structured data is stored in a
single chunk of data accessed through pointers that reside in in the data
segment(global vars) or on the stack(local vars). In C++ however, your
classes arent just one contiguous chunk of memory, but rather a table of
pointers, and if your using class inheritance, in ram you class could even
be h a table of pointers to pointers or a table of tables of tables of
pointers and so on, making for slower variable referance time and code
execution because of memory referancing. Also, when you program in C, your
designing programs to be organized optimaly to be understood by the
computer, but when your programming in C++ and other OOP languages, you are
designing your programs to be organized optimaly to be understood by the
PROGRAMMER. At the risk of this verging on flame, i try to avoid OOP
because while it is extremely popular, i think it is designed for ease of
use by the programmer, whereas for a language to be most efficient, it
should be designed with the CPU in mind. There is of course the issue of
rapid application developement and readability, but those things aside, i
think C inherantly makes faster running programs than C++.

----- Original Message -----
From: “Stanley E Brown” <stanley.brown at zimmer.com>
To:
Sent: Friday, August 16, 2002 8:38 AM
Subject: Re: Stop porting C code to C++ (was: Re: [SDL] Howto dipslay a text
with SDL?)

initialize method, like SDL_Init for example. Using OOP you cannot call

this

function from a constructor, because then it would be called every time.

So,

To prevent what you are talking about is a simple check of a flag in the
constructor to see if SDL_Init has already been performed. If not,
perform SDL_Init and set the flag so you dont have to do it again.

Don’t get me wrong, this was NOT a flame! I’m just ‘warning’ you. For
example: I used to have some singletons in my project, a Video object, a
Soundsystem object and some resource-management objects. I ‘stripped’

the

class-code and made them API’s, and guess what? It feels so much natural
(sorry, second time) when calling a function.

Thats an opinion. Using C++ or C, in my opinion, is more of a personal
preference rather than picking one over the other due to capabilities.
I think your comments may be misinterpreted as saying “C is better
than C++” and that could be considered a flame.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Atrix Wolfe wrote:

well…while the difference in execution speed betweeen C and C++ is
for the most part very little, there is some inherant differences
between them however. For instance, in C programs, your structured
data is stored in a single chunk of data accessed through pointers
that reside in in the data segment(global vars) or on the stack(local
vars). In C++ however, your classes arent just one contiguous chunk
of memory, but rather a table of pointers, and if your using class
inheritance, in ram you class could even be h a table of pointers to
pointers or a table of tables of tables of pointers and so on, making
for slower variable referance time and code execution because of
memory referancing.

Why do you say this? Every implementation I’ve ever seen of any C++
class is exactly the same as it would be for a C struct, with the single
exception of a 4-byte pointer at the start which allows for type
identification and virtual function despatch. And if the class isn’t
part of a virtual hierarchy it doesn’t even have that exception. There’s
no performance hit at all unless you do things badly.

Also, when you program in C, your designing
programs to be organized optimaly to be understood by the computer,
but when your programming in C++ and other OOP languages, you are
designing your programs to be organized optimaly to be understood by
the PROGRAMMER.

Even on this list, about multimedia applications where performance is
supposedly paramount, there are far more posts about something the
programmer has misunderstood or done incorrectly than there are about
optimisation or performance. Draw your own conclusions…

There is of
course the issue of rapid application developement and readability,
but those things aside, i think C inherantly makes faster running
programs than C++.

Only if you’re a poor C++ programmer and a good C programmer.–
Kylotan
http://pages.eidosnet.co.uk/kylotan

There is of
course the issue of rapid application developement and readability,
but those things aside, i think C inherantly makes faster running
programs than C++.
Only if you’re a poor C++ programmer and a good C programmer.

And I’ve seen an ungodly amount of poor C++ programmers. In the right
hands it’s a powerful tool. In the wrong hands it’s a dangerous weapon. I
have countless stories of just absolutely stupidity on the part of C++
“programmers” who thought that learning C++ was whizzy/cool, without
having a firm understanding of the constructs of the language, or even the
basics of C for that matter. I point blank asked a programmer recently why
he’s putting everything in classes, and he had no idea. Use of C++ has
become rampant and religious with no underlying understanding of why it
should or should not be used. Many can’t separate the fact that OOP !=
C++. You can write nonobject oriented code in C++ and highly object
oriented code in C, but the C++ zealots don’t understand that. OOP Is a
programming methodology, not a language.

–>Neil-------------------------------------------------------------------------------
Neil Bradley What are burger lovers saying
Synthcom Systems, Inc. about the new BK Back Porch Griller?
ICQ #29402898 “It tastes like it came off the back porch.” - Me

I point blank asked a programmer recently why
he’s putting everything in classes, and he had no idea.

I don’t know about most schools, but my school’s CS department is about 95%
Java now. And of course, in Java, everything must be in a class. Perhaps Java
practices are leaking over to C++?

Since entering school I’ve been programming in Java almost constantly, and
only recently have started up a personal C++ project, and I’m still
readjusting myself.

MattOn Friday 16 August 2002 01:54 pm, Neil Bradley wrote:

yeah, thats what im talking about, the tables it keeps for virtual functions
and hierchy junk. The difference is very minute, but it is there (like
.001% speed difference for well written code).

C and C++ are the kickinest languages around, even if C++ is just a TINY bit
slower. ::smirk::> ----- Original Message -----

From: kylotan@kylotan.eidosnet.co.uk (Kylotan)
To:
Sent: Friday, August 16, 2002 10:44 AM
Subject: Re: Stop porting C code to C++ (was: Re: [SDL] Howto dipslay a text
with SDL?)

Atrix Wolfe wrote:

well…while the difference in execution speed betweeen C and C++ is
for the most part very little, there is some inherant differences
between them however. For instance, in C programs, your structured
data is stored in a single chunk of data accessed through pointers
that reside in in the data segment(global vars) or on the stack(local
vars). In C++ however, your classes arent just one contiguous chunk
of memory, but rather a table of pointers, and if your using class
inheritance, in ram you class could even be h a table of pointers to
pointers or a table of tables of tables of pointers and so on, making
for slower variable referance time and code execution because of
memory referancing.

Why do you say this? Every implementation I’ve ever seen of any C++
class is exactly the same as it would be for a C struct, with the single
exception of a 4-byte pointer at the start which allows for type
identification and virtual function despatch. And if the class isn’t
part of a virtual hierarchy it doesn’t even have that exception. There’s
no performance hit at all unless you do things badly.

Also, when you program in C, your designing
programs to be organized optimaly to be understood by the computer,
but when your programming in C++ and other OOP languages, you are
designing your programs to be organized optimaly to be understood by
the PROGRAMMER.

Even on this list, about multimedia applications where performance is
supposedly paramount, there are far more posts about something the
programmer has misunderstood or done incorrectly than there are about
optimisation or performance. Draw your own conclusions…

There is of
course the issue of rapid application developement and readability,
but those things aside, i think C inherantly makes faster running
programs than C++.

Only if you’re a poor C++ programmer and a good C programmer.


Kylotan
http://pages.eidosnet.co.uk/kylotan


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

use by the programmer, whereas for a language to be most efficient, it
should be designed with the CPU in mind. There is of course the issue of

Er… C isn’t designed with the CPU in mind…
If you want to use such a language, why don’t you use ASM ? ;)))On 2002-08-16 at 18:37:02 [+0200], you wrote:


Qui e’ Jack Burton, del Pork-Chop Express,
che parla a chiunque sia in ascolto…

http://digilander.iol.it/burton666/index.html