Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » C with structs or C++ with classes... Your choice and why.

This thread is locked; no one can reply to it. rss feed Print
C with structs or C++ with classes... Your choice and why.
Goalie Ca
Member #2,579
July 2002
avatar

Personally i write container classes and follow a "pipe and filter" paradigm most days. The best part of OOP in my mind is for wrapping up data. It makes things so nice and clean. :D The full OOP paradigm though with UML, use cases, everything a class and obj a using obj b is needlessly complicating things for what i do.

C++ allows me to just do that. But i must admit that a language like c# makes a really nice one to write desktop applications with.

-------------
Bah weep granah weep nini bong!

_Dante
Member #7,398
June 2006
avatar

Quote:

You can implement polymorphism using switch/case or if/else, using run-time type information and delegation, for example.

Touché. Using RTTI would certainly fill the bill (I'd opt for function pointers, but hey, that's just me). But a manually built if/else construct that requires me to add one more case for each additional subclass doesn't constitute polymorphism in my books. Whether I'm wrong about that is just semantics.

If fact, arguing about whether any particular language is OO, or procedural, or (name your methodology here), pretty much comes down to semantics. One could argue that the Intel instruction set encompasses all of these methodologies, because after all, you can accomplish all of those things with it, can't you? I'm going to agree to disagree on what sort paradigm any particular language follows, because we're not all going to agree on what the requirements are. We've reached the "Oh yeah?" "Yeah!" "Oh yeah?" "Yeah!" stage of the argument, and it's not interesting anymore.

Definitely agree with Shawn, his voice is probably one of the more sane in this particular thread, his love of C# notwithstanding ;). I've made the same point a number of times here - don't like a language feature? Then don't use it. Which is still my chief argument for using C++ over C. You get all the features of C, plus a bunch of other optional ones. Choice is good.

Java and C# (and lots of others, I'm sure) definitely "lock things down" a little more, although even some of those safe-guards can be short circuited. Dogma surrounding a language doesn't make it capable or incapable of anything; if you want to use return values to report errors in Java, there's nothing really stopping you. I've seen people write code that amounts to something like:

public static void main(String argv[])
{
    try
    {
        // entire program here
    }
    catch(Throwable t){}
}

