SFML community forums

Help => General => Topic started by: Keyeszx on May 03, 2014, 08:10:41 pm

Title: What exactly is this doing?
Post by: Keyeszx on May 03, 2014, 08:10:41 pm
So I'm following the SFML Game Dev book and I've learned the basic of C++. But I don't know what it's doing with the constructor really. Specifically, the mWindow and mPlayer right after the constructor. Why can't those be called in the constructor, why is it outside? It works and all but I would like to know why it works and what is it.

Game::Game()
        : mWindow(sf::VideoMode(630, 380), "SFML")  //What is this and the line below it?
        , mPlayer()
{
        player.setRadius(40.f);
        player.setPosition(100.f, 100.f);
        player.setFillColor(sf::Color::Blue);
}

Why doesn't this work?
Game::Game()
{      
    mWindow(sf::VideoMode(630, 380), "SFML");
        mPlayer();
        player.setRadius(40.f);
        player.setPosition(100.f, 100.f);
        player.setFillColor(sf::Color::Blue);
}
Title: Re: What exactly is this doing?
Post by: zsbzsb on May 03, 2014, 08:18:47 pm
Maybe you should get a good C++ programming book and learn that before trying out SFML. (http://lmgtfy.com/?q=intializer+lists) After all, isn't that what the SFML Game Development book states in the preface?
Title: Re: What exactly is this doing?
Post by: Ixrec on May 03, 2014, 08:19:18 pm
That's called an initialization list.  All members of an object are initialized before the constructor starts executing.  If any of those members are themselves objects, that means their default constructors get called, which isn't always what you want.  The initialization list is how you override this behavior.

You should probably go learn the rest of C++ before trying to use a library like SFML.  Initialization lists are a need-to-know feature for anyone writing non-trivial code in the language.
Title: Re: What exactly is this doing?
Post by: Keyeszx on May 03, 2014, 09:11:31 pm
Thanks. I've actually been looking through my C++ book I used when I took a class 2 years ago. I found the chapter that goes over the initialization list but I don't remember going over it 2 years ago. Like it's right after overloading constructors which I do remember. There's also the copy constructor, which I don't remember.

So I'm guessing either my professor skipped that, went over it so briefly that it didn't stick, or I missed class and forgot to read the book that the lecture went over.
Title: Re: What exactly is this doing?
Post by: Ixrec on May 03, 2014, 09:15:31 pm
Copy constructors are extremely important so I really hope it's that last one. =)
Title: Re: What exactly is this doing?
Post by: Keyeszx on May 07, 2014, 04:26:31 am
I wish it was that last one. I was asking a friend who's in that class right now. They've gone past the section and he never went over either initialization list or copy constructors.
Title: Re: What exactly is this doing?
Post by: select_this on May 07, 2014, 01:10:30 pm
I wish it was that last one. I was asking a friend who's in that class right now. They've gone past the section and he never went over either initialization list or copy constructors.

I think it's safe to say that the class wasn't particularly good, then, since it has skipped vital information. Luckily, there's plenty of good resources out there to help fill in those gaps :)

I do worry about the standard of education in a lot of programming courses, and this hasn't exactly allayed my fears...
Title: Re: What exactly is this doing?
Post by: Cirrus Minor on May 07, 2014, 01:52:02 pm
Quote
I do worry about the standard of education in a lot of programming courses, and this hasn't exactly allayed my fears...
I think that in lots of programming courses, we don't really learn a language but we learn programming. It can be Pascal, C++, Java... Focus is often on algorithmic, or object concepts, pointers, or solving a problem, etc...
Well, it was the case for me...
Title: Re: What exactly is this doing?
Post by: select_this on May 07, 2014, 02:00:35 pm
Quote
I do worry about the standard of education in a lot of programming courses, and this hasn't exactly allayed my fears...
I think that in lots of programming courses, we don't really learn a language but we learn programming. It can be Pascal, C++, Java... Focus is often on algorithmic, or object concepts, pointers, or solving a problem, etc...
Well, it was the case for me...

All vital concepts, but also I'd argue that it's important to teach proper understanding of the tools that you'll be using.

I've encountered far too many graduates who only have a rudimentary understanding of their tool of choice because the course they took gave them no indication that they'd not gone into the required depth to properly utilise said tool.
Title: Re: What exactly is this doing?
Post by: eXpl0it3r on May 07, 2014, 02:05:11 pm
All vital concepts, but also I'd argue that it's important to teach proper understanding of the tools that you'll be using.
I think that's what Cirrus Minor tried to say. Yes, it's important, but unfortunately mostly all unis don't teach it.
Title: Re: What exactly is this doing?
Post by: select_this on May 07, 2014, 03:36:02 pm
I think that's what Cirrus Minor tried to say. Yes, it's important, but unfortunately mostly all unis don't teach it.

I wasn't disagreeing with him, just expanding on why I believe that we need to hold programming education to a higher standard; sorry if that wasn't clear :)
Title: Re: What exactly is this doing?
Post by: Cirrus Minor on May 07, 2014, 03:48:00 pm
Yes, it's what I mean, no problem, and sorry for my bad english  :)