2

This is probably very basic, but the syntax looks a little foreign to me coming from a .Net background. I'm trying to learn c++ to do some hobby game development and I came across this line in a tutorial:

sf::RenderWindow window(sf::VideoMode(800, 600), "My Game");

To me, this looks like RenderWindow is maybe a static method of the sf class and window is the instance name? I'm having trouble figuring this out or coming up with good search terms for google. Can anyone break this line down for me?

Also, feel free to rename the title, I don't know how to describe this line yet so I couldn't come up with a descriptive title.

ConditionRacer
  • 4,418
  • 6
  • 45
  • 67

6 Answers6

9
  sf::RenderWindow window(sf::VideoMode(800, 600), "My Game");

It creates an object of sf::RenderWindow, the name of the object is window. It will initialize window by passing the following parameters to sf::RenderWindow's constructor:

The first parameter:

 sf::VideoMode(800, 600)

is an object of sf::VideoMode, which is initialized by corresponding constructor/static functions of sf class per sf::VideoMode refrence;

VideoMode defines a video mode (width, height, bpp, frequency) and provides static functions for getting modes supported by the display device

the second parameter:

 "My Game"

is a string, probably means the title of the window (I guess), you may need to find out how the class VideoMode is defined.

taocp
  • 23,276
  • 10
  • 49
  • 62
  • Thanks, is this the normal way to construct objects in c++? I'm used to seeing something like Type myType = new Type("whatever"); And if it's an unusual way to construct objects, is there a name for it so I can look it up easier? – ConditionRacer Jun 12 '13 at 01:27
  • 5
    @Justin984, you can put objects in the heap with `new` or on the stack with this syntax. That's the essential difference. It's not that unusual. – paxdiablo Jun 12 '13 at 01:28
  • 2
    @Justin984 Yes, this is the normal way to construct objects in C++. It is just standard way to do it. Probably you can search call constructor to create objects? – taocp Jun 12 '13 at 01:29
  • 2
    In my experience, this is the normal way, unless you're making a pointer: `Type * myType = new Type(...)`. I don't know of any name for this syntax, but the other one is simply new syntax; aka - "creating class instance using new" – Narfanator Jun 12 '13 at 01:29
  • 2
    Actually - super important, this difference - `new` allocations memory off of the heap, rather than off of the stack. Objects on the heap **are not deleted when the scope closes**, which will result in a memory leak. The rule: **Never type "new" without typing "delete"** – Narfanator Jun 12 '13 at 01:31
  • @Narfanator I wouldn't go that far, 1 example that breaks your logic is the simple adoption of smart pointers where you can build an object with `new` but it got implicitly deleted when the pointer goes out of scope. For now it's better not to add too much words in this mix. – user2384250 Jun 12 '13 at 01:38
  • @paxdiablo as for my previous comment, I wouldn't explain this is this simple terms, the standard also doesn't really talks about the stack and the heap that way ... this can be confusing for a beginner, it's much better to study what kind of memory model the C++ is using and what that keywords are really doing. – user2384250 Jun 12 '13 at 01:40
  • @user2384250 Hmmmmm, going to disagree. They need to start learning to think about memory and object ownership, which adherence to the rule enforces. Smart pointers help you manage your memory, but they don't help to plan it. To toot my own horn: http://stackoverflow.com/questions/2036182/boost-shared-ptr-vs-weak-ptr-which-to-use-when/2036298#2036298 – Narfanator Jun 12 '13 at 01:43
  • @Narfanator your rule it's not a rule, and that is true. My point is that you can talk about memory model all the day but for a beginner this kind of statements are simply confusing, because when you will write real code and you start finding examples that are breaking this logic, everything starts to look confusing. – user2384250 Jun 12 '13 at 01:45
3

That line is declaring and initializing a variable called window of type sf::RenderWindow.

Types in C++ have constructors, and the constructor can be called when creating the variable, e.g. this creates an integer called x and initializes it to the value 3:

int x(3);

The constructor for the sf::RenderWindow class being called takes variables of type sf::VideoMode and string. The first argument being passed, sf::VideoMode(800, 600), is shorthand for creating a new instance of the type sf::VideoMode and passing it to the function. It's equivalent to:

sf::VideoMode mode(800, 600);
sf::RenderWindow window(mode, "My Game");

Note that the difference between x and y in the following:

Type x(3);
Type* y = new Type(3);

is that x is allocated on the stack, while y is allocated on the heap. Since x is on the stack, it will get de-allocated (and the destructor for type Type will be called) when the variable goes out of scope. y will stay around until you call delete.

jterrace
  • 64,866
  • 22
  • 157
  • 202
0

sf could be a namespace or class

RenderWindow is a type returned by function window()

VideoMode is a constructor of class VideoMode (or has a slightly chance to be a function in sf)

digit plumber
  • 1,140
  • 2
  • 14
  • 27
0

Two things that I can see.

Either sf is a class and RenderWindow is a static class and VideoMode is a member function within the class, or sf is a namespace and those are classes and or methods within the namespace. If it were my money, I'd probably go with the namespace notion.

Tyler Jandreau
  • 4,245
  • 1
  • 22
  • 47
0

probably ...

sf::RenderWindow is a type, it's RenderWindow from the sf namespace.

window is the name of the variable of type sf::RenderWindow that you are declaring here and between the () there are the arguments for the constructor used to build the window object, the last one is a constant, the first one is the value returned by sf::VideoMode(800, 600) .

user2384250
  • 548
  • 5
  • 13
-2

What you've indicated is a call to a static function named window.

The call:

sf::RenderWindow window(sf::VideoMode(800, 600), "My Game");

The return type is of sf::RenderWindow, and the function takes two arguments, sf::VideoMode(800, 600) and a literal string "My Game".

Nathan Ernst
  • 4,540
  • 25
  • 38
  • It's not a static function, it's a variable declaration. – Narfanator Jun 12 '13 at 01:26
  • `sf:VideoMode(800, 600)` is a static function, however. – Narfanator Jun 12 '13 at 01:27
  • @Narfanator: It really isn't. There are no static methods invoked by this line, only constructors. `sf::VideoMode()` is a declaration for a *temporary* object of type `VideoMode`, passing 800 and 600 as constructor parameters. Both `RenderWindow` and `VideoMode` are `class`es in the `sf` `namespace`. – greyfade Jun 12 '13 at 01:47
  • @greyfade You are correct sir. Well, partially - from usage alone, `sf::VideoMode(...)` may also be a function in the namespace. – Narfanator Jun 12 '13 at 01:57