How to make a class A object a member of class B?
Now I will clarify my point:

class A { } class B { private: A A_object; public: B(){ this->A_object = /* Что мне написать, чтоб в конструкторе присвоить переменной A_object объект класса A? */ } } 

I realize that the question is stupid. But I am new to these languages, I used to write only in python, and then to create a class object, you just need to write A () (where A is the name of the class). Well, in C ++, I do not know.

    1 answer 1

    If I understand you correctly, then nothing needs to be written. The object is created by itself, the default constructor.

    If the situation was more difficult - for example,

     class A { public: A(int a) { ...} } 

    then it would be possible to specify a parameter for this constructor, say, like this:

     class B { private: A A_object; public: B(int a):A_object(a) { ... 
    • Which one? There is already an object in this variable. If it was a pointer - A* A_object , then yes, it would have to be initialized, say, B():A_object(new A) {... In your version, nothing is needed. - Harry
    • Hmm, I myself am confused. I apologize - Kirill Leontyev
    • Thanks for explaining the B (int a) syntax: A_object (a), it was also not clear to me, and I didn’t know what to google. - Kirill Leontyev
    • @NewView Fixed. - Harry
    • @ KirillLeontiev Initialization list. - bipll