Inconsistencies in overriding private function (bug?)  
Author Message
Jonathan Scott





PostPosted: Visual C++ Language, Inconsistencies in overriding private function (bug?) Top

I have a class, "Facade", which has two private members. One returns void, the other returns a pointer to another class.

virtual void doProcessing();
virtual ResultSet* processPolicy(Policy* policy, boost::gregorian::date evaluationDate);

I have two derived classes, class A overrides doProcessing(), class B overrides processPolicy().

I noticed that A's doProcessing() will get called, but that B's processPolicy() does not. Both instances of A and B get created and started in the same manner. Once started, doProcessing() gets called, which in turn calls processPolicy().

The only differences I can spot One is void and has no parameters; the other returns a value and has parameters. Do I need to treat one different from the other when overriding Or is this a bug

Jonathan Scott



Visual C++11  
 
 
Sarath.





PostPosted: Visual C++ Language, Inconsistencies in overriding private function (bug?) Top

Could you please post some code especially the function declarations in derived class and making the objects and calling functions


 
 
Jonathan Scott





PostPosted: Visual C++ Language, Inconsistencies in overriding private function (bug?) Top

Sarath, Thank you for taking the time to read my post. Here are the declarations of the classes, and the code from which it is called. If you need anything more, please ask!

Jonathan Scott

#include <boost/date_time/gregorian/gregorian.hpp>
#include "MRMResultSet.h"

class ModelFacade {
friend class TestModelFacade;
public:
ModelFacade(boost::gregorian::date evaluationDate);
virtual ~ModelFacade();

void processPolicies();
protected:
std::vector<Policy*> policiesWithErrors_;

private:
boost::gregorian::date evaluationDate_;
virtual void doProcessing();
virtual MRMResultSet* processPolicy(Policy* policy,
boost::gregorian::date evaluationDate);
};

################################################################################

#include "ModelFacade.h"

class STDExceptionThrowingModelFacade : public ModelFacade {
public:
STDExceptionThrowingModelFacade(boost::gregorian::date evaluationDate) :
ModelFacade(evaluationDate)
{ }
virtual ~STDExceptionThrowingModelFacade() { };

private:
void doProcessing() {
throw std::exception("std::exception OCCURRED!");
}
};

################################################################################

#include "ModelFacade.h"

class PolicyThrowsExceptionModelFacade : public ModelFacade {
public:
PolicyThrowsExceptionModelFacade(boost::gregorian::date evaluationDate) :
ModelFacade(evaluationDate)
{ }
virtual ~PolicyThrowsExceptionModelFacade() { };

private:
MRMResultSet* processPolicy(Policy* policy,
boost::gregorian::date evaluationDate)
{
throw std::exception("ERROR OCCURRED!");
return NULL;
}
};

################################################################################

This is the test code which works, whereby the overridden doProcessing() is
called:

void TestModelFacade::testModelThrowingSTDException() {
ModelFacade* facade = new STDExceptionThrowingModelFacade(
evaluationDate_);
assertEqual(date(2006,1,1), facade->getEvaluationDate(), __LINE__, __FILE__);

try {
facade->processPolicies();
fail("Should have thrown an exception!", __LINE__, __FILE__);
} catch (std::exception& e) {
assertEqual("std::exception OCCURRED!", e.what(), __LINE__, __FILE__);
}

delete facade;
}

################################################################################

This is the test code which DOES NOT work; i.e. the overridden processPolicy()
is not called.

void TestModelFacade::testHandlingOfPolicyErrors() {
ModelFacade* facade = new PolicyThrowsExceptionModelFacade(
evaluationDate_);

facade->processPolicies();

assertEqual(1, facade->policiesWithErrors_.size(), __LINE__, __FILE__);
}

I expect that the derived class' version of processPolicy() gets called from
ModelFacade's doProcessing(), where the catching of an exception should cause
the Policy pointer to get pushed on to the vector policiesWithErrors_.

The first test passes (testModelThrowingSTDException), but the second
(testHandlingOfPolicyErrors) does not; i.e. no exception is thrown. (I have
verified this with a cout in the derived class' processPolicy(), which does not
come out)


 
 
Viorel.





PostPosted: Visual C++ Language, Inconsistencies in overriding private function (bug?) Top

[...] If you need anything more, please ask! [...]

If processPolicies function iterates the policiesWithErrors_ collection and calls member functions of each object, then I think you should confirm that the required functions are declared virtual or pure-virtual inside the Policy base class. Maybe you should show us more details related to Policy class and processPolicies function.


 
 
Jonathan Scott





PostPosted: Visual C++ Language, Inconsistencies in overriding private function (bug?) Top

Are you saying that the ability to have a derived class' version of a virtual method be called from the parent class is dependent not just on the declaration of the method, but also on what that method references !

Jonathan Scott


 
 
Marius Bancila





PostPosted: Visual C++ Language, Inconsistencies in overriding private function (bug?) Top

You have a base class A, with two functions F1, and F2. Now, you have D1 overriding F1, and D2 overriding F2.

What you do is:

A* a1 = new D1;

a1->F1();

Obviously, the D1::F1 gets called.

Then you do:

A* a2 = new D2;

a2->F1();

since, D2 does not override F1, obviously, A::F1 gets called.

 

You call twice processPolicies(), I don't see you calling doProcess().



 
 
Jonathan Scott





PostPosted: Visual C++ Language, Inconsistencies in overriding private function (bug?) Top

processPolicies() calls doProcessing() which calls processPolicy().

F1 calls F2 which calls F3. In one case, if I override F2 and call F1, the new F2 gets called. In another case, if I override only F3 and call F1, the new F3 does not get called.

Jonathan Scott


 
 
Marius Bancila





PostPosted: Visual C++ Language, Inconsistencies in overriding private function (bug?) Top

I think you did not show us all the code.

Please run this sample. It shows correct behaviour, based on the relashionships you described:

class A 
{
protected:
	virtual void F2() {std::cout << "A::F2" << std::endl; F3();}
	virtual void F3() {std::cout << "A::F3" << std::endl;}

public:
	void F1() {std::cout << "A::F1" << std::endl; F2();}
};

class D1 : public A
{
	virtual void F2() {std::cout << "D1::F2" << std::endl; F3();}
};

class D2 : public A
{
	virtual void F3() {std::cout << "D2::F3" << std::endl;}
};

int _tmain(int argc, _TCHAR* argv[])
{
	A* a1 = new D1;
	a1->F1();

	A* a2 = new D2;
	a2->F1();

	return 0;
}



 
 
Jonathan Scott





PostPosted: Visual C++ Language, Inconsistencies in overriding private function (bug?) Top

My apologies, the problem is solved. Thank you. Your code worked as expected, and now, so does mine.

Next time, I will try to be more careful about my data. That was the source of my trouble. Thank you again,

Jonathan Scott