Enable Polymorphism on Run()

I

Immortal Nephi

I am trying to enable polymorphism. I created three derived classes
as B1, B2, and B3. class B1, B2, and B3 are derived from class A.
Only one virtual function Run() is invoked to select class B1, B2, or
B3 from class A.
I do not want to reassign reference of class B1, B2, and B3 to class
A pointer inside main() body. They can be done inside Run() body.
When you compile and run my source code, Run() is always invoked to
class B1 each time.
Please assist me to make the correction. Thanks for your advice.

#include <iostream>

class A
{
public:

A() : regA(0), regX(0), regY(0)
{
cout << "A()\n";
}

~A()
{
cout << "~A()\n";
}

void virtual Run(A* pa, A& ra)
{
cout << "A::Run()\n";
}

protected:
int regA;
int regX;
int regY;
};

class B1 : public A
{
public:

B1() : A()
{
cout << "B1()\n";
}

~B1()
{
cout << "~B1()\n";
}

void virtual Run(A* pa, A& ra)
{
cout << "B1::Run()\n";
pa = &ra;
}
};

class B2 : public A
{
public:

B2() : A()
{
cout << "B2()\n";
}

~B2()
{
cout << "~B2()\n";
}

void virtual Run(A* pa, A& ra)
{
cout << "B2::Run()\n";
pa = &ra;
}
};

class B3 : public A
{
public:

B3() : A()
{
cout << "B3()\n";
}

~B3()
{
cout << "~B3()\n";
}

void virtual Run(A* pa, A& ra)
{
cout << "B3::Run()\n";
pa = &ra;
}
};

int main()
{
B1 b1;
B2 b2;
B3 b3;
A* pa = &b1;

pa->Run(pa, b2); // invoke B1:Run() & Reassign B2 to Run()
pa->Run(pa, b3); // invoke B2:Run() & Reassign B3 to Run()
pa->Run(pa, b1); // invoke B3:Run() & Reassign B1 to Run()

pa->Run(pa, b2); // invoke B1:Run() & Reassign B2 to Run()
pa->Run(pa, b3); // invoke B2:Run() & Reassign B3 to Run()
pa->Run(pa, b1); // invoke B3:Run() & Reassign B1 to Run()

system("system");

return 0;
}
 
G

gw7rib

        I am trying to enable polymorphism.  I created three derived classes
as B1, B2, and B3.  class B1, B2, and B3 are derived from class A.
Only one virtual function Run() is invoked to select class B1, B2, or
B3 from class A.
        I do not want to reassign reference of class B1, B2, and B3 to class
A pointer inside main() body.  They can be done inside Run() body.
When you compile and run my source code, Run() is always invoked to
class B1 each time.
        Please assist me to make the correction.  Thanks for your advice.

You'll probably kick yourself...
#include <iostream>

class A
{
public:

        A() : regA(0), regX(0), regY(0)
        {
                cout << "A()\n";
        }

        ~A()
        {
                cout << "~A()\n";
        }

        void virtual Run(A* pa, A& ra)
        {
                cout << "A::Run()\n";
        }

protected:
        int regA;
        int regX;
        int regY;

};

class B1 : public A
{
public:

        B1() : A()
        {
                cout << "B1()\n";
        }

        ~B1()
        {
                cout << "~B1()\n";
        }

