Please have a look at the following code
GameObject.cpp
#include "GameObject.h"
GameObject::GameObject(void)
{
id = 0;
}
GameObject::GameObject(int i)
{
id = i;
}
GameObject::~GameObject(void)
{
}
GameObject.h
#pragma once
class GameObject
{
public:
GameObject(void);
GameObject(int);
~GameObject(void);
int id;
};
Main.cpp
#include <iostream>
#include "GameObject.h"
using namespace std;
int main()
{
GameObject obj1;
cout << obj1.id << endl;
GameObject obj2(45);
cout << obj2.id << endl;;
system("pause");
return 0;
}
Now, I want to make sure that it is not possible to define an object of the gameObject type using default constructor. How can I do it? Please help!
You can make the default constructor private.
As a sample, usually, when we implement a singleton class, we make the default constructor private and provide a static public “instance” method.