Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: What exactly is this doing?  (Read 3804 times)

0 Members and 4 Guests are viewing this topic.

Keyeszx

  • Newbie
  • *
  • Posts: 4
    • View Profile
What exactly is this doing?
« 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);
}

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: What exactly is this doing?
« Reply #1 on: May 03, 2014, 08:18:47 pm »
Maybe you should get a good C++ programming book and learn that before trying out SFML. After all, isn't that what the SFML Game Development book states in the preface?
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: What exactly is this doing?
« Reply #2 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.

Keyeszx

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: What exactly is this doing?
« Reply #3 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.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: What exactly is this doing?
« Reply #4 on: May 03, 2014, 09:15:31 pm »
Copy constructors are extremely important so I really hope it's that last one. =)

Keyeszx

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: What exactly is this doing?
« Reply #5 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.

select_this

  • Full Member
  • ***
  • Posts: 130
  • Current mood: just ate a pinecone
    • View Profile
    • darrenferrie.com
Re: What exactly is this doing?
« Reply #6 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...
Follow me on Twitter, why don'tcha? @select_this

Cirrus Minor

  • Full Member
  • ***
  • Posts: 121
    • View Profile
Re: What exactly is this doing?
« Reply #7 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...

select_this

  • Full Member
  • ***
  • Posts: 130
  • Current mood: just ate a pinecone
    • View Profile
    • darrenferrie.com
Re: What exactly is this doing?
« Reply #8 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.
Follow me on Twitter, why don'tcha? @select_this

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: What exactly is this doing?
« Reply #9 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

select_this

  • Full Member
  • ***
  • Posts: 130
  • Current mood: just ate a pinecone
    • View Profile
    • darrenferrie.com
Re: What exactly is this doing?
« Reply #10 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 :)
Follow me on Twitter, why don'tcha? @select_this

Cirrus Minor

  • Full Member
  • ***
  • Posts: 121
    • View Profile
Re: What exactly is this doing?
« Reply #11 on: May 07, 2014, 03:48:00 pm »
Yes, it's what I mean, no problem, and sorry for my bad english  :)

 

anything