C++模板,分配“静态”和“静态”的对象的问题和“动态”

发布于 2024-10-29 00:57:59 字数 950 浏览 1 评论 0 原文

这段代码有什么问题?

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”以便能够工作一个对象同时分配了“静态”和“动态”?感谢您的帮助...

What is wrong in this code?

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...
    }
};

The method "process" should be able to work with an object allocated both "static" and "dynamic"...The first option works

int main(int argc, _TCHAR* argv[])
{
Sample <double> s;
Process <Sample <double> > a;
a.process(&s);

return 0;
}

but the second not

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;
}

How to design a class and method "process" so as to be able to work with an object allocated both "static" and "dynamic" ? Thanks for your help...

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

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

发布评论

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

评论(4

韬韬不绝 2024-11-05 00:57:59

Process ::process() 需要一个 Item*,因此 Process *>::process() 需要一个 Sample **

然而,解决方案要简单得多:

Sample <double> *s = new Sample <double>();
Process <Sample <double> > a;
a.process(s);

对于Process,对象是如何分配的根本不重要,它得到的只是指向它的指针并使用它(假设它不会维护所有权或尝试删除该对象)。

Process <Item>::process() expects a Item*, so Process <Sample <double> *>::process() expects a Sample <double>**.

The solution is much easier, however:

Sample <double> *s = new Sample <double>();
Process <Sample <double> > a;
a.process(s);

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).

总攻大人 2024-11-05 00:57:59
Sample <double> *s = new Sample <double>();
Process <Sample <double> *> a; //<<----------- here you go wrong!
a.process(s); 

Process 的参数仍应为 Sample,而不是 Sample*,因为后者使 Item*< /代码> => Sample**,调用 process 成员函数时不会传递该示例。

所以正确的代码应该是这样的:

Process <Sample<double> > a; //<<----------- now it's correct!
a.process(s);

现在应该可以工作了!

或者这样做:

Sample <double> *s = new Sample <double>();
Process <Sample <double> *> a; //<------ your version!
a.process( &s );                 //<------ note I'm passing pointer to pointer!
       // ^^ note the ampersand (&)
Sample <double> *s = new Sample <double>();
Process <Sample <double> *> a; //<<----------- here you go wrong!
a.process(s); 

The argument to Process should still be Sample<double>, not Sample<double>*, since the latter makes Item* => Sample<double>**, which you're not passing when calling process member function.

So the correct code should be this:

Process <Sample<double> > a; //<<----------- now it's correct!
a.process(s);

This should work now!

Or do this:

Sample <double> *s = new Sample <double>();
Process <Sample <double> *> a; //<------ your version!
a.process( &s );                 //<------ note I'm passing pointer to pointer!
       // ^^ note the ampersand (&)
方圜几里 2024-11-05 00:57:59

问题不在于对象是以某种方式分配的。至少通过任何可移植的标准方法,无法仅凭其指针来判断对象是在堆栈上分配还是在空闲存储上分配。对于“堆栈”对象和“自由存储”对象,通过指针访问对象成员的方式完全相同。

实际问题是您将错误的类型传递给第二个代码段中的 Process<> 变量定义:

Process <Sample <double> *> a;

查看 Process<> 的定义:

template <typename Item>
class Process
{
public:
    void process (Item *item)
    { /* ... */ }
};

Item 类型为 Sample 时*,那么 process() 的函数签名就变成:

void process (Sample <double>** item)
{ /* ... */ }

显然,这不是你想要的。

要解决此问题,请更改

Process <Sample <double> *> a;

Process <Sample <double> > a;

使用后者,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:

Process <Sample <double> *> a;

Take a look at the definition of Process<>:

template <typename Item>
class Process
{
public:
    void process (Item *item)
    { /* ... */ }
};

When Item is of type Sample <double> *, then the function signature for process() becomes:

void process (Sample <double>** item)
{ /* ... */ }

Which is not what you want, apparently.

To fix this, change

Process <Sample <double> *> a;

to

Process <Sample <double> > a;

With the latter, the function signature for process() becomes void process (Sample <double>* item), which should allow both of your code snippets to compile.

最近可好 2024-11-05 00:57:59
Process <Sample <double> *> a;
a.process(s);

上述代码的 Process.process 签名将为 void process(Sample **) 因为 ItemSample <双> * - 您希望它是 void process(Sample *)

将此更改为:

Process <Sample <double> *> a;

到:

Process <Sample <double> > a;

如果您查看完整的错误消息,编译器可能会告诉您有关尝试将一个 Sample 与另一个 Sample 匹配时使用两个不同的 T

Process <Sample <double> *> a;
a.process(s);

The Process.process signature for the above code would be void process(Sample <double> **) since Item is Sample <double> * - You want it to be void process(Sample <double> *)

Change this:

Process <Sample <double> *> a;

To:

Process <Sample <double> > a;

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 one Sample<T> with another Sample<T>

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