(No, I've never seen anything quite that blatant, but if all of your catch blocks use Exception and are empty, then it amounts to roughly the same thing.)

-----------------------------
Anatidaephobia: The fear that somehow, somewhere, a duck is watching you

Onewing
Member #6,152
August 2005
avatar

Quote:

Definitely agree with Shawn, his voice is probably one of the more sane in this particular thread

I can't imagine anyone who creates something as great as allegro could really be...sane. ;D

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Shawn Hargreaves
The Progenitor
April 2000
avatar

Quote:

Java and C# (and lots of others, I'm sure) definitely "lock things down" a little more, although even some of those safe-guards can be short circuited. Dogma surrounding a language doesn't make it capable or incapable of anything; if you want to use return values to report errors in Java, there's nothing really stopping you.

This runs into the fuzzy line between technology and culture.

People don't often think of culture as an important part of a language, but really it is. That affects things like naming conventions and code layout, but also larger scale design patterns, where you go to find reusable code, who will help you when you get stuck, and so on.

In the same way as it doesn't force a single technological style, C++ doesn't have a single cuture either. Hence the endless holy wars over where to put your braces, but also this tends to make third party code feel very alien and often unnatural if you sit it alongside your own.

More modern languages (in which I would include Perl, Python, Ruby, etc, as well as Java and C#) tend to have much stronger and more unified cultures. Partly that's just because they haven't had so much time to diverge, but also I think there is some deliberateness on the part of the language designers. Since the rise of the internet, community is seen as very important, so languages are much more likely to have a few centralized hub sites, more in the way of tutorials and FAQ's and example code, and this all points people toward a more unified way of using the language.

That's certainly something Microsoft are very aware of. In addition to the technical decision that all the .NET framework will use exceptions rather than error codes, they put a lot of energy into making sure that the documentation and samples are aligned with that, and finding ways to encourage the whole community to adopt a more unified approach.

Is that a good thing? It's like the programmer version of globalization, and the same arguments apply as out in the wider world. In a way it is a shame to loose all the colorful local flavor and unique ways of doing things, but in practical terms, it is seriously useful to have a common language and be able to communicate and share with people from all over the world...

Nils Fagerburg
Member #7,193
May 2006
avatar

Quote:

C is for people that think beards are cool.

I must disagree here, I have a beard and I prefer C++.
Which gives me a wonderful idea: rather than argue about C vs C++ for the nth time, let's argue about beards instead. I, for one, am rather pro-beard, though I do accept that it does have some drawbacks which are more easily solvable using a non-beard paradigm.

--
There's no place like ~.

Mr. Big
Member #6,196
September 2005

HAHAHAHA! [Laughing my *** off]
For some reason that had me hysterically laughing!
This thread is fun to read.

_Dante
Member #7,398
June 2006
avatar

Beards are definitely where it's at for me. I've been working within the beard framework for years, and it's never let me down.

If you think about it, beards are a big part of the culture that Shawn describes; though much as it pains me to admit it, beards seem to represent the old school. Think about it - old school thinkers, like the Greeks: beards. Modern thinkers, like Newton (maybe not so modern, but at least this millennium), Einstein, Hawking: no beards.

Old school computer science - Ritchie, Thompson, Kernighan, Wozniak, Stroustrup: beards. Modern computer science - Bill Joy, Larry Wall, John Carmack, Michael Abrash: no beards.

Sure, there's exceptions, but those only prove the rule, don't they? Not sure where a mustache places you, probably somewhere in the middle.

-----------------------------
Anatidaephobia: The fear that somehow, somewhere, a duck is watching you

Shawn Hargreaves
The Progenitor
April 2000
avatar

I think you're missing the point. A good framework really shouldn't care whether or not the author had a beard. The important issue is how easy it is for users to customize, extend, or replace the facial hair. This is where functional techniques really come into their own. Have you ever tried using a disfunctional razer? No fun at all, trust me on that.

Mr. Big
Member #6,196
September 2005

Excuse me for interrupting your discussion about beards...
But Shawn, how do I write a... MIDI message processor function? ???

_Dante
Member #7,398
June 2006
avatar

I lean towards simplicity where my grooming interface is concerned - Wilkinson Sword is a fairly robust implementation, and it does what I need it to do. That being said, I think it's flexible enough for most facial hair paradigms, whether it be hairless, goatee, or full facial coverage (but clean below the neckline).

-----------------------------
Anatidaephobia: The fear that somehow, somewhere, a duck is watching you

Shawn Hargreaves
The Progenitor
April 2000
avatar

I can't believe you would hardcode your grooming implementation to depend a fixed worker type like Wilkinson Sword. Down that route lies tight coupling and unmaintainability in the face of changing business requirements. At the very least you should have your concrete razor type be a specialization of an abstract superclass. It also seems like the visitor pattern could be a good fit here, assuming you don't want to go the whole hog and implement a full on barber interface.

Goalie Ca
Member #2,579
July 2002
avatar

Beards can be nice but just add bloat to mustache ride functionality. Using beards increases the computational complexity of such a vehicle and turns an O(1) ride time into an O(nlogn) ride time. The additional workload can be increased so much that in some cases thick beards are not even tractable.

-------------
Bah weep granah weep nini bong!

Shawn Hargreaves
The Progenitor
April 2000
avatar

Don't be ridiculous: beards are linear!

Unlike bad haircuts. It is well known that understanding why anyone would choose to have a mullet is NP-hard.

_Dante
Member #7,398
June 2006
avatar

Quote:

assuming you don't want to go the whole hog and implement a full on barber interface

I have tried a barber interface on a couple of occasions, but found it to be too expensive an operation for my tastes. My current interface serves me well, mostly because it's a legacy platform, and I have no plans to upgrade. I've considered a going with a simple mustache or hairless paradigm, but my Nookie Provider claims some sort of incompatibility and has threatened to cancel my account, or even switch to an entirely different platform.

Sometimes you just gotta go with what works.

-----------------------------
Anatidaephobia: The fear that somehow, somewhere, a duck is watching you

Goalie Ca
Member #2,579
July 2002
avatar

Not having a beard, nor mustache, nor hair solves the Sleeping barber problem [1]

http://en.wikipedia.org/wiki/Sleeping_barber_problem

-------------
Bah weep granah weep nini bong!

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

Sometimes you just gotta go with what works.

Some/Most times the Nookie interface itself incurs huge costs all by itself.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

http://www.research.att.com/~bs/ieee_interview.html

Does anybody else read that as "home of BS"?

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

_Dante
Member #7,398
June 2006
avatar

Quote:

Quote:

Sometimes you just gotta go with what works.

Some/Most times the Nookie interface itself incurs huge costs all by itself.

Can't argue with that. However, the overhead associated with creating a new object is a lot more expensive than the maintenance of a currently existing one.

-----------------------------
Anatidaephobia: The fear that somehow, somewhere, a duck is watching you

Thomas Fjellstrom
Member #476
June 2000
avatar

That would be included in the overall maintenance costs ;)

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Shawn Hargreaves
The Progenitor
April 2000
avatar

It's not just the constructor overhead you have to worry about when switching provider, though. There are also a bunch of resource management issues when it comes to disposing of the previous instance.

The simplest approach is just to let the garbage collector to deal with this for you, but that can lead to unpredictable resource consumption while a potentially unbounded number of previous relationships are awaiting finalization.

Reference counts are simple and seem to work ok in practice, but don't deal well with cyclic structures so are best avoided unless you are strictly monogamous.

Old skool coders will argue in favor of manual cleanup, which is incredibly efficient but also very dangerous because it is so easy to accidentally leak resources like your house, car, kids...

Goalie Ca
Member #2,579
July 2002
avatar

Quote:

Reference counts are simple and seem to work ok in practice, but don't deal well with cyclic structures so are best avoided unless you are strictly monogamous.

Which is why people use shared pointers and weak pointers.

-------------
Bah weep granah weep nini bong!

Shawn Hargreaves
The Progenitor
April 2000
avatar

I've never really been into the whole boost::shared_ptr<> scene, but each to his own I suppose...

Thomas Fjellstrom
Member #476
June 2000
avatar

And why anyone would want to use/have a weak_pointer<> is beyond me.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

_Dante
Member #7,398
June 2006
avatar

One of our interns is a Python freak, and he claims it uses them all over the place. I don't recall what precisely the usage is (he's not here today), but Pygame does seem pretty nifty.

Shawn, quit being so funny, I'm not getting any work done.

-----------------------------
Anatidaephobia: The fear that somehow, somewhere, a duck is watching you

Shawn Hargreaves
The Progenitor
April 2000
avatar

How on earth did we end up talking about snake fetishes? I can think of a lot of things you could do with a snake, with or without weak and/or shared pointers, but I don't think I'd particularly enjoy any of them...

Leather, on the other hand, now that's something I can relate to.



Go to: