Problem with inheritance, pointers and initialiasation lists.

Ok here's an example of the problem im having:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class obj{
    protected:
    int x;
   
    public:
    obj(int);
};
obj::obj(int x1): x(x1){

}


class someclass: public obj{
    private:

    public:
    someclass(int);
    void somefuntion();
}*someclass1;
someclass::someclass(int y):obj(y){

}
someclass::somefunction(){
    x += 1; //this gives a segmentation fault which means it's unitialised in memory doesn't it? 
}

main(){
    someclass1->somefunction();
    return 0;

}



SO i guess my question is why is x seen as unaccessible when i have initialised it in the call to someclass::someclass???

Thanks for any help

edit: I hope this highlights the problem because its not actual code.
Last edited on
closed account (zb0S216C)
Private members of a base class can only be seen by it's own members. You must declare x of obj as protected in order for inherited classes to see x.

private: Only the members of the same class that declared x can see this member.
protected: Only inherited classes and the base class can see protected members.

Wazzak
yep sorry changed that it was meant to be protected. I've found how to sort the problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class obj{
    protected:
    int x;
   
    public:
    obj(int);
};
obj::obj(int x1): x(x1){

}


class someclass: public obj{
    private:

    public:
    someclass(int);
    void somefuntion();
};
someclass::someclass(int y):obj(y){

}
someclass *someclass1 = new someclass;//all i needed was this line I think
someclass::somefunction(){
    x += 1; 
}

main(){
    someclass1->somefunction();
    return 0;

}
1
2
3
4
5
main(){
    someclass1->somefunction();
    return 0;

}


Your problem is here: Where is someclass1 pointing? We don't don't know because you haven't allocated any memory for the someclass1 pointer. Basically there is no someclass object at which the someclass1 pointer is pointing to.

EDIT

someclass *someclass1 = new someclass;//all i needed was this line I think
Didn't see this line.

1
2
3
4
5
6
main(){
    someclass *someclass1 = new someclass;
    someclass1->somefunction();
    delete someclass1;
    return 0;
}


never allocate globally, it can lead to some serious problems which are difficult to debug. Put your allocating code inside the main scope. Also do not forget do deallocate when you're done =)
Last edited on
Doesn't the someclass constructor call in line 23 require an argument? But yes, you were getting a segmentation violation because someclass1 was not allocated, not because of the member variable.
@doug4
No the someclass() "the default constructor" is always available by default even tho it hasn't been specified.
Unless something has changed recently, the default constructor is only automatically generated when NO constructors are declared for the class. Since the single-argument constuctor is declared, the default constructor should not be generated.

Am I missing something?
Yeah you're right , the default constructor is hidden if other constructor exists, I just tested it. That code shouldn't compile
Are you saying my code shouldn't compile? It does, well it seems to anyway.

Also CppSpartan in your first post you said i shouldn't allocate globally. Could you elaborate a little on that?

I've just spent ages ripping everything out of my main function to allocate globally because I've got quite a few classes all accessing each other and this seemed like the best way to do it.


edit: Oh sorry yeah your right didn't write that part out right. It has an argument in my code and compiles ok.:)

Oh thanks for help btw.
Last edited on
It's generally a bad practice because it makes the program more complex. When having several classes that access each other you can solve this in several ways.

1 pass a reference, to each class you want to access, into your constructor and store it as a member.

2 pass a reference to a class that encapsulate our classes.

3 use function pointers
Thanks for the help.
Topic archived. No new replies allowed.