2

Is there a way you can invoke a member function of a base class upon a class derived from it?

Class Bass{
    public:
        void func();
};

Class Derived: public Base{
    public:
        void func();
};

I have a practice midterm, and I suspect no, because how would the Base class know about the Derived, but I am not sure.

Zack
  • 13,454
  • 24
  • 75
  • 113
  • Are you talking about virtual functions? That's how they work. You define a virtual function in the base class, implement it in the dervied class, and are able to call it from the base. This is often used as the [TemplateMethod](http://en.wikipedia.org/wiki/Template_method_pattern), to allow the base class to define the order of steps in a process but have derived classes define how those steps are implemented. – Peter Wood Mar 05 '12 at 22:17

5 Answers5

2

Is there a way you can invoke a member function of a base class upon a class derived from it?

Not sure exactly what you mean by this, but given your Base and Derived classes you can do the following. Just make sure you use a reference or pointer, not pass-by-value because of the slicing problem.


Call Base::func() from within Derived::func():

void Derived::func()
{
    Base::func();
}

Call Base::func() explicitly on a Derived object:

Derived d;
d.Base::func();

I [...] am wondering if you could do something like Base::func(Derived d)

As others have pointed out, you can do this using a forward declaration:

// Tell the compiler "Derived" is a class name.
class Derived;

class Base
{
    // Can use the class name since it has been declared.
    void func(Derived& derived);
};

// Define the class named "Derived".
class Derived : public Base
{
    // ...
};

// Use the derived class.
void Base::func(Derived& derived)
{
    // For this bit to work, the definition of `Derived` must
    // be visible at this point (like putting the class above
    // or including your "Derived.h" from "Base.cpp").
    derived.some_derived_method();
}

However, you won't be able to define the Base::func(Derived&) directly in the class definition since you need to finished defining Base and to define Derived first.

Community
  • 1
  • 1
André Caron
  • 44,541
  • 12
  • 67
  • 125
  • I would have guessed that a midterm question would be referring to virtual functions (so invoking a virtual method on a 'Base&' would actually call the 'Derived' implementation). – bdow Mar 05 '12 at 22:24
1

if I understand correctly, you need to call base function with derived parameter? You can do it only using forward declaration and passing derived object by pointer or ref.

class Derived;
class Base{
    public:
        void func(Derived&);
};
innochenti
  • 1,093
  • 2
  • 8
  • 23
0

You should be able to do something like this:

class Derived;

class Base {
    public:
        void func();
        void func(Derived);
};

class Derived : public Base {
    public:
        void func();
};

void 
Base::func(Derived D) {
}

It is okay to use incomplete types in the Base's member function declarations, but you must provide the complete type before their definition.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • @KillianDS - He didn't ask for pass-by-pointer or pass-by-reference. His question asked for pass-by-value. Why the objection? Are you suggesting that my code fragment is invalid? – Robᵩ Mar 05 '12 at 20:15
  • 1
    yes, it is invalid. see [this code sample](http://ideone.com/X04d3). The compiler does not know enough about `Derived` when it's forward declared to make to copy that is necessary for pass-by-value. [pass-by-reference](http://ideone.com/xfb2p) does work because it does not need all type information. – KillianDS Mar 06 '12 at 07:15
  • @KillianDS - No, it is not invalid. See [this code sample](http://ideone.com/Ub7Me). The difference between your incorrect code and my correct code is critical: I define `Base::func(Derived)` *after* I define `Derived`; you define `Foo::test(Bar)` *before* you define `Bar`. You have ignored the essence of my answer: "it is okay to use incomplete types in the Base member function **declarations**, but you must provide the complete type before their **definition**. The type must be complete at the definition (C++03 §8.3.5/6) and the call (§5.2.2/4), but it may be incomplete at the declaration. – Robᵩ Mar 06 '12 at 15:00
  • I stand corrected, messed up the declaration/definition part. Upvote to counter the current -1 (which wasn't mine) – KillianDS Mar 06 '12 at 15:35
0

You can use forward declaration of Derived class:

class Derived;
ebutusov
  • 563
  • 2
  • 5
-1

First of all, do you mean methods on an object, or static class methods?

Secondly, the answer is: it depends what the object you're invoking the method call on is. This is the nature of polymorphism: if your object is of type 'Derived', then even if it has been cast to a 'Base' the method call will still invoke the Derived version of func.

Is that what you were asking?

Dave
  • 556
  • 1
  • 6
  • 17