Accessing derived class functions through pointer of base class

I'm not sure if this counts as a beginner question but, i am a beginner so yeah..

First of all, I know that you can't access a derived class' functions through a pointer to a base class, so I assume there is some way around this issue because right now nothing makes sense to me anymore.

So,

I have an abstract base class A and two classes B and C whom inherit from this base class.

The base class has a pure virtual function called getInfo() that is meant to get the info from the objects of the derived classes.

So in my main I have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
int main(){
    A *arr[4];
 
 
    arr[0] = new B("Lam1", 5, "blank lack");
    arr[1] = new C("Tra1", 25, "blank lack");
    arr[2] = new C("Tra2", 35, "blank lack");
    arr[3] = new B("Lam2", 12, "blank lack");
 
 
    for (int i = 0; i < 4; i++)
    {
        cout << arr[i]->getInfo() << endl << endl;
    }
 
 
    system("pause");
 
 
    for (int i = 0; i < 4; i++)
    {
        if (dynamic_cast <B*>(arr[i]) != nullptr)
        {
            cout << arr[i]->getInfo() << endl;
        }
    }
 
 
    system("pause");
 
 
    for (int i = 0; i < 4; i++) {
 
 
        if (dynamic_cast <C*>(arr[i]) != nullptr)
        {
                        //I want to access the current object's method and change its value, i.e setValue()
            cout << arr[i]->getInfo() << endl << endl;
        }
    }
    return 0;
}


The comment in the bottom of the code shows where my problem lies. What do I need to do to access the object's methods without the use of virtual functions? Or do I have to? Should I be declaring the array or the objects differently?

I must be missing something because what's the point of using inheritance if you'll need a virtual copy of each derived class' functions to get it to work properly?

Any help appreciated.

If you need to see any other code i.e. classes just ask but it doesnt seem to me like that's where the problem lies.

Regards, E. Finoli
First of all, I know that you can't access a derived class' functions through a pointer to a base class
Yes you can. Make the function virtual in the base class.

what's the point of using inheritance if you'll need a virtual copy of each derived class' functions to get it to work properly?

You can make the function different. Don't copy the function. Make it different. Make the derived function do something different. If you don't want the derived class function to do something different, don't write that function in the derived class.
Yes I do realize i can solve it all with virtual functions, but to me it just seems like there could potentially be a buttload of virtual functions in the base class only because one of my derived classes has a method that needs to be accessed through a pointer-to-A array.
I dunno, just seems counter intuitive to me :|
to me it just seems like there could potentially be a buttload of virtual functions in the base class only because one of my derived classes has a method that needs to be accessed through a pointer-to-A array.


Why? If one of your functions needs to be virtual, make one of your functions virtual.
Last edited on
Sure sure but in a bigger program the base class would be cluttered with virtual functions. That's in a way fine with me, if that's the way to do it then I'll do it, but I just want to know if my design is at fault for needing these virtual functions.
virtual is a single word. I can't imagine anything involving less clutter.
Alright well I added a pure virtual function to be overridden by C, but now I can't use B because it has no overridden function for it. The thing is I don't want it to have it since in C it modifies a value that B does not have. What should I do to solve this?
Don't make it pure.
You need to get a bit more informed about abstract class : virtual functions and pure virtual functions. In a nutshell :
A class is called abstract if it has at least one pure virtual class. This means that this class can never be initantiated.
A virtual function means that a base class is providing an implementation for that function and any derived class can use the base class implementation OR provide their own implementations (override the base class behavior).
A pure virtual function means that any derived class HAS to implement this function if not they become abstract classes themselves and will not be able to be instantiated. It is a way to force the derived classes to provide their own behavior.
Have a quick look at this link:
http://www.cplusplus.com/doc/tutorial/polymorphism
Last edited on
Alright, I think I get it. If I make it non-pure, does that mean that the function in the bass class needs to be identical to the one in the derived class that overrides it? I.e same arguments?
If it is a virtual function then yes the prototype in the derived class has to be the same as the one in the base class.
Topic archived. No new replies allowed.