Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

possible to overload function in matlab class?

Is it possible to overload a function in a Matlab class that you've created?

Like the following:

    function [ sigma_nc ] = sustained_interference( N )
       sustained_interference( N, N.center_freq);
    end

    function [ sigma_nc ] = sustained_interference( N, center_freq )
       ...
    end

Unfortunately when I try this, I get a redefinition error

like image 829
gnychis Avatar asked Nov 10 '11 22:11

gnychis


People also ask

Is it possible to overload a function?

Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading.

Can const function be overloaded?

C++ allows member methods to be overloaded on the basis of const type. Overloading on the basis of const type can be useful when a function return reference or pointer. We can make one function const, that returns a const reference or const pointer, other non-const function, that returns non-const reference or pointer.

What are the requirements to overload a function?

The only other requirement for successfully overloading functions is that each overloaded function must have a unique parameter list. The function return types may be the same or they may be different, but even if the return types are different, the functions must still have distinct parameter lists.

What are the scenarios in which we can overload the function?

If any class has multiple functions with different parameters having the same name, they are said to be overloaded. If we have to perform a single operation with different numbers or types of arguments, we need to overload the function. In OOP, function overloading is known as a function of polymorphism.


1 Answers

If you create the function using the latter, then you can pass it just a single parameter which will be interpreted as the first. If you want default values, then you can do something like this:

function [ sigma_nc ] = sustained_interference( N, center_freq )
   if nargin < 2
       center_freq = N.center_freq;
   end
   ...
end
like image 196
PengOne Avatar answered Sep 27 '22 21:09

PengOne