班级成员是另一个班级的对象 [英] class member is another class' object
问题描述
我是C ++的新用户...
I am a new C++ user...
我有一个问题,关于如何声明作为另一个类"classB"的对象的类"classA"的成员,知道"classB"具有一个带有字符串参数的构造函数(除了默认构造函数外) ).我在网上做了一些有关此问题的研究,但是对解决我正在解决的问题并没有多大帮助.
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" has a constructor that takes a string parameter (in addition to the default contructor). I did some research online about this issue however it did not help much to help me fix the issue I'm dealing with.
更具体地说,我想创建一个类,该类具有一个VideoCapture对象作为成员(VideoCapture是提供视频流的openCV类).
To be more specific, I want to create a class that has as member a VideoCapture object (VideoCapture is an openCV class that provide a video stream).
我的班级有这个原型:
class myClass {
private:
string videoFileName ;
public:
myClass() ;
~myClass() ;
myClass (string videoFileName) ;
// this constructor will be used to initialize myCapture and does other
// things
VideoCapture myCapture (string videoFileName /* : I am not sur what to put here */ ) ;
};
构造函数是:
myClass::myClass (string videoFileName){
VideoCapture myCapture(videoFileName) ;
// here I am trying to initialize myClass' member myCapture BUT
// the combination of this line and the line that declares this
// member in the class' prototype is redundant and causes errors...
// the constructor does other things here... that are ok...
}
我已尽最大努力以最简单的方式揭露我的问题,但我不确定自己是否能够做到...
I did my best to expose my issue in the simplest way, but I'm not sure I managed to...
感谢您的帮助和解答.
Thank you for your help and answers.
L.
推荐答案
您需要的是初始化列表:
What you need is initializer list:
myClass::myClass (string videoFileName) : myCapture(videoFileName) {
}
这将使用带有string
参数的构造函数构造myCapture
.
This will construct myCapture
using its constructor that takes a string
argument.
这篇关于班级成员是另一个班级的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!