Hi all,
I've been struggling with something for a number of weeks now and I just can't seem to solve it.
I've got a bit of code that looks as follows:
class classA
{
public:
classA( );
~classA( );
classBBase* pointerToClassBBase;
protected:
private:
};
class classBBase
{
public:
classBBase( );
~classBBase( );
protected:
private:
}
class classB : public classBBase
{
public:
double testValue;
protected:
private:
};
namespace predefined_classB_objects
{
enum PredefinedClassBObjects
{
predefinedClassBObject1,
predefinedClassBObject2
};
classB createPredefinedClassBObject( PredefinedClassBObjects predefinedClassBObjectName )
{
classB predefinedClassBObject_;
switch( predefinedClassBObjectName )
{
case predefinedClassBObject1:
predefinedClassBObject_.testValue = 1.0;
break;
case predefinedClassBObject1:
predefinedClassBObject_.testValue = 2.0;
break;
default:
cerr << "Case is not defined" << endl;
};
return predefinedClassBObject_;
}
}
namespace predefined_classA_objects
{
enum PredefinedClassAObjects
{
predefinedClassAObject1,
predefinedClassAObject2
};
classA createPredefinedClassAObject( PredefinedClassAObjects predefinedClassAObjectName )
{
classA predefinedClassAObject_;
classB predefinedClassBObject_;
switch( predefinedClassAObjectName )
{
case predefinedClassAObject1:
predefinedClassBObject_ = predefined_classB_objects::createPredefinedClassBObjects( predefined_classB_objects::predefinedClassBObject1 );
break;
case predefinedClassAObject2:
predefinedClassBObject_ = predefined_classB_objects::createPredefinedClassBObjects( predefined_classB_objects::predefinedClassBObject2 );
break;
default:
cerr << "Case is not defined" << endl;
};
predefinedClassAObject_.pointerToClassBBase = &predefinedClassBObject_;
return predefinedClassAObject_;
}
}
int main( )
{
classA predefinedClassAObject;
predefinedClassAObject = predefined_classA_objects::createPredefinedClassAObject( predefined_classA_objects::predefinedClassAObject1 );
std::cout << predefinedClassAObject.pointerToClassBBase->testValue << std::endl;
return 0;
}
If I run this code, it produces the right output and on the screen I see the value 1.0 as expected.
The problem however is when I then pass the predefinedClassAObject to another class which has a function that is iteratively called, which uses the testValue. When I print the value of testValue from within that function it's 0 to machine precision (my last run gave me 1.4822e-323).
So I'm confused as to where it's going wrong. There is more detail involved in how I pass the predefinedClassAObject to the other class and how I use it. Before getting into that though, I wanted to just ask if there is anything fundamentally wrong with what I've put in code here.
I'd really appreciate feedback, as I just can't seem to crack this and figure out where things are going wrong.
I can provide more details if necessary.
Thanks a lot in advance!
Cheers,
Kartik