How to access derived member functions from base class.

I want to access some member function in a project I am working on. These member function are in a derived class that get me private data from that object class. I thought I could just call the derived member class function but it is getting me an error. With the sample code provided. How can I get the X from the derived member function?

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class Base {

public:
    Base(){};

};

class DervidedOne : public Base {

public:
    DervidedOne();
    DervidedOne(int xIn);
    void SetX(int xIn);
    int GetX();

private:
    int x;

};

DervidedOne::DervidedOne(){
    x = 0;
}

DervidedOne::DervidedOne(int xIn){
    x = xIn;
}

void DervidedOne::SetX(int newXIn) {
    x = newXIn;
}

int DervidedOne::GetX() {
    return x;
}

int main(){

    Base* basePtr = nullptr;
    vector <Base*> v;
    new vector <Base()>;

    Base* dervidedOne = new class DervidedOne(1);
    v.push_back(dervidedOne);

    Base* dervidedTwo = new class DervidedOne(1);
    v.push_back(dervidedTwo);

    Base* dervidedThree = new class DervidedOne(1);
    v.push_back(dervidedThree);

    for (int i = 0; i < v.size(); ++i) {

        // Error: No member named "SetX" in 'Base'
        v.at(i) -> SetX(0);

    }

    return 0;

}


I'll check this post regularly throughout the day. Thanks!
1
2
3
auto casted = dynamic_cast<DervidedOne *>(v[i]);
if (casted) //If this check fails, v[i] doesn't point to an instance of DervidedOne.
    casted->SetX(0);


By the way, this is unrelated to your question, but on line 41 you're not doing anything, you're just leaking some memory. vector <Base*> v; already creates an instance of a vector; you don't need to do anything else to initialize it.
Last edited on
If you use the interface of your base class, you are forced to use merely the methods which are defined in that class. Class Base doesn't know about any methods you have defined in your class DerivedOne but not in class Base.

A solution could be, that you define your GetX and SetX methods also in your base class If you do not want that objects of your base class could be created, then you could make GetX and SetX as pure virtual methods.

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <vector>
using namespace std;

class Base {

public:
    Base(){};
    // Both methods will be pure virtual:
    virtual void SetX(int) = 0;
    virtual int GetX() = 0;

};

class DervidedOne : public Base {

public:
    DervidedOne();
    DervidedOne(int xIn);
    virtual void SetX(int xIn) override;
    virtual int GetX() override;

private:
    int x;

};

DervidedOne::DervidedOne(){
    x = 0;
}

DervidedOne::DervidedOne(int xIn){
    x = xIn;
}

void DervidedOne::SetX(int newXIn) {
    x = newXIn;
}

int DervidedOne::GetX() {
    return x;
}

int main(){

    Base* basePtr = nullptr;
    vector <Base*> v;
    // new vector <Base()>;

    Base* dervidedOne = new class DervidedOne(1);
    v.push_back(dervidedOne);

    Base* dervidedTwo = new class DervidedOne(1);
    v.push_back(dervidedTwo);

    Base* dervidedThree = new class DervidedOne(1);
    v.push_back(dervidedThree);

    for (int i = 0; i < v.size(); ++i) {
        v.at(i) -> SetX(0);

    }

    return 0;

}
Last edited on
@Hellos

Ah! This works! Thanks!

About you unrelated questions. Should take out the new vector <Base()> to prevent memory leaks then?
If you want to call SetX() or GetX() on all values in vector v then you expect all values in v to be DerivedOne, not Base. So why not just declare v as vector<DerivedOne*> and let the compiler enforce the requirement? Otherwise you're taking on the responsibility yourself. If you mess up, you won't know until you get some weird runtime error. If the compiler does it, you'll know about an error at compile time.
Topic archived. No new replies allowed.