Class Scope-Can a super class type reference be used to refer to one of it's subclasses?

Can a super class type reference be used to refer to one of it's subclasses?
e.g

class superclass
{
virtual void delete_subclass()=0;
};


class subclass1: public superclass
{
public:

delete_subclass(superclass &a); // <--this line
};


class subclass2: public superclass
{
public:

delete_subclass(superclass &b);// <--this line
};

main()
{

subclass1 *object1=new subclass1;
subclass2 *object2=new subclass2;
subclass1 *object3=new subclass1;

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.
Last edited on
i would like to know if my above syntax is viable


I don't think it's viable. Perhaps you can tell me why you think you need to do this?
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.
Last edited on
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

Rikasi wrote:
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?

Last edited on
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
Last edited on
That doesn't sound like a very fun game. Assuming that this is actually the problem you're trying to solve, why is polymorphism insufficient?

In particular:
1
2
struct Super { virtual ~Super() {}; }; 
class Sub: public Super { ... }; 

What owns the objects? The owner should destroy them polymorphically.
Topic archived. No new replies allowed.