473,322 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Call derived class function from base class reference

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
4 14391
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
9
by: Daniel Kay | last post by:
Hello! I have written two template classes which implement the observerpattern in C++. I hope I manage to desribe the problem I have. template<class T> class Observer { /* ... */ }; ...
3
by: John A. Prejean | last post by:
This one has me stumped. I have a base form I am trying to wrap up, but I have one problem. In two functions I am opening a "record detail" form. I would like to keep the code in the base form...
7
by: Rafał Maj Raf256 | last post by:
There was a way to trinck program to call purly abstract non-existing method (causing undefined behaviour), I would like to try that for curiosity (how different compilers/platforms react), how to...
4
by: Jeff | last post by:
The derived class below passes a reference to an object in its own class to its base calss constructor. The code compiles and will run successfully as long as the base class constructor does not...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
2
by: Jessica | last post by:
I have a base class and a derived class, but I am getting errors when I try to access functions of the derived class. Simplified version of my code is as follows: //////////////// // test2.hh...
10
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.