inheritance question
-
Hi, I need to update a code that was written a long time ago, and have some problem. my question is as follow. I have a Base class with function f1 that do something. the Derived class ovveride function f1 and do other thing. now I need to add to the derived class a function f2 that do exactly what Base::f1() is doing. and I dont want to duplicate the code (of course) is it OK to do:
void Derived::f2()
{
Base::f1();
}or is there a better way to call the f1 from the base class? Many thanks,
-
Hi, I need to update a code that was written a long time ago, and have some problem. my question is as follow. I have a Base class with function f1 that do something. the Derived class ovveride function f1 and do other thing. now I need to add to the derived class a function f2 that do exactly what Base::f1() is doing. and I dont want to duplicate the code (of course) is it OK to do:
void Derived::f2()
{
Base::f1();
}or is there a better way to call the f1 from the base class? Many thanks,
-
Hi, I need to update a code that was written a long time ago, and have some problem. my question is as follow. I have a Base class with function f1 that do something. the Derived class ovveride function f1 and do other thing. now I need to add to the derived class a function f2 that do exactly what Base::f1() is doing. and I dont want to duplicate the code (of course) is it OK to do:
void Derived::f2()
{
Base::f1();
}or is there a better way to call the f1 from the base class? Many thanks,
You don't actually have to implement a function to call a base class version of a function: Derived d; d.base::f(); will call the base class version of f - no virtual function dispatch will happen. Oh, don't try this the other way around - strange and destructive things will happen :-) Cheers, Ash
-
You don't actually have to implement a function to call a base class version of a function: Derived d; d.base::f(); will call the base class version of f - no virtual function dispatch will happen. Oh, don't try this the other way around - strange and destructive things will happen :-) Cheers, Ash
Actually there are scenarios where this is useful, like exposing methods from private and protected inheritance. A sort of forwarding.
class Base
{
public:
string name() const;
};
class Derived : protected Base
{
public:
string name() const { return Base::name(); }
};Edit: The reason being of course to change the access level of a method.
-
Actually there are scenarios where this is useful, like exposing methods from private and protected inheritance. A sort of forwarding.
class Base
{
public:
string name() const;
};
class Derived : protected Base
{
public:
string name() const { return Base::name(); }
};Edit: The reason being of course to change the access level of a method.
Just a thought: As someone who prefers composition over inheritance, I can hardly think of a case using protected/private inheritance. Not sure when I used it last time. Also see Stackoverflow[^]. Cheers :) /Moak
-
Just a thought: As someone who prefers composition over inheritance, I can hardly think of a case using protected/private inheritance. Not sure when I used it last time. Also see Stackoverflow[^]. Cheers :) /Moak
It's more likely to be used to open up a protected function in the base class. I've seen that a lot in MFC code and it gives me the willies:
class base
{
protected:
virtual void a();
};class derived : public base
{
public:
virtual void b()
{
a();
}
};You can also do it using using declarations but that's scary as well. Cheers, Ash
-
Actually there are scenarios where this is useful, like exposing methods from private and protected inheritance. A sort of forwarding.
class Base
{
public:
string name() const;
};
class Derived : protected Base
{
public:
string name() const { return Base::name(); }
};Edit: The reason being of course to change the access level of a method.
In this case I'd almost certainly rewrite the base class to provide what I want than hack the derived class. I know there are social reasons why it may be preferable to forward calls like that but it leaves a bad taste in my mouth. Actually the real bad taste is implementation inheritance but in the world of GUI frameworks people are very stuck in the early 90s. Cheers, Ash
-
Just a thought: As someone who prefers composition over inheritance, I can hardly think of a case using protected/private inheritance. Not sure when I used it last time. Also see Stackoverflow[^]. Cheers :) /Moak
-
In this case I'd almost certainly rewrite the base class to provide what I want than hack the derived class. I know there are social reasons why it may be preferable to forward calls like that but it leaves a bad taste in my mouth. Actually the real bad taste is implementation inheritance but in the world of GUI frameworks people are very stuck in the early 90s. Cheers, Ash
Unfortunately, far from all classes can be rewritten (for several reasons other than social) I have used this for access level elevation a couple of times (probably more than two) in about 17 years, so it's not really common, but I believe there is a use for it. There are of course also other reasons to implement a method in terms of the base class implementation. Code generation tools does it all the time. Mostly along with ToDo comments.
-
It's more likely to be used to open up a protected function in the base class. I've seen that a lot in MFC code and it gives me the willies:
class base
{
protected:
virtual void a();
};class derived : public base
{
public:
virtual void b()
{
a();
}
};You can also do it using using declarations but that's scary as well. Cheers, Ash
Begs for a redesign/refactoring of the interface, if you ask me. :D
-
Unfortunately, far from all classes can be rewritten (for several reasons other than social) I have used this for access level elevation a couple of times (probably more than two) in about 17 years, so it's not really common, but I believe there is a use for it. There are of course also other reasons to implement a method in terms of the base class implementation. Code generation tools does it all the time. Mostly along with ToDo comments.
The only time you can't rewrite a base class is when you haven't got the code for it. The rest of the reasons are all social. Just out of interest anything you can do with inheritance (of concrete base classes) in the context of code generation you could a lot more cleanly do with composition. MFC, as one example, wouldn't be such a mess of deriving from concrete classes if they'd have done that. However there wasn't the C++ expertise around in 1991 that there is now so you can't blame them in hindsight. Cheers, Ash
-
The only time you can't rewrite a base class is when you haven't got the code for it. The rest of the reasons are all social. Just out of interest anything you can do with inheritance (of concrete base classes) in the context of code generation you could a lot more cleanly do with composition. MFC, as one example, wouldn't be such a mess of deriving from concrete classes if they'd have done that. However there wasn't the C++ expertise around in 1991 that there is now so you can't blame them in hindsight. Cheers, Ash
-
I think the social bit got lost in translation. I was looking upon social as political, and not economical, which I assume now you include in social?