C ++类 - 从一个类到另一个类 [英] C++ classes - getting from one class while in another
本文介绍了C ++类 - 从一个类到另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2个课程(recipe.cpp中的食谱和ingredients.cpp中的成分)。来自
void Recipe :: show_recipe(std :: ostream& ; out) const
{
// 打印出含有成分的食谱
}
成分配方:: get_ingredient( int 位置) const
{
return Ingredient();
}
我正在尝试从配料类打印配方成分。我如何访问它们?
我尝试过:
指针 - 传递地址 - 我不知所措
解决方案
我想你的食谱应该有一个容器(例如vector
)成分。
然后它可以访问他们的公共方法。你只需要'知道'配料
的Ingredient
类(通过包含相应的头文件) )。
I have 2 classes (recipe in recipe.cpp and ingredients in ingredients.cpp). From
void Recipe::show_recipe( std::ostream &out ) const
{
//print out receipe with ingredients
}
Ingredient Recipe::get_ingredient( int location) const
{
return Ingredient();
}
I am trying to print recipe ingredients from the ingredient class. How do I access them?
What I have tried:
Pointers - passing addresses - I am at a loss
解决方案
I suppose your recipe should hold a container (e.g. avector
) of ingredients.
Then it may access their public methods. You just need to 'make aware' theRecipe
class of theIngredient
one (that is by including the appropriate header file).
这篇关于C ++类 - 从一个类到另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文