Reputation: 319
New in c++, I would like to use an class with a constructor in another class and use its methods globally.
MyMachine class calls MyComponent class. It works if MyComponent has no constructor, but I don't find how to call it with a constructor.
Working:
#include <iostream>
/*********************************************
* MyComponent
*********************************************/
// MyComponent.h
class MyComponent
{
public:
int _id;
MyComponent();
int getId();
};
// MyComponent.cpp
MyComponent::MyComponent()
{
}
int MyComponent::getId()
{
return _id;
}
/*********************************************
* MyMachine
*********************************************/
// MyMachine.h
class MyMachine
{
private:
MyComponent component;
public:
MyMachine();
void printComponentInfo();
};
MyMachine::MyMachine()
{
component._id = 123456;
}
void MyMachine::printComponentInfo()
{
int id = component.getId();
std::cout << id << "\n";
}
/*********************************************
* Main
*********************************************/
int main()
{
MyMachine machine;
machine.printComponentInfo();
return 0;
}
Displays 123456
But with the constructor, it is not working:
#include <iostream>
/*********************************************
* MyComponent
*********************************************/
// MyComponent.h
class MyComponent
{
public:
int _id;
MyComponent(int id);
int getId();
};
// MyComponent.cpp
MyComponent::MyComponent(int id)
{
_id = id;
}
int MyComponent::getId()
{
return _id;
}
/*********************************************
* MyMachine
*********************************************/
// MyMachine.h
class MyMachine
{
private:
MyComponent component(int id);
public:
MyMachine();
void printComponentInfo();
};
MyMachine::MyMachine()
{
component._id = 123456;
}
void MyMachine::printComponentInfo()
{
int id = component.getId();
std::cout << id << "\n";
}
/*********************************************
* Main
*********************************************/
int main()
{
MyMachine machine;
machine.printComponentInfo();
return 0;
}
An error is thrown
all.cpp: In constructor ‘MyMachine::MyMachine()’: all.cpp:43:7: error: invalid use of member function ‘MyComponent MyMachine::component(int)’ (did you forget the ‘()’ ?) component._id = 123456; ^~~~~~~~~ all.cpp: In member function ‘void MyMachine::printComponentInfo()’: all.cpp:48:16: error: invalid use of member function ‘MyComponent MyMachine::component(int)’ (did you forget the ‘()’ ?) int id = component.getId();
Upvotes: 0
Views: 303
Reputation: 1110
I think that your issue is with understanding when the constructor is being called.
When you construct a new object of MyMachine
, the code first initializes all of MyMachine
's members before getting into the body of the constructor function. You can initialize the members on your own in the initializer list before getting into the ctor body:
MyMachine::MyMachine(int id) :
_id(id)
{
}
In fact, this is a more efficent way to handle initialization, since all members must be initialized before the ctor body, therefore, in the previous state, the code will:
_id
using its no-argument ctor.id
into MyComponent.When you use the initializer list, there is only one step in the initialization, which is more efficient (especially in much more complex types!).
Upvotes: 0
Reputation: 12283
You have 2 mistakes:
MyComponent
class:MyComponent() : _id(0) {}
MyMachine
class:class MyMachine {
private:
MyComponent component;
// ...
};
Upvotes: 3