C ++指向对象的指针,作为另一个对象的成员函数的参数 [英] C++ pointer to an object, as a parameter to a member funct of another object

查看:99
本文介绍了C ++指向对象的指针,作为另一个对象的成员函数的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我正在尝试自己学习c ++。原因是,我实际上知道c#而且我非常喜欢它,它非常整洁美观但不是portobale而且没有c ++那么快(从我研究过的)。



所以我学习的一个新东西是使用指针(我没有在c#中使用它们,因为它们在'不安全'的地方)。



我有两个类:A类和B类(它们都在main.cpp上)



Hi everyone!

I am trying to learn c++ by my own. The reason being, that I actually know c# and I really like it, its very neat and beautifull but not portobale and not as fast as c++(From what I´ve researched).

So one of the "new" things Im learning are the use of pointers (I didnt used them in c# because they where 'insecure').

I have Two classes: Class A and Class B(they are both on main.cpp)

class A
{
    public:
        int ClassA_variable;
};







class B
{
    public:
        void printClassA_variable(A *myObject)
        {
            cout << myObject->ClassA_variable << endl;
        }
};





B类成员函数应该是一个指向对象的指针并显示它的变量值。



主要:





Class B member function is supposed to take a pointer to an object and display its variable value.

In main:

int main()
{
   A *pointer = new A;
   B object2;

   pointer->ClassA_variable = 6;
   object2.printClassA_variable(pointer);

   return 0;
}



您可能认为这不是我的代码,但事实上,这是一个让我感到震惊的概念,我尝试以简单的方式创建它(简单的解决方案是正确的解决方案)。



所以这是因为A类在B类之前定义,但是如果我在A类中编写一个接受指向a的指针的函数B对象失败导致B类当时没有被定义。


$ b $b¿我该如何解决这个问题?
$ b $b¿如果我该怎么办?我想把A级和A级分开B级到2个不同的.h文件并在main中使用它们?



感谢您的答案或有用的建议。


You may think this aint my code but it actually is, this is a concept that hit me and I tried to create it in a simplyfied way(simple solutions are right solutions).

So this works because class A its defined before class B, but if I write a function in class A that accepts a pointer to a B object it fails cause class B isnt defined at that moment.

¿How can I solve this?
¿What should I do if I want to separate class A & class B into 2 different .h files and use them in main?

Thanks in advanced by your answers or helpful advices.

推荐答案

嗯,是的,我相信这是你的代码......至少是C#开发人员的代码。 :-)



如果你有2个相互引用的课程,你可能想学习的几种技巧是:

(1使用.cpp和.h文件,头文件中的类定义和.cpp文件中的实现。通常,每班一对文件。

(2)前向定义。你将在至少一个头文件中使用这种技术。



如果你声明一个类的前向指针,你可以在之前创建一个指向该类的指针。 class已定义。



如果在实现这些方法之前定义类的接口(即数据和方法),则可以编写调用它们的代码。 ...哦,是的,那么你可以编写方法。



如果你在一个地方定义和实现一个类(就像你做的那样,这是C#风格) ,然后编译器可以(并且通常会)认为所有方法都是'内联'。您通常不希望这样,除了访问器或非常短的方法与简单的逻辑。通常,您希望为给定的类使用标头和实现文件。有一些例外,但它们相对较少。



这里有一些示例代码(我手工创建并且没有编译,如果有任何错误) 。



Well, yes I believe it is your code... at least the code of a C# developer. :-)

If you have 2 classes that reference each other, a couple of techniques you likely want to learn are:
(1) use of .cpp and .h files, with the class definition in the header file and the implementation in the .cpp file. Usually, one pair of files per class.
(2) forward definitions. You will use this technique in at least one of your header files.

If you declare a forward pointer to a class, you can create a pointer to that class before the class is defined.

If you define a class's interfaces (ie. the data and methods) before those methods are implemented, you can write code that calls them. ... oh yeah, then you can write the methods.

If you define and implement a class in one place (as you did, which is the C# style), then the compiler can (and usually does) consider that all of the methods are to be 'inline'. You typically don't want this, except for accessors or very short methods with trivial to simple logic. Generally you want to use both a header and implementation file for a given class. There are exceptions to this but they are relatively rare.

Here is some sample code (which I created by hand and didn't compile, if any errors are present).

// ClassA.h
class B;  // forward declaration

class A
{
public:
  A(B* b) { m_b = b; }
  void foo();
private:
  B* m_b;
};




// ClassA.cpp
#include "ClassA.h"
#include "ClassB.h"

A::foo()
{ m_b->bar(); }







// ClassB.h
class A;

class B
{
public:
  B(A* a) { m_a = a; }
  void bar();
private:
  A* m_a;
};




// ClassB.cpp
#include "ClassB.h"
#include "ClassA.h"

void B::bar()
{
  m_a->foo();
}





此代码定义了两个可以互相引用的类。希望你知道如何在剩下的路上转动曲柄......



This code defines two classes that can reference each other. Hopefully you know how to turn the crank the rest of the way...


这是技术解决方案。在实践中,这种相互依赖性是最好的设计。



This is the technical solution. In practice this type of interdependency is best designed out.

class B
{
public:
	int ClassB_variable;
    void printClassA_variable(class A *myObject);
};


class A
{
    public:
        int ClassA_variable;

        void printClassB_variable(B *BObject)
        {
            cout << BObject->ClassB_variable << endl;
        };

};

void B::printClassA_variable(A *myObject)
{
       cout << myObject->ClassA_variable << endl;
}


int _tmain(int argc, _TCHAR* argv[])
{
A object1 = {6};
B object2 = {9};
B* object3 = new B;


object1.printClassB_variable(&object2);
object2.printClassA_variable(&object1);
object3->printClassA_variable(&object1);


return 0;
}


这篇关于C ++指向对象的指针,作为另一个对象的成员函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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