        void virtual Run(A* pa, A& ra)
        {
                cout << "B1::Run()\n";
                pa = &ra;

Here's your problem. pa is passed by value, so changing the value of
pa in this function doesn't change the value in the calling function.

I think what you want is to replace "void virtual Run(A* pa, A& ra)"
with "void virtual Run(A*& pa, A& ra)" all through your code. Passing
by reference means that the line just above will change the variable
in the calling routine as well.


(If this doesn't make sense to you, I suggest reading question 4.8 of
the C FAQ, at http://c-faq.com/ptrs/passptrinit.html But using C++
gives you more ways to deal with the problem.)

Hope that helps.
Paul.
 
V

Victor Bazarov

Immortal said:
I am trying to enable polymorphism. I created three derived classes
as B1, B2, and B3. class B1, B2, and B3 are derived from class A.
Only one virtual function Run() is invoked to select class B1, B2, or
B3 from class A.
I do not want to reassign reference of class B1, B2, and B3 to class
A pointer inside main() body. They can be done inside Run() body.
When you compile and run my source code, Run() is always invoked to
class B1 each time.
Please assist me to make the correction. Thanks for your advice.

#include <iostream>

class A
{
public:

A() : regA(0), regX(0), regY(0)
{
cout << "A()\n";
}

~A()
{
cout << "~A()\n";
}

void virtual Run(A* pa, A& ra)

If you are going to assign to 'pa' and hope to keep the change outside
of this function, 'pa' cannot be passed by value, otherwise the new
value (after assignment) only exists inside 'Run' function and the
caller does not know about it. Try printing the value of 'pa' in 'main'
right after each call to 'Run'...
{
cout << "A::Run()\n";
}

protected:
int regA;
int regX;
int regY;
};
[..]

V
 
I

Immortal Nephi

If you are going to assign to 'pa' and hope to keep the change outside
of this function, 'pa' cannot be passed by value, otherwise the new
value (after assignment) only exists inside 'Run' function and the
caller does not know about it.  Try printing the value of 'pa' in 'main'
right after each call to 'Run'...

Hi, V

You are correct about pass by value. I am unaware of A* is a
reference, but it should be A*&. Thanks for the tip. I have one
problem.
/* OK */ pa->Run(pa, b2); // invoke B1:Run() & Reassign B2 to Run()
/* OK */ pa->Run(pa, b3); // invoke B2:Run() & Reassign B3 to Run()
/* NO */ pa->Run(pa, b1); // invoke B3:Run() & Reassign B1 to Run()

After you reassign b3 to pa, third function of pa->Run(pa, b1) above
will invoke to A::Run() instead of B3::Run(). Please give me a clue
what is going wrong?

Thanks...
 
N

Neelesh

Hi, V

You are correct about pass by value.  I am unaware of A* is a
reference, but it should be A*&.  Thanks for the tip.  I have one
problem.




/* OK */   pa->Run(pa, b2); // invoke B1:Run() & Reassign B2 to Run()
/* OK */   pa->Run(pa, b3); // invoke B2:Run() & Reassign B3 to Run()
/* NO */   pa->Run(pa, b1); // invoke B3:Run() & Reassign B1 to Run()

After you reassign b3 to pa, third function of pa->Run(pa, b1) above
will invoke to A::Run() instead of B3::Run().  Please give me a clue
what is going wrong?

Thanks...

did you forget to change A* to A*& in B3::Run ?
 
H

Hao Wang

Immortal Nephi said:
I am trying to enable polymorphism. I created three derived classes
as B1, B2, and B3. class B1, B2, and B3 are derived from class A.
Only one virtual function Run() is invoked to select class B1, B2, or
B3 from class A.
I do not want to reassign reference of class B1, B2, and B3 to class
A pointer inside main() body. They can be done inside Run() body.
When you compile and run my source code, Run() is always invoked to
class B1 each time.
Please assist me to make the correction. Thanks for your advice.

#include <iostream>

class A
{
public:

A() : regA(0), regX(0), regY(0)
{
cout << "A()\n";
}

~A()
{
cout << "~A()\n";
}

void virtual Run(A* pa, A& ra)
{
cout << "A::Run()\n";
}

protected:
int regA;
int regX;
int regY;
};

class B1 : public A
{
public:

B1() : A()
{
cout << "B1()\n";
}

~B1()
{
cout << "~B1()\n";
}

void virtual Run(A* pa, A& ra)
{
cout << "B1::Run()\n";
pa = &ra;
}
};

class B2 : public A
{
public:

B2() : A()
{
cout << "B2()\n";
}

~B2()
{
cout << "~B2()\n";
}

void virtual Run(A* pa, A& ra)
{
cout << "B2::Run()\n";
pa = &ra;
}
};

class B3 : public A
{
public:

B3() : A()
{
cout << "B3()\n";
}

~B3()
{
cout << "~B3()\n";
}

void virtual Run(A* pa, A& ra)
{
cout << "B3::Run()\n";
pa = &ra;
}
};

int main()
{
B1 b1;
B2 b2;
B3 b3;
A* pa = &b1;

pa->Run(pa, b2); // invoke B1:Run() & Reassign B2 to Run()
pa->Run(pa, b3); // invoke B2:Run() & Reassign B3 to Run()
pa->Run(pa, b1); // invoke B3:Run() & Reassign B1 to Run()

pa->Run(pa, b2); // invoke B1:Run() & Reassign B2 to Run()
pa->Run(pa, b3); // invoke B2:Run() & Reassign B3 to Run()
pa->Run(pa, b1); // invoke B3:Run() & Reassign B1 to Run()

system("system");

return 0;
}

You some thing like

void virtual Run(A*& pa, A& ra)

as the prototype for Run?
That idea is not good.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top