Why not using 'int' in the cpp file in the constructor? (lecture 28)

My “problematic” code:

My output: (The arrow poinst to ‘1’, but when i dont use ‘int’ then there is a ‘666’ at that spot, as shown in the course).

Why is there a diffrent output and why use ‘int’ there and/or why not?

Thanks for any reply.
-Jonas

PS: Awesome course so far! :smiley:

1 Like

Looks like just a simple call of this method here… I would make sure to feed in the level name so you’re not putting any magic strings in your code. I don’t believe there’s anything to specifically worry about, as loading a level just remakes most things in the game (except for things like GameInstance).

It is just a simple console application, thus, I do not use any levels.

1 Like

@Carson_Hoffman I think you replied to the wrong thread.

@Jonormannen A constructor doesn’t return a value so int or any other type shouldn’t be put in front and I’m not sure how you got that to compile if you did do that.

1 Like

Agh! Had a bunch of tabs open, replied to the wrong thread like DanM said. Sorry :frowning:

1 Like

Hello @Jonormannen,

is the problem still there?

Cheers
Kevin

I know this is an old thread, but other people might have the same question. So, the answer to your questions is this:

When you use a type keyword in this way, you are creating a new variable. In your first screenshot, you are creating an additional ‘int’ variable inside the constructor that only exists inside the constructor. It just happens to have the same name as another already-defined variable. As soon as you leave the constructor, your variable with the 666 value is gone.

If you remove the ‘int’ keyword, you are no longer making a new variable. You would be accessing an existing variable.

Privacy & Terms