user3496846
user3496846

Reputation: 1665

Overriding - how to do it with less typing?

Suppose I have

class A
{
public:
   virtual void Muhahahaha(int a, float f, Object C, Smth else, Etc etc);
};

and

class B: public A
{
public:
   virtual void Muhahahaha(int a, float f, Object C, Smth else, Etc etc) override;
};

Now, what bothers me is the fact that I have to retype the whole argument list every time I override a base-class member function. Why could not I just write something like this:

virtual void Muhahahaha override;

and the compiler just knows what I want there? Well, excluding the case when there is only one Muhahahaha in there, of course...

Is there such a way?

Upvotes: 0

Views: 48

Answers (2)

Christian Hackl
Christian Hackl

Reputation: 27548

The problem is that you are taking too many parameters in a single function. This is bad for every function, virtual or not.

As an example for a solution which often works to mitigate this problem, why not bundle int a, float f, Object C, Smth else, Etc etc into a class? Like this:

struct Parameters
{
    int a;
    float f;
    Object C;
    Smth Else;
    Etc etc;
};

Then you have less typing to do:

class A
{
public:
   virtual void Muhahahaha(Parameters const& arguments);
};

class B : public A
{
public:
   void Muhahahaha(Parameters const& arguments) override;
};

The fact that you "have to retype the whole argument list every time" is just a symptom. Once you refactor your code such that the argument list becomes short, the problem disappears all by itself.

Upvotes: 1

Richard Hodges
Richard Hodges

Reputation: 69942

Is there such a way?

Yes, but you must never, ever do it.

I won't

Look if I show you this, and you do it, my reputation will suffer...

I promise

They'll downvote me just for showing you...

Go on, please...?

ok then:

#define OBFUSCATE Muhahahaha(int a, float f, double C, char etc)

class A
{
public:
    virtual void OBFUSCATE;
};

class B: public A
{
public:
    virtual void OBFUSCATE override;
};

void A::OBFUSCATE
{
    // try to debug this
}

void B::OBFUSCATE
{
    // try to debug this too!
}

Upvotes: 3

Related Questions