I have a parent class and two child classes that inherit from the parent class. The parent class has a protected pointer to something, and a method to access that pointer.

class parent{
public:
	int* accessPtr(){
		return ptr;
	}
protected:
	int* ptr;
};
class child1 : public parent{
public:
	child1(int* somevalue){
		//initializes ptr to somevalue, a pointer to something stored elsewhere
	}
};
class child2 : public parent{
public:
	child2(){
		//initializes ptr using new
	}
	~child2(){
		delete ptr;
	}
};

int main(){
	child2* instance = new child2();
	instance->accessPtr();	//this line is where my debugger stops
}

Naturally, my code compiles, and it even runs when I don't call accessPtr() on anything. Any ideas?

Recommended Answers

All 4 Replies

Without having lookin so closely at your code shouldnt
child2->accessPtd() be
instance->accessPtr()
instead?

Good point, though that mistake only appears in the code I typed here, not in my source. Edited.

It is somewhat difficult to know what you have in real code,
like your comment
//initialize ptr using new

Do you actually do this?

If you supply your full sourcecode it will be easier to help you

Sorry to all for making this topic, turns out I had forgotten to pass a value for the pointer to my constructor, and it was getting initialized to NULL (thanks to the default value I put there myself), and SIGSEGV is G++'s way of saying I tried to deref null.

to monkey_king: the header file for the classes is about 100 lines long, the .cpp file is 300 lines long, and of course the pointer is to another class which I implemented, which is another 70 or 80 lines. Therefore, I tried to narrow my source code to just the part that was giving me trouble.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.