[英]Calling functions from other classes
I want to use a function from my Page
class but I do not want to inherit from it. 我想使用我的
Page
类中的一个函数,但是我不想继承它。 Is there another way I can use the onPage(vector<string> vec)
function in my element
and elementX
class without using inheritance? 有没有其他方法可以在不使用继承的情况下在
element
和elementX
类中使用onPage(vector<string> vec)
函数? Would association Work? 联想会工作吗? I was thinking that.
我当时在想
Code is below: 代码如下:
class Page
{
string str;
vector<string>::iterator it;
void onPage(vector<string>vec);
};
class element
{
Page p;
};
class elementX : public element
{
};
Declare it public
, so foreign classes can access it. 将其声明为
public
,以便外国班级可以访问它。
class Page
{
string str;
vector<string>::iterator it;
public:
void onPage(vector<string>vec);
};
(you can use public
, private
and protected
to control access to class members, make sure to read them all up. private
is the default for classes). (您可以使用
public
, private
和protected
来控制对类成员的访问,请确保全部阅读它们private
是类的默认设置)。
Yes you can. 是的你可以。 The issue I can see is that you did not declare the
void onPage(vector<string>vec);
我看到的问题是您没有声明
void onPage(vector<string>vec);
function as public (place public:
before the function), since class members are private
(hidden for other classes) in C++ by default. 由于默认情况下C类中的类成员是
private
(对于其他类而言是隐藏的),因此它可以作为公共函数(在函数之前放置public:
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.