如何在B类中定义A类的对象 [英] How to define an object from Class A in Class B

查看:312
本文介绍了如何在B类中定义A类的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++编程的初学者,我有一个(愚蠢的,我想)怀疑如何将一个对象的类传递到另一个类。假设我们有这两个类:

I'm a beginner in C++ programming and I've a (stupid, I think) doubt about how to pass an object's class into another class. Suppose we have these two classes:

class A {...}
class B {...}

,我想使用A中的对象到B.例如:

and I want to use an object from A into B. For example:

class B {
    A ab;
    [methods prototypes that include the object ab]
    method_B (A ab); //for example

    ...
}

问题是,我可以这样做吗?是否有意义,考虑面向对象编程?

The question is, can I do this? Does it make sense, thinking about object-oriented programming?

或者,我可以在main()中定义一个A的对象,然后我将从B调用一个方法将包括A的对象作为参数?

Or, I could define an A's object in main() and after that I would call a method from B that would include A's object as argument?

我的问题是如何使用对象从另一个类到另一个(功能独立!),而不违反面向对象的编程规则

My question is all about how to use object's from another class into another (functionally independent!) without "violating" the object oriented programming rules.

感谢您的帮助,

推荐答案

这个。许多图书馆和语言做到这一点。从OO视图,你需要设计A类,它将公开(公共)一个最小和有用的界面,但其他类(如B类)。

You can do this. Many libraries and languages do this. From OO view you need to need to design class A such that it will expose (public) a minimal and useful inteface to be used but other classes (such as class B).

这篇关于如何在B类中定义A类的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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