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

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?
line 17 makes no sense with how you've defined getbody().
@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
you're probably right MikeyBoy.
my brain is completely fried today.
apologies OP.
q1670741824 wrote:
why I can't do like this?
What do you mean? You can:
http://ideone.com/ieyL1e
Last edited on
@ 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.