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
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With