C ++类 - 从一个类到另一个类 [英] C++ classes - getting from one class while in another

查看:119
本文介绍了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. a vector) of ingredients.
Then it may access their public methods. You just need to 'make aware' the Recipe class of the Ingredient one (that is by including the appropriate header file).


这篇关于C ++类 - 从一个类到另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