Sign in to follow this  

overriding hell problem

This topic is 3318 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Check this code:
class mainClass_mg
{
public:
	virtual void overrideMe()
	{
		std::cout << "overrideMe mainClass_mg" << endl;
	}
};

mainClass_mg* getMeOne(int i);

class subClass_s : public mainClass_mg
{
protected:
	int id;
public:
	virtual void overrideMe()
	{
		std::cout << "overrideMe subClass_s id " << id << endl;
	}
	void update()
	{
		getMeOne(id)->overrideMe();
	}
};

class child1 : public subClass_s
{
	
public:
	child1(int i) { subClass_s::id = i ;}
	void overrideMe()
	{
		std::cout << "id is " << subClass_s::id << endl;
		subClass_s::overrideMe();
	}
	void update()
	{
		subClass_s::update();
	}
};

class child1;

child1 ch1(1);
child1 ch2(2);
child1 ch3(3);

mainClass_mg* getMeOne(int i)
{
	if(i == 1) return &ch1;
	if(i == 2) return &ch2;
	if(i == 3) return &ch2;

	return 0;
}


int main(int argc,char **argv)
{
	
	ch1.update();

	return 0;
}


The problem is not really visible, I catched this from debugger, so logically I expected on first line to be writing id = 1, but sometimes i got called ( not allways ) for example if I call ch1.update(); i expect to get calls of ch1's subClass_s. But no, I get calls from ch2's subClass_s. so it is not writing that id is 1 , but 2??? What seems to be the problem? thanks

Share this post


Link to post
Share on other sites

mainClass_mg* getMeOne(int i)
{
if(i == 1) return &ch1;
if(i == 2) return &ch2;
if(i == 3) return &ch2;//This should be &ch3

return 0;//change this to return NULL; as explained below
}




That's a small bug there, but I can't imagine that it would affect ch1.

EDIT:


getMeOne(id)->overrideMe();




It's also worth pointing out that this defaults to 0, but you de-reference it anyway. Change it to something like:


mainClass_mg * temp = getMeOne(id);
if(temp != NULL)
getMeOne(id)->overrideMe();




Also, you should change the return 0; in getmeone to return NULL -- for the sake of readability and unambiguity. Obviously it works either way, but traditionally with pointers you use NULL if it isn't pointing to anything in particular.

Share this post


Link to post
Share on other sites
Ok, I know that, there is checking of NULL pointers, just I didn't put it in the code, sorry. Bu this doesn't have any impact, on that program is going in different class's functions.

Thank you anyway

Share this post


Link to post
Share on other sites

This topic is 3318 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this