【问题标题】:passing a child object value to callee class through pointer in C++通过 C++ 中的指针将子对象值传递给被调用者类
【发布时间】:2018-08-27 11:06:46
【问题描述】:

原始代码似乎造成了很多混乱,因此我删除并添加了其他解释性代码。希望它不会引起同样的混乱。

将此视为Main calss (something like manager class)

hpp:

 private: std::vector<int>* _cache;

cpp:

 whatever* whatever::somefunction(const String& nothing)
    {
        const auto newWhatever = new whatever{nothing, _cache};
        return newWhatever; // not important
    }

Other Class(做一些工作并返回结果的类)

hpp:

private: std::Vector<int> _cache;

cpp:

     class OtherClass
{
    std::vector* _value;
    std::vector _result;
public:
    OtherClass(const string& nothing, std::vector<int>* cache) : _value{cache}

    void calculateresult()
    {
      *_value = _result;
      }
}

returning value from the method is impossible as per the original setup

我想将OtherClass中得到的结果捕获到Main class的指针中。

目的是:OtherClass 是临时的,它将被销毁并每 10 秒重新创建一次。所以我需要存储这个OtherClass 的结果并将其用于下一次实例化。正如你所看到的,这个OtherClass 的实例化将从Main class 发生,它就像一个经理。

当我调试时,我确实看到为_value 分配了一个地址,并且取消引用它可能不起作用,我不知道。

但它在分配期间失败了,即 *_value = _result;

左边是 T = QVector*,右边是 QVector。

代码异常:qvector.h

template <typename T>
QVector<T> &QVector<T>::operator=(const QVector<T> &v)
{
    if (v.d != d) {
        QVector<T> tmp(v);
        tmp.swap(*this);
    }
    return *this;
}

【问题讨论】:

  • 您显示的初始代码与您描述该代码的使用方式并不完全匹配。 Child 类中没有 doSomething
  • 至于如何将一个值“传递”回“父”对象,为什么Child::doSomething不能简单的返回价值?如果不可能的话,您的代码和描述中似乎缺少一些东西。因此,请read about how to ask good questionsthis question checklist 寻求帮助以改进您的问题。
  • int doSomething();
  • @Someprogrammerdude 在我的例子中,指针是指向 QVector 的指针,并且在将孩子的计算值分配给父值指针时遇到了异常。我将发布异常。
  • 您能发布工作的、可编译的示例代码吗?因为您当前的代码很难遵循。

标签: c++ qt pointers


【解决方案1】:

很简单:

class Child
{
    int m_Data = 0;

public:
    Child(int Data) : m_Data(Data) {};

    int doSomething()
    {
        // Do something ("Child calculation") , for example:
        int a = 0, b = 0, c = 0, d = 0, e = 0, f = m_Data, r = 0;
        r = a * b * c * d * e * f + a * b * c + f + 5;

        return r;
    }
};

用途:

Child Chl(5);

int Result = Chl.doSomething(); // Result is 10

--

如果您更喜欢使用指针: ...您写道:“将值从父级传递给子级作为指针,并使子级计算结果存储在该父级值指针指向的位置。” :

class Child
{
    int * m_pData = nullptr;

public:
    Child(int * pData) : m_pData(pData) {};

    void doSomething()
    {
        // Do something ("Child calculation") , for example:
        int a = 0, b = 0, c = 0, d = 0, e = 0, f = *m_Data, r = 0;
        r = a * b * c * d * e * f + a * b * c + f + 5; 

        *m_pData = r; // "store in the location pointed"
    }
};

用途:

int Data = 5; // "A value from Parent"

Child Chl(&Data); // "To the Child, as a pointer"

Chl.doSomething(); // Data is 10

--

  • 它可以是std::vector 而不是int

【讨论】:

    【解决方案2】:

    我想你想要的是(注意 * 代表 Localvalue):

    class Child
    {
        public:
            Child (int* value) : Localvalue{value};
            int* Localvalue;
            int Resultvalue;
            void doSomething() {
                if (Localvalue != nullptr) 
                    *Localvalue = Resulvalue; 
            }
    };
    

    话虽如此,这是一个糟糕的设置。 child 要求所有者保证传递的指针在 Child 实例生命周期内保持有效。

    您可以使用shared_ptr&lt;int&gt; 对其进行改进。或者按照您的问题的其他建议从doSomething() 返回值。即使您现在返回一个向量,由于返回值优化,应该没有性能问题。

    【讨论】:

    • 我做的第一件事是检查指针是否为空,但不是,我在调试器中看到它的有效地址,它只是在分配过程中失败。
    猜你喜欢
    • 2022-01-08
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 2010-09-20
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多