Took me five years to figure this out

Posted by Astryl on April 17, 2012, 9:15 a.m.

Apologies

My last blog was too short an irrelevant to anything. This ought to make up for it.

Something for C++ programmers

I've now spent time with both C# and Java, and no longer have too many complaints about them; in fact, I'm using C# for one of my games, and Java for some minor tools, so I must be liking them.

But here's something that bothered me about both languages:

// Java
public MyObject()
{
   super(9001);
}

// C#
public MyObject()
{
   base(9002);
}

Base/Parent constructors.

Don't get me wrong, I love them; what bothers me is… C++ doesn't have them.

Or so I thought. But after wading through many an obscure book and falling into a dark pit on some ancient-and-still-functioning BBS (Yeah, there are still functioning BBS servers), I finally found the knowledge I was seeking (And no thanks to those obscure books/weird message boards).

Completely by accident. You see, I was creating a constructor:

MSprite(int& ic):ID(ic) // This was the intended behavior
{
}

Of course, being Mega as I am and typing away at who-knows-how-many statements per minute, I typed this in and didn't notice the typo until I tried to compile:

MSprite(int& ic):(ic)
{
}

Of course, you'd be expecting a syntax error, but instead I got the "No matching call for Object(int&). Candidates are Object()…" message.

MSprite is inherited from Object, which meant only one thing… I had discovered the way to call a base-class constructor.

I can't believe that in the five years I've been programming in C++, I couldn't find that out, or do it before now. ;_;

Anyway, this is all a part of that game I mentioned I was going to make yesterday.

The little game I started yesterday

Here have another

Made in C++. :D

Also, yay another space shmup that I probably won't get anywhere near to a state of completion, but is great practice.

*Aims the Mega Rifle at Lunacy Star* <- Playful trolling, new members.

Sorry Taizen >:3

I started the actual code yesterday, and in 6 hours I was done with a framework that gives me all the functionality that Hydra had, but works better (Because I now know how to handle my pointers correctly, and make use of Early-Out algorithms for drawing).

It uses SFML, as usual, but was made from scratch. :3

Ludum Dare should be a piece of cake (6 hours working on the 'engine' will leave me with about 42 hours for the rest of the things I need to do).

My last blog was *that* close to being a "I'm feeling depressed" blog. Whay was I feeling depressed? I don't have a clue.

I'm feeling great now though.

My LD plan

Very simple. MAKE GAEM AND WIN!

Or, in simpler terms [/irony]:

> Create a stable and easy to extend OOP framework in the first 6-8 hours of the comp. [Proven that I can]

> Work on design and art assets in the next ?? hours. [Done it before]

> Cobble the final game together. [Done this before as well]

I already have plans for any level-creation I need to do; I'll use a layered map from TileStudio; I just have to write an importer; fortunately I find all binary IO pathetically simple to pull off in a short amount of time. :P

For music, I'm probably going to use GMGME, with standard SFML music objects as a fallback.

In addition to this, I'm going to create an Asset manager (You have no idea how much this speeds up the development process), an Instance manager and who-knows-what-else.

The Instance manager is just a simple system built into my base Game Object's that assigns a unique ID to each instance of the class (Using a global counter), and essentially allows me to remove objects by instance instead of by class.

Well, back to my papered desk (I've got a 'scratchpad' for the RPG design, and another one ready for LD on the weekend).

I've got a checklist for both LD and RPG4D, blank paper stuck on the walls, pencils everywhere, and a blank/cleaned desktop. I think.. Wait, what?!… I'm getting organized?!?! The world is going to end!

*Runs away and spreads the word with bouts of lunatic yelling*

Comments

Toast 12 years, 7 months ago

My LD plan:

Coffee and tea and energy drinks and beer and biscuits and pie.

About 12 hours of sleep

The game, you say? errrrrrrrr

sirxemic 12 years, 7 months ago

Under what weird rock have you been living to only discover base/parent constructors only now?

Or how few open source C++ libraries have you been using?

Josea 12 years, 7 months ago

Quote:
Under what weird rock have you been living to only discover base/parent constructors only now?
My thoughts exactly, it only makes me wonder what other things he hasn't 'discovered' yet.

Astryl 12 years, 7 months ago

EDIT: Cellphone posting fail'd.

Quote:
Or how few open source C++ libraries have you been using?
I use a few, but I prefer to reinvent the wheel, as you should know by now.

Quote:
Under what weird rock have you been living to only discover base/parent constructors only now?
I've known about them for some time, in other languages. The rock I've been living under is the Poverty rock, and having to rely on the Internet for all my knowledge.

Also: I still have a lot of bad C habits.

Quote:
My thoughts exactly, it only makes me wonder what other things he hasn't 'discovered' yet.
After discovering how to create the new and delete operators from scratch, as well as the hidden __main function (For messing around with kernels), I progressed to attempting to make a sandwich. Unfortunately, I can't figure out the code for the bread. >;3

sirxemic 12 years, 7 months ago

Quote:
The rock I've been living under is the Poverty rock, and having to rely on the Internet for all my knowledge.
That is no excuse at all. :P

Relying on the internet for your knowledge should be a reason why you would know about C++ base/parent constructors! Heck, I even learned C++ just by looking at many examples on the internet, and by talking to people who have more experience than me :P

Rob 12 years, 7 months ago

Quote:
Under what weird rock have you been living to only discover base/parent constructors only now?

This.

I figured that out after less than a month or two of C++…

It wouldn't let me compile because of some error bitching about not handling the construction of the base class. :(

Quote:
Relying on the internet for your knowledge should be a reason why you would know about C++ base/parent constructors!

Yeah, that. My classes are all in Java so it had nothing to do with school. I learned about them from googling the aforementioned error when I tried to construct a class without handling the base class' constructor lol.

Alert Games 12 years, 7 months ago

And I still need to get into C#. After only knowing a little bit of each programming language I really need to practice using it since I decided I would like to program with that over other languages.

But maybe a little java on the side.

Astryl 12 years, 7 months ago

Quote:
That is no excuse at all. :P

Relying on the internet for your knowledge should be a reason why you would know about C++ base/parent constructors! Heck, I even learned C++ just by looking at many examples on the internet, and by talking to people who have more experience than me :P

I have an excuse for that too: I'm a hard-headed learn-everything-myself-through-trial-and-error type. Or was, until I discovered that learning from online material is faster.

And I don't usually look at examples. Anyway, not knowing how to use the base-constructor hasn't caused me any trouble so far, but now it's one of those things I can think of a thousand uses for.

Rob 12 years, 7 months ago

Quote:
Anyway, not knowing how to use the base-constructor hasn't caused me any trouble so far, but now it's one of those things I can think of a thousand uses for.

So you've never had a class inherit from a base class that wasn't an abstract class? (or at least it gave me errors when I didn't specify the base constructor to use)

Astryl 12 years, 7 months ago

Quote:

So you've never had a class inherit from a base class that wasn't an abstract class? (or at least it gave me errors when I didn't specify the base constructor to use)
In most cases, I always supply a default constructor in my classes. The default constructors for all parents up the inheritance tree are called in order from the root.

Usually, I take advantage of this in the normal way (Default initialization).

However, this allows me to use the overloaded constructors to specifically initialize the parent objects first, which is a naturally useful bit of functionality. I had seen references to it in some of my books, but they didn't explain further; I recall shelving it in my list of 'things to look up sometime', but I forgot (No small wonder with all the stuff I've been doing).