从var转换为var&

时间:2012-12-08 11:49:14

标签: c++

如何将varvar*类型的变量转换为var&

我要使用一个带有var类对象的函数(假设有一个类)。示例代码如下: -

testFunc(false, namespace1::namespace2::var(), 100);

在函数声明中它表示第二个参数是namespace1::namespace2::var&类型,我可以创建namespace1::namespace2::varnamespace1::namespace2::var*,但是如何创建namespace1::namespace2::var&

我知道这是一个非常基本的问题,但我无法理解。

修改
我曾尝试使用var,但它会出现一些奇怪的错误。我很确定在我使用的功能中存在某种错误。这是错误: -

Error   3   error C2825: 'CType': must be a class or namespace when followed by '::'
Error   4   error C2039: 'TypeCode' : is not a member of '`global namespace''
Error   5   error C2146: syntax error : missing ',' before identifier 'TypeCode'
Error   6   error C2065: 'TypeCode' : undeclared identifier 
Error   7   error C3203: 'CustomType' : unspecialized class template can't be used as a template argument for template parameter 'Base', expected a real type   

修改2

我认为如果我包含真实代码就很难回答,因为它很复杂。但看看它是否有帮助。真实函数的签名是这样的: -

virtual bool opRaiseEvent(bool reliable, const Common::Hashtable& parameters, nByte eventCode, nByte channelID=0, int* targetPlayers=NULL, short numTargetPlayers=NULL);

,示例代码使用了这样的函数: -

mLoadBalancingClient.opRaiseEvent(false, ExitGames::Common::Hashtable(), 100);

工作正常。但是现在我想将数据添加到HashTable中,所以我需要创建一个对象,然后将其传递给函数。它不接受指针或正常变量。我不知道为什么它只使用HashTable()。

3 个答案:

答案 0 :(得分:3)

这意味着第二个参数由 reference 传递。你必须简单地通过:

namespace1::namespace2::var

答案 1 :(得分:1)

这应该有效:

const Common::Hashtable param = namespace1::namespace2::var();
opRaiseEvent(false, param, 100);

答案 2 :(得分:1)

namespace1::namespace2::var v;
testFunc(false, v, 100);