通过引用将对象传递到类中

发布于 2025-01-06 17:27:39 字数 819 浏览 0 评论 0原文

是的,我将尝试在这里解释我的想法。 我现在有两个班级,分别是class1和class2。 我想通过引用将 class2 的对象传递给 class1 的对象。

但我只想将其传入一次,而不是每次调用方法时都必须将其传入。

我尝试过创建指针,也尝试过创建引用,但无济于事。 任何帮助表示赞赏。

#include <iostream>

using namespace std;

class myclass
{
    private:
int i;

    public:
myclass()
{
}
void method()
{
    cout << "Enter num: ";
    cin >> i;
}
void display()
{
    cout << i;
}
};

class relatedclass
{

    public:

relatedclass(myclass ob)
{
    pmc = &ob;
}
myclass *pmc;

};

void main()
{
myclass mc;

relatedclass rc(mc);

//display value of mc.i
mc.display();
cout << endl;

//ok lets change the i variable
rc.pmc->method();
cout << endl;

//display new value of mc.i
mc.display();
cout << endl;
}

对于测试日期,我输入了 50,并且我预计 mc 对象会更新,现在 i 等于 50。

Right Ill try to explain my thinking here.
I have two classes at the moment, known as class1 and class2.
I want to pass an object of class2 into an object of class1, by reference.

but i want to only have to pass it in once, rather than having to pass it in every time a method is called.

I've tried creating a pointer and I have tried creating a reference, but to no avail.
Any help appreciated.

#include <iostream>

using namespace std;

class myclass
{
    private:
int i;

    public:
myclass()
{
}
void method()
{
    cout << "Enter num: ";
    cin >> i;
}
void display()
{
    cout << i;
}
};

class relatedclass
{

    public:

relatedclass(myclass ob)
{
    pmc = &ob;
}
myclass *pmc;

};

void main()
{
myclass mc;

relatedclass rc(mc);

//display value of mc.i
mc.display();
cout << endl;

//ok lets change the i variable
rc.pmc->method();
cout << endl;

//display new value of mc.i
mc.display();
cout << endl;
}

for the test date I entered 50, and i expected the mc object to be updated and i would now equal 50.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦中楼上月下 2025-01-13 17:27:39
struct class2 {
  int i, j;
};
struct class1 {
  class2& c2;
  class1(class2& c2) : c2(c2) {}
  void Froz() {
    c2.i = c2.j;
  }
  int Baz() {
    return c2.i * c2.j;
  }
};


EDIT: In your example, in the relatedclass constructor, you take the address of a local variable. That variable will be destroyed when the constructor returns. After that, your pmc pointer points to a destroyed object, which is a no-no.

将此行更改

relatedclass(myclass ob)

为:

relatedclass(myclass& ob)
struct class2 {
  int i, j;
};
struct class1 {
  class2& c2;
  class1(class2& c2) : c2(c2) {}
  void Froz() {
    c2.i = c2.j;
  }
  int Baz() {
    return c2.i * c2.j;
  }
};


EDIT: In your example, in the relatedclass constructor, you take the address of a local variable. That variable will be destroyed when the constructor returns. After that, your pmc pointer points to a destroyed object, which is a no-no.

Change this line:

relatedclass(myclass ob)

to this

relatedclass(myclass& ob)
﹉夏雨初晴づ 2025-01-13 17:27:39

您可以像这样在 class1 中保存 class2 的成员变量,并使用 set 变量设置该成员变量。

#include <iostream>

using namespace std;

class class2 
{
public:
    int m_i;
};

class class1 
{
public:
    void set(class2& c2) { m_c2 = c2; };
    class2 m_c2;
};

void main()
{
    class2 c2;
    c2.m_i = 2;

    class1 c1;
    c1.set(c2);

    cout << c1.m_c2.m_i << std::endl;

}

You can hold a member variable of class2 in class1 like this and have a set variable set the member variable.

#include <iostream>

using namespace std;

class class2 
{
public:
    int m_i;
};

class class1 
{
public:
    void set(class2& c2) { m_c2 = c2; };
    class2 m_c2;
};

void main()
{
    class2 c2;
    c2.m_i = 2;

    class1 c1;
    c1.set(c2);

    cout << c1.m_c2.m_i << std::endl;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文