模板参数类型构造函数方法调用 [英] Template parameter type constructor method call

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

问题描述

在这种情况下如何编写构造函数mehtod调用


/ * ----------- * /

template< ; typename Tclass CObjectPoolImpl

{

public:


void smth(T * pObj)

{

if(pObj)

pObj-> T :: T(); //试图调用构造函数方法

class T

}


};

CObjectPoolImpl< mynamespace :: CMyTypeCF;

CMyType mt;


CF.smth(& mt);

/ * ----------- * /


MS Visual C ++ 7.1:

错误C2039:''T'' :不是''mynamespace :: CMyType'的成员'


有理由不写''新T'而不是明确写出

构造函数方法名称(类型名称)。

的默认构造函数方法CMyType存在。

How do I write a constructor mehtod call in this case

/*-----------*/
template<typename Tclass CObjectPoolImpl
{
public:

void smth(T* pObj)
{
if (pObj)
pObj->T::T(); // an attempt to call a constructor method of
class T
}

};

CObjectPoolImpl<mynamespace::CMyTypeCF;
CMyType mt;

CF.smth(&mt);
/*-----------*/

MS Visual C++ 7.1:
error C2039: ''T'' : is not a member of ''mynamespace::CMyType''

There is a reason not to write ''new T'' and not to explicitly write
constructor method name (type name). Default constructor method for
CMyType exists.

推荐答案

AlexanderVX写道:
AlexanderVX wrote:

在这种情况下如何编写构造函数mehtod调用
How do I write a constructor mehtod call in this case



没有构造函数方法调用在C ++中。什么是你想要完成的

There is no "constructor method call" in C++. What is it you''re
trying to accomplish?


>

/ * --- -------- * /

模板< typename Tclass CObjectPoolImpl

{

public:


void smth(T * pObj)

{

if(pObj)

pObj-> T :: T() ; //试图调用构造函数方法

class T
>
/*-----------*/
template<typename Tclass CObjectPoolImpl
{
public:

void smth(T* pObj)
{
if (pObj)
pObj->T::T(); // an attempt to call a constructor method of
class T



我在这里猜测,但你可能想做


new(pObj)T();

I am guessing here, but you might want to do

new (pObj) T();


}


};


CObjectPoolImpl< mynamespace :: CMyTypeCF;

CMyType mt;


CF.smth(& mt);
}

};

CObjectPoolImpl<mynamespace::CMyTypeCF;
CMyType mt;

CF.smth(&mt);



这很可能会导致构造函数被调用两次,因为

相同的对象。这有不确定的行为,AFAICT。为什么你认为你需要它?
? ''mt''已经是一个完全构造的对象。

This will very likely cause the constructor to be called twice for the
same object. That has undefined behaviour, AFAICT. Why do you think
you need it? ''mt'' is already a fully constructed object.


/ * ----------- * /

MS Visual C ++ 7.1:

错误C2039:''T'':不是'mynamespace :: CMyType'的成员'


有理由不写''new T'而不是明确写

构造函数方法名(类型名)。

的默认构造函数方法存在CMyType。
/*-----------*/

MS Visual C++ 7.1:
error C2039: ''T'' : is not a member of ''mynamespace::CMyType''

There is a reason not to write ''new T'' and not to explicitly write
constructor method name (type name). Default constructor method for
CMyType exists.



了解新位置。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

Read about "placement new".

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


AlexanderVX写道:
AlexanderVX wrote:

如果在这种情况下如何编写构造函数mehtod调用


/ * ----------- * /

模板< typename Tclass CObjectPoolImpl

{

public:


void smth(T * pObj)

{

if(pObj)

pObj-> T :: T(); //尝试调用类T
的构造函数方法
How do I write a constructor mehtod call in this case

/*-----------*/
template<typename Tclass CObjectPoolImpl
{
public:

void smth(T* pObj)
{
if (pObj)
pObj->T::T(); // an attempt to call a constructor method of class T



new((void *)pObj)T();


或:


std :: allocator< T>()。construct(pObj,T());

new ( (void*) pObj ) T ();

or:

std::allocator<T>().construct( pObj, T() );


}


};


CObjectPoolImpl< mynamespace :: CMyTypeCF;

CMyType mt;


CF.smth(& mt);

/ * ----------- * /


MS Visual C ++ 7.1:

错误C2039:''T'':不是'mynamespace :: CMyType'的成员'


那里是不写'新T的原因而不是明确写出

构造函数方法名(类型名称)。

的默认构造函数方法存在CMyType。
}

};

CObjectPoolImpl<mynamespace::CMyTypeCF;
CMyType mt;

CF.smth(&mt);
/*-----------*/

MS Visual C++ 7.1:
error C2039: ''T'' : is not a member of ''mynamespace::CMyType''

There is a reason not to write ''new T'' and not to explicitly write
constructor method name (type name). Default constructor method for
CMyType exists.



注意:可能,有人会告诉你,你不能调用构造函数和

你可以做的就是让实现调用/调用它。

这个口号是指三个事实:(a)构造函数调用不是函数

调用,(b)标准,有点隐秘,表示构造函数

没有名字(因此通过名称查找找不到它们),而且(c)

标准在谈论被调用的构造函数时使用被动语态。


就个人而言,我发现通过采用语言许可来强调

函数调用和构造函数调用之间的区别来说很困惑

说我们调用函数但没有获得许可证说我们称之为

构造函数。 (你在任何一种情况下都要获得语言许可,因为

标准在谈论一个名为

的函数时也使用被动语态:标准只是不关心什么程序员另外,

a函数调用根据标准只是某种形式的

表达式:你要做的就是在程序中编写这样的表达式并且

导致实现在

评估表达式的过程中调用函数。)

Best


Kai-Uwe Bux

Note: probably, someonewill tell you that you cannot call a constructor and
that all that you can do is cause the implementation to invoke/call it.
That slogan refers to three facts: (a) constructor calls are not function
calls, (b) the standard, somewhat cryptically, states that constructors
have no names (hence they are not found through name lookup), and (c) the
standard uses the passive voice in talking about constructors being called.

Personally, I find it rather confusing to underscore the difference between
functions calls and constructor calls by taking the linguistic license to
say that we call functions but not taking the license to say that we call
constructors. (You take a linguistic license in either case since the
standard also uses the passive voice when talking about a function being
called: the standard is just not concerned with what programmers do. Also,
a function call according to the standard is just a certain form of
expression: all you do is to write such an expression in your program and
that causes the implementation to invoke the function in the course of
evaluating the expression.)
Best

Kai-Uwe Bux


这很可能会导致构造函数被调用两次
This will very likely cause the constructor to be called twice for the

相同宾语。这有不确定的行为,AFAICT。为什么你认为你需要它?
? ''mt''已经是一个完全构造的对象。
same object. That has undefined behaviour, AFAICT. Why do you think
you need it? ''mt'' is already a fully constructed object.



你是对的。这只是一个样本。问题不在于

它...

You are right. This is just a sample. And the question was not about
it...


阅读placement new。
Read about "placement new".



我知道,但这种解决方案并不是我的首要任务......

I knew, but this kind of solution was not on the top of my head...

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

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