unexpected class/pointer behavior

After a few hours of questioning myself why my button doesn't work in one case but works in another , using the same function I simplified the problem to something from c++ :

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
#include <iostream>

using namespace std;

class first{
    public:
    first *pointer;
    void print(){
        cout<<"first\n";
    }
    void point(){
        pointer->print();
    }
};
class second:public first{
    public:
    void print(){
        cout<<"second\n";
    }
};

int main(){
    first unu;
    second doi;
    unu.pointer=&doi;
    doi.pointer=&unu;
    unu.print();
    unu.point();
    doi.print();
    doi.point();
    doi.pointer=&doi;
    doi.point();
}

Output:
1
2
3
4
5
first
first
second
first
first


I would have expected different:
1
2
3
4
5
first //just calls print
second //calls te overwritten function in second (doi)
second //just calls
first //calls the function from first (unu)
second //calls the overwritten function in second (doi) 

I think the language will not change for me (Sad) what should I do to get what I expect/want...?

edit: just made everything virtual and it worked... this is it?
edit2: just made the functions virtual in my main program and it didn't work:(
Last edited on
nonvirtual functions use the type at the time of the call to determine which class's function to call. Therefore if you have a first pointer, calling print, it will always call first::print, even if first really points to a second object.

Virtual functions are the opposite. When a function is declared virtual, the class creates a function pointer which tells which function to call. Therefore if you have a 'first' pointer which really points to a 'second' object, then second::print is called instead of first::print.

explained:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// if print is non-virtual
    first unu;
    second doi;
    unu.pointer=&doi;
    doi.pointer=&unu;

    unu.print();   // unu is of type first, therefore print "first"
    unu.point();  // unu.pointer is of type first*, therefore print "first"
    doi.print();  // doi is of type second, therefore print "second"
    doi.point();  // doi.pointer is of type first*, therefore print "first"
    doi.pointer=&doi;
    doi.point();  // doi.pointer is still/always of type first*, so still print "first"

// if print is virtual
    unu.print();  // unu is *really* of type first, print "first"
    unu.point();  // unu.pointer points to doi, which is of type second, so print "second"
    doi.print();  // doi is of type second, so print "second"
    doi.point();  // doi.pointer points to unu, which is of type first, so print "first"
    doi.pointer=&doi;
    doi.point();  // doi.pointer now points to doi, which is of type second, print "second" 


EDIT: but I guess you already figured all that out XD

anyway can't help with your main program with the info you provided here
Last edited on
Dam that was a huge pain in the darkness to fix.

I made them virtual but they had a parameter witch was a reference to an abstract class in a library (to make my engine or what ever more general) and I was giving a normal parameter, a derivate of the abstract one and it was causing confusion for the compiler and even with the virtualness it was calling the first function...

I had to strip the entire code line by line and than to see that if I remove something that does absolutelly nothing in the minimal code it will work, otherwise even the minimal example didn't work.

Hmm is that behavior what its supposed to be?
I'll be trying to make a minimal example of that, if I remeber how...
Topic archived. No new replies allowed.