Class/Subclass as Arguments

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
#include <cstdlib>
#include <iostream>
using namespace std;

class c1
{
    protected:
        int i1;
    public:
        c1(){};
        c1(int i3):i1(i3){};
        void f1( c1 source ) {i1=source.geti1();};
        int geti1(){return i1;};
};
class c2: public c1
{
    public:
        c2(int i5): c1(i5) {};
        
};
int main(int argc, char *argv[])
{
    
    c1 t1(4);
    c2 t2(10);
    cout << t1.geti1() << "\n";
    t1.f1(t2);
    cout << t1.geti1() << "\n";
    system("PAUSE");
    return EXIT_SUCCESS;
};

This works. 100%.

but this doesn't

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cstdlib>
#include <iostream>
using namespace std;

class c1
{
    protected:
        int i1;
    public:
        c1(){};
        c1(int i3):i1(i3){};
        void f1( c1 source ) {i1=source.geti1();};
        int geti1(){return i1;};
};
class c2: public c1
{
    public:
        c2(): c1(66) {}
        
};
int main(int argc, char *argv[])
{
    
    c1 t1(4);
    c2 t2();---------------------------------
    cout << t1.geti1() << "\n";
    t1.f1(t2);
    cout << t1.geti1() << "\n";
    system("PAUSE");
    return EXIT_SUCCESS;
};





i was using the 2nd way to do this basically. and turns out
c2 t2();
should have been this
c2 t2;

that was my problem. c++ is odd. Im used to actionscript 3
Last edited on
closed account (z05DSL3A)
1
2
3
c1 test1(2);
c2 test2(3);
c1.f1(c2); //  <----? 


Do you mean test1.f1(test2);?
yes. that is what i meant. thanks.

problem persists.
Last edited on
If you want to use polymorphism your argument must be a pointer or a reference.
Is that polymorphism. I want to use the exact same public member function (f1) on ALL subclasses of the c1 class. Im not wanting to change it in anyway. I just need it to acecpt all subclasses of c1 as the argument.

Actually i just wrong a basic program that persisted of just this code and it worked 100%. i must have an odd bug in my real program thats causing this error to show up.
Last edited on
Topic archived. No new replies allowed.