I have a structure like this
struct structure
{
BaseObject &A; //BaseObject has a function declared as virtual
};
In run time, I am dynamically assign an object to &A
structure *s = new structure;
DerivedObjectB *B = new DerivedObjectB(); //Derived class overloads the virtual function
s->A = *B; //s is a pointer of the structure. S has been initialized
I can compile this code but I am getting a seg-fault error in runtime.
I have a restriction that I can not use a pointer.
This is not a homework. The compiler I am using as a reverse compiler has a restriction of using pointers because of an issue in building SSA
If you cannot use a pointer, you must use a reference.
If you use a reference, it must be initialized to it’s final value as it’s constructed.
Your code above shouldn’t have compiled since a
structurecould not be created that way since it had no automatic default structure since it has a reference member. Also, Even if it did compile, theAmember would have been aBaseObjectcopy of theDerivedObjectB‘s parentBaseObjectobject, or some other bizzare not-what-you-wanted.Are you absolutely sure you cannot use pointers there? That makes no sense at all, and makes this very difficult to work with.