Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Accessing an object's private integer array.

Hello everyone. So I'm working with a class that has a private member variable that is an integer array of size 4. Here is the code for it:
http://pastebin.com/i9u5DtUi - MoveRate.h
http://pastebin.com/q1xd61VQ - MoveRate.cpp

Now if one looks at the code they can execute the following snippets:
PHP Code

<?php 
//The declaration of the object in the private field of a header
private:
MoveRate m_moveRate;

//A getter to access the private object
MoveRate DirectionSim::getMoveRate(){
    return 
m_moveRate;
}

//Finally, a snippet to actually modify the private integer array.
getMoveRate().setAMoveRate(2, 0);
cout << "COMP: "<< getMoveRate().getAMoveRate()[0] << endl; 

Now, looking at the MoveRate.cpp, I have placed debug code in the setter for the A array. Running this program should give the following output:
Code
VAL: 2 ACT: 2
COMP: 2

Instead, I get this:
Code
VAL: 2 ACT: 2
COMP: 473122

Can anyone explain this behavior? Thanks.
White Hat Since 7/27/12
Make your own Bukkit Plugins
/1e3rOg4]Part 1
/1jCHIA4]Part 2


You release that you have to create the object and save the instance in a variable to access it later right?
(Sep 27, 2015 07:18 AM)bbbysmth Wrote:  You release that you have to create the object and save the instance in a variable to access it later right?

The object created was m_MoveRate. It is stored in the variable m_MoveRateA inside of the m_MoveRate object.


I managed to fix the error. for anyone else looking for the solution, you need to make the array dynamic instead.

so in the constructor do:
Code
m_moveRateA = new int[ARRAY_SIZE];
White Hat Since 7/27/12
Make your own Bukkit Plugins
/1e3rOg4]Part 1
/1jCHIA4]Part 2




Possibly Related Threads...
Thread / Author Replies Views Last Post
by iconiaAug 17, 2026 02:46 PM 3 91 Aug 17, 2026 02:46 PM
Last Post: iconia
[C] by Equivalent_Jaguar198Jun 13, 2022 02:20 PM 4 188 Jun 13, 2022 02:20 PM
Last Post: Equivalent_Jaguar198
[C] by gurlhackerclubMay 19, 2020 05:18 PM 8 260 May 19, 2020 05:18 PM
Last Post: k1r3n