[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Built-in Functions inside method of a class
From: |
John W. Eaton |
Subject: |
Built-in Functions inside method of a class |
Date: |
Mon, 10 Oct 2011 15:55:09 -0400 |
On 9-Oct-2011, Juan Pablo Carbajal wrote:
| Are built in functions called inside a method of a class considered as
| friends (as in C++) of that class?
| In other words: Will the function be evaluated in the context of the
| caller or in the context of the method.
|
| I have a class with a field 'Path' that is a structure with many
| fields inside. I have a method that is meant to return a cell
| containing information inside those subfields. The following code
| works correctly
|
| wanted_ids = {ids{tf}};
| for i = 1: numel (wanted_ids)
| paths{i} = obj.Path.(ids{i}).data;
| end
|
| by returning the cell with the contents of the subfields of Path
| described in 'wanted_ids'.
|
| However any of the following doesn't work
| % Variation
| paths = cellfun (@(s) obj.Path.(s).data,wanted_ids,'UniformOutput',false);
| % Another variation
| paths = cellfun (@(s) getfield (obj,'Path').(s).data,
| wanted_ids,'UniformOutput',false);
| % Yet another
| paths = cellfun (@(s) getfield (obj.Path,s).data,
| wanted_ids,'UniformOutput',false);
| % Yet yet another
| dummy = @(s) obj.Path.(s).data;
| paths = cellfun (dummy, wanted_ids,'UniformOutput',false);
|
| The error says that the class cannot by indexed with 'Path'. If one
| manually gives this interface to the class, by defining a case in
| subref 'Path' the problem is solved. However, this breaks the privacy
| of the class field 'Path', that is not meant to be accessed directly.
I don't know whether this is a bug or not. Can you please post a
complete example that can be used to reproduce the problem?
| Is there any way of solving this without making Path public?
| Equivalently, can one define functions "friend" of a class?
No, in Matlab classes, there is no concept of friends like in C++.
jwe
- Built-in Functions inside method of a class, Juan Pablo Carbajal, 2011/10/08
- Re: Built-in Functions inside method of a class, Juan Pablo Carbajal, 2011/10/10
- Built-in Functions inside method of a class,
John W. Eaton <=
- Re: Built-in Functions inside method of a class, Carnë Draug, 2011/10/10
- Re: Built-in Functions inside method of a class, Ben Abbott, 2011/10/10
- Re: Built-in Functions inside method of a class, Ben Abbott, 2011/10/10
- Message not available
- Re: Built-in Functions inside method of a class, Carnë Draug, 2011/10/10
- Re: Built-in Functions inside method of a class, Ben Abbott, 2011/10/10
- Re: Built-in Functions inside method of a class, Carnë Draug, 2011/10/10