疯狂的本地班 [英] Crazy Local Class
问题描述
我在函数模板中有一个本地类。我可以在我的本地课程中包装
任何类型。它是合法的C ++吗?什么类型的课程
本地?是班级模板还是普通班级?
提前致谢。
- 日期
---------- -------------------------------------------------- -------------------------------------------------- -------------------------------
class Foo
{
public:
virtual~Foo(){}
virtual void print()= 0;
} ;
模板< typename T>
Foo * wrap(const T& t)
{
class Local:public Foo
{
public:
Local(const T& t):val_(t){}
void print()
{
cout<< In Local :: print() << val_<<结束;
}
私人:
T val_;
};
返回新的本地(t);
}
int main(无效)
{
int x = 50;
Foo * ptr = wrap(x);
ptr-> print();
std :: string s(wrap);
ptr = wrap(s);
ptr-> print();
}
-
[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]
[comp.lang.c ++。moderated。第一次海报:做到这一点! ]
Hi,
I have a local class inside a function template. I am able to wrap
any type in my local class. Is it legal C++? What type of class is
Local? Is it a class template or regular class?
Thanks in advance.
--dhina
---------------------------------------------------------------------------------------------------------------------------------------------
class Foo
{
public:
virtual ~Foo() {}
virtual void print() = 0;
};
template< typename T >
Foo* wrap(const T& t)
{
class Local : public Foo
{
public:
Local(const T& t):val_(t) { }
void print()
{
cout << "In Local::print()" << val_ << endl;
}
private:
T val_;
};
return new Local(t);
}
int main(void)
{
int x = 50;
Foo* ptr = wrap(x);
ptr->print();
std::string s("wrap");
ptr = wrap(s);
ptr->print();
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
推荐答案
Local是一个非模板类。本地课程可以改善
符号的位置。类的类型可以用作模板参数和
因此它不是完全隐藏的,因为它是函数的本地。
Fraser。
Local is a non-template class. A local class can improve locality of
symbols. The type of the class can be used as a template argument and
so it is not entirely hidden because its local to a function.
Fraser.
10月23日,3:36 * pm,Fraser Ross < z ... @ zzzzzz.comwrote:
On Oct 23, 3:36*pm, "Fraser Ross" <z...@zzzzzz.comwrote:
Local是非模板类。 *本地课程可以改善符号的b
位置。 *类的类型可以用作
模板参数,因此它不是完全隐藏的,因为它的函数是本地的
。
Local is a non-template class. *A local class can improve
locality of symbols. *The type of the class can be used as a
template argument and so it is not entirely hidden because its
local to a function.
不确定我理解你想说的是什么,但是本地
类不能用作模板参数。
-
James Kanze(GABI软件)电子邮件:ja ********* @ gmail.com
Conseils更多信息,请联系我们b $ b beratung in objektorientierter Datenverarbeitung
9placeSémard,78210 St.-Cyr-l''cocole,France,+ 33(0)1 30 23 00 34
Not sure I understand what you are trying to say, but a local
class cannot be used as a template argument.
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34
" James Kanze"
"James Kanze"
>不确定我明白你想说的是什么,但本地
类不能用作模板参数。
>Not sure I understand what you are trying to say, but a local
class cannot be used as a template argument.
它在14.3.1 / 2的选秀中。
Fraser。
Its in the draft at 14.3.1/2.
Fraser.
这篇关于疯狂的本地班的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!