Call derived class function from base class reference

J

Javier

Hello, is this possible?
I have a pure virtual function in the base class (to force the
programmers of the derived classes to have this function implemented)
but I want to call the derived class function from a base class
reference:

class A
{
public:
virtual void function() = 0;
};

class B
:
public A
{
public:
void function() { cout << "Hello" << endl; };
}

int main()
{
B derived();
A &base = dynamic_cast<A &>(B); // don't now if this is the way

base.function(); // I need this type of call because I dont't
know the derived class type at runtime.
}


Of course I could have a function2 (private) and make it pure virtual,
and then have function() in base class calling the virtual function2:

class A
{
public:
void function() { this->function_impl() };

protected:
virtual void function_impl() = 0;
};

class B
:
public A
{
protected:
void function_impl() { cout << "Hello" << endl; };
}

int main()
{
B derived();
A &base = dynamic_cast<A &>(B);

A.function();
}

Is this good?
But... can I do it the first way?

Thanks.
 
V

Victor Bazarov

Javier said:
Hello, is this possible?
I have a pure virtual function in the base class (to force the
programmers of the derived classes to have this function implemented)
but I want to call the derived class function from a base class
reference:

class A
{
public:
virtual void function() = 0;
};

class B
:
public A
{
public:
void function() { cout << "Hello" << endl; };
}
;

You've missed a semicolon.
int main()
{
B derived();

'derived' here is a function. Drop the parentheses.
A &base = dynamic_cast<A &>(B); // don't now if this is the way

Of course this is not correct. First of all, if you have dropped
the parentheses, you _could_ do

A &base = dynamic_cast<A&>(derived);

but you don't need to, the conversion is _implicit_:

A &base = derived;
base.function(); // I need this type of call because I dont't
know the derived class type at runtime.

That should work OK, after you fix the declarations.
}


Of course I could have a function2 (private) and make it pure virtual,
and then have function() in base class calling the virtual function2:

class A
{
public:
void function() { this->function_impl() };

protected:
virtual void function_impl() = 0;
};

class B
:
public A
{
protected:
void function_impl() { cout << "Hello" << endl; };
}
;
int main()
{
B derived();
A &base = dynamic_cast<A &>(B);

A.function();
}

Is this good?

That's just as OK, given that you fix the declarations and get
rid of the dynamic_cast.
But... can I do it the first way?

Should be OK.

V
 
V

Victor Bazarov

Daniel said:
Dynamic cast is unecessary here, simply
A& base(B);
will work.

Yes, but that would declare 'base' as a FUNCTION.
This is perfectly valid.

Nope, it wouldn't be. Try it.
Whether or not you want to encapsulate call
to the virtual function or not is your design decision, possible are
both ways.
Yours,
Daniel

V
 
D

Daniel Kraft

Javier said:
class A
{
public:
virtual void function() = 0;
};

class B
:
public A
{
public:
void function() { cout << "Hello" << endl; };
}

int main()
{
B derived();
A &base = dynamic_cast<A &>(B); // don't now if this is the way

Dynamic cast is unecessary here, simply
A& base(B);
will work.
base.function(); // I need this type of call because I dont't
know the derived class type at runtime.
}

This is perfectly valid. Whether or not you want to encapsulate call to
the virtual function or not is your design decision, possible are both ways.

Yours,
Daniel
 
J

James Kanze

I have a pure virtual function in the base class (to force the
programmers of the derived classes to have this function implemented)
but I want to call the derived class function from a base class
reference:
class A
{
public:
virtual void function() = 0;
};
class B
:
public A
{
public:
void function() { cout << "Hello" << endl; };
}

You need a semicolon here.
int main()
{
B derived();

Here you declare a function called derived, which returns a B.
I doubt that that's what you want. Maybe:

B derived ;
A &base = dynamic_cast<A &>(B); // don't now if this is the way

Presumably, you mean:

A& base = derived ;

You don't need the dynamic_cast, or any other type of cast (but
what you cast must be the name of an object, not the name of a
type).
base.function(); // I need this type of call because I dont't
know the derived class type at runtime.

That's exactly what virtual functions (pure or not) are for.
Of course I could have a function2 (private) and make it pure virtual,
and then have function() in base class calling the virtual function2:
class A
{
public:
void function() { this->function_impl() };
protected:
virtual void function_impl() = 0;
};

That's the usual solution, in order to be able to add code
enforcing pre- and post-conditions. If the function doesn't
have any pre- and post-conditions (often the case when inversion
of the control is involved), then there's no point.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top