Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Accessing an array-class member

12 views
Skip to first unread message

JiiPee

unread,
Jan 12, 2017, 2:17:42 PM1/12/17
to
This is a bit relating to my previous post, because this is the problem
am having in my project.

class MyArray

{

public:

int get(int index) {

}

private:

int array[10];

};


Now I can get an array element:

MyArray arr;

int second = arr.get(1); // return the second element

But I want this to be as fast operation as possible. The problem is that
this inline code seems to do it something like (after adding inline code
into place):

MyArray arr;

int index{1};

int second = array[index];

, where array is arr's array. But it has this extra temporary variable
int index{1}; . So how can I get rid of that temporary?

I know I can do:

class MyArray

{

public:

int* getArray() {

return array;

}

private:

int array[10];

};


and call it:

MyArray arr;

int second = arr.getArray()[1];

but that gets a bit ugly... (and this is what I am currently actually doing)


JiiPee

unread,
Jan 12, 2017, 6:15:13 PM1/12/17
to
On 12/01/2017 22:30, Stefan Ram wrote:
> r...@zedat.fu-berlin.de (Stefan Ram) writes:
>> r...@zedat.fu-berlin.de (Stefan Ram) writes:
>>> Con.4: Use const to define objects with values that do not
>>> change after construction
>> If you prefer Scott Meyers:
>> »Use const whenever possible.« -- SCOTT MEYERS
> Sorry to followup on this one more time!
>
> I'd like to put it this way:
>
> "Use const whenever possible.''
>
> SCOTT MEYERS
>
> "Guideline: Con.4: Use const to define objects with
> values that do not change after construction"
>
>

sure

JiiPee

unread,
Jan 12, 2017, 6:15:50 PM1/12/17
to
On 12/01/2017 21:52, Stefan Ram wrote:
> JiiPee <n...@notvalid.com> writes:
>> , where array is arr's array. But it has this extra temporary variable
>> int index{1}; . So how can I get rid of that temporary?
> Don't worry. The optimizer can deal with such minutiae.
>
>

Are you 100% sure? it did not deal with it when I used an object
(instead of and index)

JiiPee

unread,
Jan 12, 2017, 6:30:15 PM1/12/17
to
On 12/01/2017 23:23, Stefan Ram wrote:
>> it did not deal with it when I used an object (instead of and
>> index)
> How can you know whether this really runs slower?


I debugged the code line by line (in release mode). And it did create a
temporary s in inline function.

0 new messages