English 中文(简体)
具有超负荷指数参照数的班级获得方法和特性
原标题:Access to methods and properties for classes with overloaded index reference

我想通过<代码>()和{><>>>>/code>超载我的MATLAB课程之一。 可通过实施>>subsref,对该类别进行罚款。

However, I also want to keep the default behaviour of indexing via . (i.e. access to methods and properties) for this class. I tried using the following code:

function x = subsref(this, S)
    if strcmp(S(1).type,  {} )
        % My own implementation here
    elseif strcmp(S(1).type,  () )
        % My own implementation here
    elseif strcmp(S(1).type,  . )
        x = builtin( subsref , this, S);
    else
        error( Unknown index variant. );
end

然而,这并不奏效,因为我不知道要求<代码>builtin(分点,即S)的多少产出论点会回来。 尤其是,我的班子的一种方法可能回去了一些不同的论点,其产出论点的数量取决于其投入论点的数量和价值。 A MATLAB example for subsref (中间环节)说明:

当你执行<条码> 某类方法,你必须明确执行所有附则提及[......]

这是否真正是唯一可能的解决办法? 对于复杂的方法,这基本上意味着重复许多逻辑。 就我而言,这无疑是无法维持的(有多种方法和特性的大型班级)。

注:我知道,这都是关于合成糖,我总是能够使用标准方法电话,而不是超载({}

最佳回答

For a partial fix, I think you can use nargout to defer to the caller, which will usually be in a position to know. Any function that s capturing multiple outputs must know at the time of the call how many it s capturing.

这给你带来了多处压力,但是,只要你重新掌握回归的价值观,就应该像大多数法典一样,工作。

function varargout = subsref(this, S)
   switch S(1).type
      ...
      case  . 
         varargout = cell(1, nargout);
         if nargout == 0
             builtin( subsref , this, S);
         else
             [varargout{:}] = builtin( subsref , this, S);
         end
   end
end

硬墙强制规定:该法典没有经过测试,在底部/底部内有一根fin子。

问题回答

暂无回答




相关问题
C++ Overloading the >> operator

I need to overload the stream extraction operator. I need to do this by allowing a user to input a string of characters at a prompt, say "iamastring", and then the operator would extract each ...

What on earth would compell C++ to call this function?

I m working on a programming language that uses C++ as it s target language for now. I m hitting an exceptionally strange backtrace. #1 0x08048d09 in factorial (n=0x8052160) at ir.cpp:35 35 ...

Const correctness in C++ operator overloading returns

I m a little confused as to why I ve been told to return const foo from a binary operator in c++ instead of just foo. I ve been reading Bruce Eckel s "Thinking in C++", and in the chapter on operator ...

C# Extend array type to overload operators

I d like to create my own class extending array of ints. Is that possible? What I need is array of ints that can be added by "+" operator to another array (each element added to each), and compared by ...

What is ->* operator in C++?

C++ continues to surprise me. Today i found out about the ->* operator. It is overloadable but i have no idea how to invoke it. I manage to overload it in my class but i have no clue how to call it. ...

Overloading Insertion Operator in C++

I have a class in which I m trying to overload the << operator. For some reason, it is not being overloaded. Here is my .h file: friend std::ostream& operator<<(std::ostream&, ...

How would you overload the [] operator in javascript

I can t seem to find the way to overload the [] operator in javascript. Anyone out there know? I was thinking on the lines of ... MyClass.operator.lookup(index) { return myArray[index]; } or am ...

热门标签