class member is another class' object

前端 未结 2 437
别跟我提以往
别跟我提以往 2021-01-26 01:45

I am a new C++ user...

I have a question regarding how to declare a member of a class \"classA\" that is an object of another class \"classB\", knowing that \"classB\" h

相关标签:
2条回答
  • 2021-01-26 02:20

    If you want a VideoCapture to be a member of the class, you don't want this in your class definition:

    VideoCapture myCapture (string videoFileName /* :  I am not sur what to put here */ )  ;
    

    Instead you want this:

    VideoCapture myCapture;
    

    Then, your constructor can do this:

    myClass::myClass (string PLEASE_GIVE_ME_A_BETTER_NAME)
    : myCapture(PLEASE_GIVE_ME_A_BETTER_NAME),
    videoFileName(PLEASE_GIVE_ME_A_BETTER_NAME)
    {
    }
    
    0 讨论(0)
  • 2021-01-26 02:26

    What you need is initializer list:

    myClass::myClass (string videoFileName) : myCapture(videoFileName) {
    }
    

    This will construct myCapture using its constructor that takes a string argument.

    0 讨论(0)
提交回复
热议问题