班级成员是另一个班级的对象

编程入门 行业动态 更新时间:2024-10-24 18:27:17
本文介绍了班级成员是另一个班级的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是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.

更多推荐

班级成员是另一个班级的对象

本文发布于:2023-10-31 05:56:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545208.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:班级   对象   成员

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!