【发布时间】: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 questions 和this question checklist 寻求帮助以改进您的问题。 -
int doSomething(); -
@Someprogrammerdude 在我的例子中,指针是指向 QVector 的指针,并且在将孩子的计算值分配给父值指针时遇到了异常。我将发布异常。
-
您能发布工作的、可编译的示例代码吗?因为您当前的代码很难遵循。