构造函数中的模板参数 [英] Template parameters in constructor

查看:91
本文介绍了构造函数中的模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个''Command''类,其构造函数接受一个

模板参数T的实例。然后构造函数打印出
$ b $的名称b传递给它的T类。当我提前构建T类实例化Command时,它可以工作。当我在*

命令构造函数中构造T类*时,它不起作用。任何想法?


================代码如下===============


#include< iostream>


class命令

{

public:


模板< class T>显性命令(T& t)

{

std :: cout<< typeid(T).name()<< std :: endl;

}

};


class Bar {};


int main()

{

Bar b;

命令cmd(b); //这有效

命令cmd2(Bar()); //这不是


返回0;

}

I''ve got a ''Command'' class whose constructor takes an instance of a
template argument T. The constructor then prints out the name of the
class T that was passed to it. When I construct the T class in advance
of instantiating Command, it works. When I construct the T class *in*
the Command constructor, it doesn''t work. Any ideas?

================ CODE FOLLOWS ===============

#include <iostream>

class Command
{
public:

template<class T> explicit Command(T & t)
{
std::cout << typeid(T).name() << std::endl;
}
};

class Bar { };

int main()
{
Bar b;
Command cmd(b); // This works
Command cmd2(Bar()); // This doesn''t

return 0;
}

推荐答案

mrstephengross写道:
mrstephengross wrote:
我有一个''Command''类,其构造函数接受一个
模板参数T的实例。然后构造函数打印出来传递给它的
类T的名称。当我提前构建T类实例化Command时,它可以工作。当我在*
命令构造函数中构造T类*时,它不起作用。任何想法?

================代码如下===============

#include< iostream>

类命令
{
公开:

模板<类T>显性命令(T& t)
{
std :: cout<< typeid(T).name()<< std :: endl;
}
};

类Bar {};

int main()
{
栏b;
命令cmd(b); //这有效
命令cmd2(Bar()); //这不是


这是一个函数的声明。在档案中阅读它。

这对于初学者来说是一个众所周知的陷阱,有时奇怪的是,对于经验丰富的C ++程序员来说,
适用。

返回0;
}
I''ve got a ''Command'' class whose constructor takes an instance of a
template argument T. The constructor then prints out the name of the
class T that was passed to it. When I construct the T class in advance
of instantiating Command, it works. When I construct the T class *in*
the Command constructor, it doesn''t work. Any ideas?

================ CODE FOLLOWS ===============

#include <iostream>

class Command
{
public:

template<class T> explicit Command(T & t)
{
std::cout << typeid(T).name() << std::endl;
}
};

class Bar { };

int main()
{
Bar b;
Command cmd(b); // This works
Command cmd2(Bar()); // This doesn''t
It''s a declaration of a function. Read about it in the archives.
It''s a well-known trap for beginners, which curiously enough sometimes
works for seasoned C++ programmers.

return 0;
}




V



V


有趣;我会查一下......有没有正确的语法用于实例化我想要调用的那种实际的语法?


- 史蒂夫

Interesting; I''ll look it up... Is there a correct syntax for the kind
of instantiation I''m trying to invoke?

--Steve


2005年6月17日星期五18:15:56 + 0400,mrstephengross

< mr **** ********@hotmail.com>写道:
On Fri, 17 Jun 2005 18:15:56 +0400, mrstephengross
<mr************@hotmail.com> wrote:
我有一个''Command''类,其构造函数接受一个
模板参数T的实例。构造函数然后打印出的名称
传递给它的T类。当我提前构建T类实例化Command时,它可以工作。当我在*
命令构造函数中构造T类*时,它不起作用。有任何想法吗?


请清楚说明它的含义它的工作原理和id不起作用。如果

您的编译器产生错误消息,请向他们提出您的问题。

================代码跟踪==== ===========

#include< iostream>

课程命令
{
公开:

模板< class T>显性命令(T& t)
{
std :: cout<< typeid(T).name()<< std :: endl;
}
};

类Bar {};

int main()
{
栏b;
命令cmd(b); //这有效
命令cmd2(Bar()); //这不会返回0;
}
I''ve got a ''Command'' class whose constructor takes an instance of a
template argument T. The constructor then prints out the name of the
class T that was passed to it. When I construct the T class in advance
of instantiating Command, it works. When I construct the T class *in*
the Command constructor, it doesn''t work. Any ideas?
Please state clearly what it means "it works" and "id does not work". If
your compiler produces error messages provide them with your question.
================ CODE FOLLOWS ===============

#include <iostream>

class Command
{
public:

template<class T> explicit Command(T & t)
{
std::cout << typeid(T).name() << std::endl;
}
};

class Bar { };

int main()
{
Bar b;
Command cmd(b); // This works
Command cmd2(Bar()); // This doesn''t

return 0;
}




在这不会中你创建一个临时对象的情况。暂时的

对象是一个r值。 R值不能绑定到对

非const的引用。要修复错误,你的模板化构造函数接受

T by value(T t)或引用const(T const& t)。

-

Maxim Yegorushkin



In the "This doesn''t" case you create a temporary object. A temporary
object is an r-value. R-value can not be bound to a reference to a
non-const. To fix the error make your templated constructor accept either
T by value (T t) or by reference to const (T const& t).
--
Maxim Yegorushkin


这篇关于构造函数中的模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