重载 opIndexAssign
我似乎在我的一个类中重载 opIndexAssign 时遇到了一些问题。
我有课; JSObject 的定义如下:
alias char[] String;
...
class JSObject : Dobject
{
/*****************************************************************
* Constructors
******************************************************************/
this( Dobject dobj )
{
super( dobj ) ;
}
this()
{
super( null ) ;
}
this( AssociativeArray data )
{
// initiate
super( null ) ;
// then populate
foreach( k, v ; data )
{
this[ k ] = v ;
}
}
public void opIndexAssign( String key , String val )
{
Value* v = new Value() ;
v.putVstring( val ) ;
this.Put(key, v , DontDelete);
}
public void opIndexAssign( String key , Dobject dobj )
{
Value* v = new Value() ;
v.putVobject( dobj ) ;
this.Put(key, v , DontDelete);
}
public void opIndexAssign( String key , JSObject jso )
{
Value* v = new Value() ;
v.putVobject( jso ) ;
this.Put(key, v , DontDelete);
}
public Value* opIndex( String key )
{
return this.Get( key );
}
}
Dobject 超类重载了 put() 和 get() 方法,我试图包装它们,以便可以将它们作为关联数组访问:
77: JSObject jso = new JSObject() ;
78: jso[ "foo" ] = "bar" ;
79:
80: JSObject jsoParent = new JSObject() ;
81: jsoParent[ "child" ] = jso ;
它适用于 String,String 方法,但是当我尝试使用 JSObject 作为值,但失败了。
test2.d => test2
+ c:\dmd\dsss\bin\rebuild.exe -version=PhobosCompatibility -w -Idsss_imports\ -I. -S.\ -Ic:\dmd\dsss\include\d -Sc:\dmd\dsss\lib\ -Ic:\dmd\dsss\include\d -Sc:\dmd\dsss\lib -oqdsss_objs\D -debug -gc test2.d -oftest2
test2.d(81): Error: function dmdscripttest.JSObject.opIndexAssign (char[],char[]) does not match parameter types (JSObject,char[5u])
test2.d(81): Error: cannot implicitly convert expression (jso) of type dmdscripttest.JSObject to char[]
test2.d(81): Error: cannot implicitly convert expression ("child") of type char[5u] to dmdscripttest.JSObject
Error: Command failed, aborting.
Command c:\dmd\dsss\bin\rebuild.exe returned with code 1, aborting.
我对自己做错的事感到有点茫然。这就像编译器尝试将其转换为适合 opIndexAssign( String, String ) 而不是 opIndexAssign( String, JSObject ) 方法。
我是否错误地定义了 opIndexAssign 函数?
提前致谢,
I seem to have some trouble overloading opIndexAssign in one of my classes.
I have a class; JSObject which is defined like this:
alias char[] String;
...
class JSObject : Dobject
{
/*****************************************************************
* Constructors
******************************************************************/
this( Dobject dobj )
{
super( dobj ) ;
}
this()
{
super( null ) ;
}
this( AssociativeArray data )
{
// initiate
super( null ) ;
// then populate
foreach( k, v ; data )
{
this[ k ] = v ;
}
}
public void opIndexAssign( String key , String val )
{
Value* v = new Value() ;
v.putVstring( val ) ;
this.Put(key, v , DontDelete);
}
public void opIndexAssign( String key , Dobject dobj )
{
Value* v = new Value() ;
v.putVobject( dobj ) ;
this.Put(key, v , DontDelete);
}
public void opIndexAssign( String key , JSObject jso )
{
Value* v = new Value() ;
v.putVobject( jso ) ;
this.Put(key, v , DontDelete);
}
public Value* opIndex( String key )
{
return this.Get( key );
}
}
The Dobject superclass has overloaded put() and get() methods and I'm trying to wrap them so I can access them as associative arrays:
77: JSObject jso = new JSObject() ;
78: jso[ "foo" ] = "bar" ;
79:
80: JSObject jsoParent = new JSObject() ;
81: jsoParent[ "child" ] = jso ;
It works for the String,String method but when I try using the JSObject as the value, it fails.
test2.d => test2
+ c:\dmd\dsss\bin\rebuild.exe -version=PhobosCompatibility -w -Idsss_imports\ -I. -S.\ -Ic:\dmd\dsss\include\d -Sc:\dmd\dsss\lib\ -Ic:\dmd\dsss\include\d -Sc:\dmd\dsss\lib -oqdsss_objs\D -debug -gc test2.d -oftest2
test2.d(81): Error: function dmdscripttest.JSObject.opIndexAssign (char[],char[]) does not match parameter types (JSObject,char[5u])
test2.d(81): Error: cannot implicitly convert expression (jso) of type dmdscripttest.JSObject to char[]
test2.d(81): Error: cannot implicitly convert expression ("child") of type char[5u] to dmdscripttest.JSObject
Error: Command failed, aborting.
Command c:\dmd\dsss\bin\rebuild.exe returned with code 1, aborting.
I'm a bit at loss to what I'm doing wrong. It's like the compiler tries to cast it to fit with opIndexAssign( String, String ) instead of the opIndexAssign( String, JSObject ) method.
Did I define the opIndexAssign functions incorrectly?
Thanks in advance,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是 opIndexAssigne 首先需要值,然后是键(或索引)
http: //www.d-programming-language.org/operatoroverloading.html#Assignment
所以你需要定义它,因为
这样做的原因是这样你可以为索引定义一个可变参数
the issue is that opIndexAssigne needs the value first and then the keys (or indices)
http://www.d-programming-language.org/operatoroverloading.html#Assignment
so you'll want to define it as
the reason this is done is so that you can define a vararg for the index