passing parameters to constructor?

I am trying to figure out a way to pass parameters to a class constructor upon creation. for example;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Object{
    public:
	//Object constructor
	Object::object(Engine * ref_Engine ){ myengine = ref_Engine }

    private:
	//Engine Reference
	Engine * myengine;
};

class Engine{
    public:
	//Object instance
   	 Object myObject(this);
    
};


This isn't exactly what I'm trying to do but you get the idea. I mean i could create a 'Register_Engine' method or something in Object class and then pass the engine reference that way; but I guess I'm stingy with my code :)


EDIT:
lightbulb: Do I get errors because a hidden 'this' is already being passed to the constructor?
Last edited on
To call a constructor of a member object other than the default constructor, you need to use the initializer list:

1
2
3
4
5
class Engine{
    public:
      Engine() : myObject(this) {}
      Object myObject;
};
Topic archived. No new replies allowed.