-2

I've changed from java to C++ a while ago. As soon as I try to code some complicated functions, I fail. I wanna make a variable that points to the instance of my class. But even I try to declare the instance, I get this error...

#include <iostream>
#include "Game.h"
#include <string>

Game instance; // that should be the Instance

class Game
{
    public: 

    Game()
    {
        instance = this; // here I got the error.
    }
  • 1
    Please edit your question and add the exact text of the error message. You can copy that easily from the Output tab in Visual Studio ( not the errors list). – drescherjm Jul 05 '19 at 16:45
  • 1
    This is not how you want to create a singleton. Study the Meyer’s Singleton (if you really want a singleton): http://laristra.github.io/flecsi/src/developer-guide/patterns/meyers_singleton.html – drescherjm Jul 05 '19 at 16:46
  • Welcome to StackOverflow. When asking for assistance with an error message please include the actual error text into the post itself. It is also a good idea to mention which specific version of the compiler such as which version of Visual Studio you are using. – Richard Chambers Jul 05 '19 at 16:47
  • *I've changed from java to C++ a while ago* -- In C++, `this` is a pointer, not an object. That explains why you're having a lot of issues -- assuming C++ uses the same rules as Java. – PaulMcKenzie Jul 05 '19 at 16:49
  • Also please realize that `java` and `c++` are very different languages even though they share some syntax. If you think like you do in `java` you will not produce good code in `c++` – drescherjm Jul 05 '19 at 16:49
  • 2
    Java is not C++. You will do yourself a big favor if you completely forget everything you know about Java, when attempting to learn C++. Despite the similar syntax, objects and classes in C++ work in fundamentally different ways from how they work in Java, and similar things are often completely different. Continuously drawing analogies with Java will only create non-stop confusion, like this. – Sam Varshavchik Jul 05 '19 at 16:54
  • Sounds like you want a singleton object. Search for how to correctly implement singleton in C++. – Joseph Willcoxson Jul 05 '19 at 19:50

3 Answers3

2

In C++, unlike Java, When you write:

Game instance; // that should be the Instance

you are creating an actual object of type Game. In Java this would be creating a handle variable and you would then need to use the new operator to actually create the Game object. That is not the way it works in C++.

In the source lines:

Game()
{
    instance = this; // here I got the error.
}

the variable this is actually a pointer to the current object. However instance is not a pointer variable, which would be defined by Game *instance; but rather is an actual Game object. Assigning a pointer value to something that is not a pointer is a compile error.

One modification to your source, which may or may not be what you actually want, is to do the following changes:

#include <iostream>
#include "Game.h"
#include <string>

Game *instance; // global that contains a pointer to a Game object, no object created.

class Game
{
    public: 

    Game()
    {
        instance = this; // we are creating an object of class Game now assign it to our global.
    }
}

However this doesn't really make sense in C++. The constructor may be called more than once for multiple Game objects.

Assuming that the header file Game.h contains the class definition, if you are wanting to just create a single instance of Game then the most straightforward would be to write it as:

#include <iostream>
#include "Game.h"
#include <string>

Game instance; // the global Game object that is created as part of the application starting up.

However if you are wanting to create a single instance using the singleton design pattern which will enforce that there is one and only one such object created, you will need to do additional work which will require a more involved knowledge of C++ and class construction.

Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
0

The cause of the issue is that ‘this’ is a pointer in c++, not a reference to an object as in Java, so you must dereference it to assign it to an object:

instance = *this;

Be wary though that this will copy your object. Although this is how you could do this, I’d like to say that it is discouraged to use global variables and singletons in general. I’d rather recommend you to use a static class. For a discussion about that look here. In summary: don’t use a singleton if you don’t have to.

0

You should de declare instance as a pointer to Game:

Game *instance = NULL;
Jorge
  • 137
  • 4
  • Personally I think this is a bad idea, but I'm willing to change my mind if you can update this answer with a good explanation of why this will help. – user4581301 Jul 05 '19 at 17:02
  • Now I get the Compiler-Error C2440 ""=": "Game *" can not be converted to "int *"" – SpruceCode Jul 05 '19 at 17:03
  • You shouldn’t use pointers if you don’t have to. The main reason is ownership. Should the memory pointed by the pointer be deallocated, if yes than who is supposed to deallocate it? –  Jul 05 '19 at 17:04
  • @SpruceCode that's probably an error resulting from another error and a legacy hold-over from the C programming language that still exists in some C++ compilers . when `Game *instance = NULL;` is found by the compiler, it doesn't know what `Game` is yet, so in C, it would assume `int`. Bad things follow from this. In C++, most things must be declared BEFORE you can use them. [A good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) will cover what you can get away with. – user4581301 Jul 05 '19 at 17:14