object1.delete_subclass(object2);//here is my question
object2.delete_subclass(object1);
}
the problem is each object must be able to delete other subclass objects of the superclass including it's own subclass objects
hence object1 should be able to delete object3 aswell which is why i can't
use 'delete_subclass2(subclass2 a);'
given that object3 is not of type subclass2
i can't compile my project at this point which is why i would like to know if my above syntax is viable thank you.
Can a super class type reference be used to refer to one of it's subclasses?
Yes. Pointers and references are polymorphic. References have pointer semantics with a few critical exceptions.
You need to make the destructor virtual (or perhaps take advantage of constant reference lifetime extension) to avoid object slicing.
However,
1 2
object1.delete_subclass(object2);//here is my question
object2.delete_subclass(object1);
Is either poorly named or broken. Once (if) object2 is destroyed as a result of the first line, line 2 invokes undefined behavior -- it's illegal to call a method on an object which no longer exists.
The implication of those two lines is that there is an ownership cycle between two or more subclasses. More information is required regarding the best way to resolve that issue.
Ok line 2 was a error it wasn't meant to be apart of the question but thank you for identifying it.
The code is meant to give each object the ability to destroy other objects without having to use a switch or various nested if's given that if a new subclass is added later the functions of the previous wouldn't need to be changed.
Also I want to try various approaches to compare speed
The code is meant to give each object the ability to destroy other objects without having to use a switch or various nested if's given that if a new subclass is added later the functions of the previous wouldn't need to be changed.
Why is polymorphism insufficient?
More importantly: why do you need to control of the destruction of other objects?
This seems like an XY problem: ( http://xyproblem.info/ ). What problem are you trying to solve?
Also I want to try various approaches to compare speed
Why? Is this specific part of your program causing a performance issue?
It's a game where different subclasses have to be destroyed by the same function regardless of which subclass object is passed
Otherwise i'd have to overload the several functions