C++模板,分配“静态”和“静态”的对象的问题和“动态”
这段代码有什么问题?
template <typename T>
class Sample
{
public:
T first;
T second;
typedef T Type;
};
and
template <typename Item>
class Process
{
public:
void process (Item *item)
{
typename Item::Type var = item->first + item->second;
//Some code...
}
};
方法“process”应该能够使用分配了“静态”和“动态”的对象...第一个选项有效,
int main(int argc, _TCHAR* argv[])
{
Sample <double> s;
Process <Sample <double> > a;
a.process(&s);
return 0;
}
但第二个选项无效
int main(int argc, _TCHAR* argv[])
{
Sample <double> *s = new Sample <double>();
Process <Sample <double> *> a;
a.process(s); //Error C2664: 'Process<Item>::process' : cannot convert parameter 1 from 'Sample<T> *' to 'Sample<T> *'
return 0;
}
如何设计一个类和方法“process”以便能够工作一个对象同时分配了“静态”和“动态”?感谢您的帮助...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Process- ::process()
需要一个Item*
,因此Process *>::process()
需要一个Sample**
。然而,解决方案要简单得多:
对于
Process
,对象是如何分配的根本不重要,它得到的只是指向它的指针并使用它(假设它不会维护所有权或尝试删除该对象)。Process <Item>::process()
expects aItem*
, soProcess <Sample <double> *>::process()
expects aSample <double>**
.The solution is much easier, however:
For
Process
, it doesn't matter at all how the object was allocated, all it gets is a pointer to it and works with it (assuming it won't maintain ownership or attempt to delete the object).Process
的参数仍应为Sample
,而不是Sample*
,因为后者使Item*< /代码> =>
Sample**
,调用process
成员函数时不会传递该示例。所以正确的代码应该是这样的:
现在应该可以工作了!
或者这样做:
The argument to
Process
should still beSample<double>
, notSample<double>*
, since the latter makesItem*
=>Sample<double>**
, which you're not passing when callingprocess
member function.So the correct code should be this:
This should work now!
Or do this:
问题不在于对象是以某种方式分配的。至少通过任何可移植的标准方法,无法仅凭其指针来判断对象是在堆栈上分配还是在空闲存储上分配。对于“堆栈”对象和“自由存储”对象,通过指针访问对象成员的方式完全相同。
实际问题是您将错误的类型传递给第二个代码段中的 Process<> 变量定义:
查看
Process<>
的定义:当
Item
类型为Sample 时*
,那么process()
的函数签名就变成:显然,这不是你想要的。
要解决此问题,请更改
为
使用后者,
process()
的函数签名变为void process (Sample* item)
,这应该允许您的要编译的代码片段。The issue isn't the fact that the object was allocated a certain way. It's not possible to tell whether if an object was allocated on the stack or the free store given its pointer alone, at least though any portable, standard means. Accessing members of an object through a pointer happens the exact same way for both "stack" objects and "free store" objects.
The actual issue is that you're passing the wrong type to the
Process<>
variable definition in the second snippet:Take a look at the definition of
Process<>
:When
Item
is of typeSample <double> *
, then the function signature forprocess()
becomes:Which is not what you want, apparently.
To fix this, change
to
With the latter, the function signature for
process()
becomesvoid process (Sample <double>* item)
, which should allow both of your code snippets to compile.上述代码的
Process.process
签名将为void process(Sample **)
因为Item
是Sample <双> *
- 您希望它是void process(Sample *)
将此更改为:
到:
如果您查看完整的错误消息,编译器可能会告诉您有关尝试将一个
Sample
与另一个Sample
匹配时使用两个不同的T
The
Process.process
signature for the above code would bevoid process(Sample <double> **)
sinceItem
isSample <double> *
- You want it to bevoid process(Sample <double> *)
Change this:
To:
If you look at the full error message, likely the compiler is telling you about the two different
T
's used when trying to match oneSample<T>
with anotherSample<T>