What happens when you don't bind an object?

What happens when you call a function that returns a static object but don't bind it to a reference or pointer? Example
game.h
#include <iostream>

class Game
{
public:
static Game Create();
Game(int i){int g = i;};
private:
Game();
};

inline Game Game::Create()
{
return Game();
};

inline Game::Game()
{
std::cout << "constructor" << std::endl;
};


main.cpp
#include "game.h"

int main()
{
Game MyGame_ = Game::Create();
Game::Create();
<----What happens??
Game(4);
<----What happens??
std::cout << "sizeof(Game): " << sizeof(Game) << std::endl;
return 0;
}


Objects are created in stack area for main() but with no way to access them?
Objects are still deleted when main(){ } (or any other function they appear in) goes out of scope?

Thanks
1
2
3
4
5
6
7
8
class Game
{
public:
static Game Create(); //This is a static function - not a function returning a static object. 
Game(int i){int g = i;};
private:
Game();
};


You can't return a object of storage type 'static' from a function anyway. The returns from functions
are temporary objects.
You can return a copy of, or reference to, or pointer to
an object - but the actual return thing is temporary - although the object that was copied, or refereenced or pointed to may exist long after the function has completed.

Last edited on
Hmm okay but what is the meaning of a return if it's not assigned to a reference or a pointer?

Such as:

1 + 2;

Sure the answer is 3 but what does it mean if it's not assigned?
It's stored in what's called a 'temporary' object and it is destroyed at the end of the scope in which is was created.
Topic archived. No new replies allowed.