0

I am looking to have a virtual class used to ensure that all inheriting classes have the following functions

Initialize()
IsInitialized()
Clear()

Maybe there is another approach but the use of the abstract class seems to be the best options as far as I can tell.

The issue is that the Initialize() does not always have the same parameters For example:


class baseObject
{
    virtual void Initialize(...) = 0; //Only want to ensure Initialize is implemented in the inheriting class but parameters may very
    virtual void Clear() = 0;
    virtual bool IsInitialized() = 0;
}

class triangle : public baseObject
{
    virtual void Initialize(double length1, double length2, double length3); //Three parameters
    virtual void Clear();
    vitrual bool IsInitialized();
    ...
}

class square : public baseObject
{
    virtual void Initialize(double baseLength, double baseHeight); //two parameters
    virtual void Clear();
    virtual bool IsInitialized();
    ...
}

etc...

8
  • 1
    Perhaps you're thinking about it wrong? Perhaps you should make constructors for the initialization of each object instead? If you use constructors, all three of the listed functions simply won't be needed. Oct 9, 2021 at 4:59
  • 1
    Just about Anything is possible in c++ but trying to force a square peg into a round hole is a clear sign you should rethink what you’re doing.
    – Taekahn
    Oct 9, 2021 at 5:01
  • 2
    @Taekahn Sometimes brute force is not needed... youtu.be/baY3SaIhfl0 ;) Oct 9, 2021 at 5:04
  • 3
    Let's say a function has returned to you a baseObject* that points to an object that needs to have its Initialize method called. How many parameters is it expecting / can it handle? (If you cannot answer without knowing which derived class was instantiated, then Initialize probably has no business being defined at the baseObject level.)
    – JaMiT
    Oct 9, 2021 at 5:04
  • @Someprogrammerdude Maybe I should have chosen different function names... maybe Update() instead of Initialize(). I will have one class that has a bunch of objects that will need to be updated and am looking for a way to "force" a template if you will. I like the idea of using the constructor but it does not allow for the dynamic update.
    – JCoder
    Oct 9, 2021 at 5:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.