Jump to content
  • Advertisement
Sign in to follow this  
Flandria4

Base class as argument instead of derived class?

This topic is 1506 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey

 

I'm experimenting a bit and was wondering if it is possible to make (something alike) a method/function that accepts derived classes as argument instead of a base class.

 

To illustrate my (poorly worded?) question:

 

 

I have 2 base classes: Items.cpp and Enemies.cpp.

Both have several derived classes.

 

I have made a DeleteAll() method with several overloads as following:

void DeleteAll(static const int arrLength, HitRegion *hitRegion[], Box* boxPtr[]);
void DeleteAll(static const int arrLength, HitRegion *hitRegion[], Coin* coinPtr[]);
void DeleteAll(static const int arrLength, HitRegion *hitRegion[], Diamond* diamondPtr[]);
void DeleteAll(static const int arrLength, HitRegion *hitRegion[], Heart* heartPtr[]);

void DeleteAll(static const int arrLength, HitRegion *hitRegion[], Unit1* unit1Ptr[]);
void DeleteAll(static const int arrLength, HitRegion *hitRegion[], Unit2* unit2Ptr[]);
void DeleteAll(static const int arrLength, HitRegion *hitRegion[], Frog* frogPtr[]);

But this doesn't feel like it's the correct way.  I only have to execute

for (int i = 0; i < arrLength; i++){
	if(itemPtr[i]!=nullptr){
		delete itemPtr[i];
		itemPtr[i] = nullptr;
	}
	if(hitRegion[i]!=nullptr){
		delete hitRegion[i];
		hitRegion[i] = nullptr;
	}
}

every method and I could just copy this for every derived class in one big method without arguments that takes care of all of it.

 

But still, I love experimenting and I'm wondering if it would it be possible to do something like:

void DeleteAll(static const int arrLength, HitRegion *hitRegion[], Items* itemPtr[]);
void DeleteAll(static const int arrLength, HitRegion *hitRegion[], Enemies* ememyPtr[]);

and put the derived classes instead of the base class as argument, like this:

DeleteAll(ITEMS_BOX, m_HitBoxPtr, m_BoxPtr);

I know it doesn't work like this, but is there an alternate/similar/professional approach? I'm just trying to improve my coding skills.

I'm sorry if this is a weird question, I couldn't find anything on this on the interwebz.

 

 

Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement

I would just have this be a template function:

template<typename T>
void DeleteAll(const int arrLength, T *thing[])
{
	for (int i = 0; i < arrLength; i++){
		delete thing[i]; // handles null
		thing[i] = nullptr;
	}
}

But before doing that, ask yourself whether or not these arrays really need to be pointers at all. If your array values are of type Enemy, say, instead of Enemy*, then you don't need to worry about deleting them.(and if you don't know the number of items at compile time, use std::vector, not raw arrays).

Edited by phil_t

Share this post


Link to post
Share on other sites

I didn't know about templates before, thank you! Also, I just learned about vectors a week ago, I'll use those for my next project. Way better than arrays.

 

About the pointers, how can my separate class not be a pointer/object?

Share this post


Link to post
Share on other sites

how can my separate class not be a pointer/object


Pointers and objects are not the same thing. Consider:
 
class MyClass { };

MyClass  object;
MyClass* pointer_to_object = &object;
MyClass& reference_to_object = object;
One of the advantages of C++ over other languages is that user-defined types are (almost) indistinguishable from built-in types. Just like an int or a string or an array can be local values, so too can your own classes be used as local values. You can have a vector of (almost) any type:
 
std::vector<int> integers;
std::vector<MyClass> objects;
std::vector<MyClass*> pointers;
std::vector<int*> int_pointers;
The big thing to note is that a vector of int or MyClass is a vector to those types specifically. They cannot hold derived types at all. A pointer can point to either the specified type or a derived type. Pointers (or references) are necessary if you need polymorphism (though you should be following more modern C++ tutorials and avoiding plain T* pointers in favor of unique_ptr<T> or shared_ptr<T> or references). Pointers also have a number of other applications; knowing when to use and not to use them are part of mastering C++. Edited by SeanMiddleditch

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!