By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,386 Members | 881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,386 IT Pros & Developers. It's quick & easy.

Call derived class function from base class reference

P: n/a
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.

Jun 8 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Javier wrote:
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
Jun 8 '07 #2

P: n/a
Javier wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 8 '07 #3

P: n/a
Daniel Kraft wrote:
Javier wrote:
>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.
Yes, but that would declare 'base' as a FUNCTION.
>
> base.function(); // I need this type of call because I dont't
know the derived class type at runtime.
}

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 8 '07 #4

P: n/a
On Jun 8, 7:58 pm, Javier <jjeron...@gmail.comwrote:
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.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 8 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.