what's the matter with using private via methods in this case?

Jan 22, 2014 at 1:28pm
I wrote a derived class, see the code first plz :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class baseclass
{
...
int **body;//which will be dynamic allocated in the method definition
public:
...
int ** getbody(){return body; }
}

class derivedclass: public baseclass
{
public:
update();
}
derivedclass::update()
{
getbody()[0][1] = 10;//the problem occur here!
...
}


why I can't do like this?
Jan 22, 2014 at 1:41pm
line 17 makes no sense with how you've defined getbody().
Jan 22, 2014 at 1:55pm
@q1670741824:

The whole point of a private member is that nothing can access it except the class it's a member of.

If you want derived classes to be able to access it, declare it as protected, not private. Or write accessor methods.

Dammit, didn't read the OP's code properly. Nevermind.

@mutexe:

Isn't that line 17 equivalent to:

1
2
int **tmpPtr = getbody();
tmpPtr[0][1] = 10;


?

Or am I missing something?
Last edited on Jan 22, 2014 at 1:56pm
Jan 22, 2014 at 2:04pm
you're probably right MikeyBoy.
my brain is completely fried today.
apologies OP.
Jan 22, 2014 at 2:16pm
q1670741824 wrote:
why I can't do like this?
What do you mean? You can:
http://ideone.com/ieyL1e
Last edited on Jan 22, 2014 at 2:16pm
Jan 22, 2014 at 2:22pm
@ OP: What's the error you are getting? Are you maybe segfaulting because you didn't initialize your pointer to a pointer? Where is the rest of your code?
Topic archived. No new replies allowed.