small confusion about the the C++ programming language by B.S.

P

puzzlecracker

on page 312 of 3ed SE edition last paragraph:
=======
.......
Calling a function using scope resolution operator:: as is done in
Manager::print()ensures that the virtual mechanism is not used.
Otherwise, Manager::print() would suffer an infinite recursion.\
....
...
==================
My question is WHY would you have an infinite recursion if,
hypothtically speaking, virtual mechanism were to be used?
 
V

Victor Bazarov

puzzlecracker said:
on page 312 of 3ed SE edition last paragraph:
=======
......
Calling a function using scope resolution operator:: as is done in
Manager::print()ensures that the virtual mechanism is not used.
Otherwise, Manager::print() would suffer an infinite recursion.\
...
..
==================
My question is WHY would you have an infinite recursion if,
hypothtically speaking, virtual mechanism were to be used?

It would keep calling itself, most likely (I don't have the book
handy).

V
 
E

ES Kim

puzzlecracker said:
on page 312 of 3ed SE edition last paragraph:
=======
......
Calling a function using scope resolution operator:: as is done in
Manager::print()ensures that the virtual mechanism is not used.
Otherwise, Manager::print() would suffer an infinite recursion.\
...
..
==================
My question is WHY would you have an infinite recursion if,
hypothtically speaking, virtual mechanism were to be used?

Think over what the following code would do if it were not for ::.

void Manager::print() const
{
print(); // meaning this->print();
}
 
D

David White

puzzlecracker said:
i still a bit confusing.

If the virtual mechanism were used every time, the same function would be
called every time (Manager::print), because that's the correct function for
the type of object. Therefore the call to Employee::print from inside
Manager::print cannot use the virtual mechanism. If it did, Manager::print
would call itself instead of Employee::print.

DW
 
P

puzzlecracker

David said:
If the virtual mechanism were used every time, the same function would be
called every time (Manager::print), because that's the correct function for
the type of object. Therefore the call to Employee::print from inside
Manager::print cannot use the virtual mechanism. If it did, Manager::print
would call itself instead of Employee::print.

DW

THANKS

it is so trivial; should read carifully next time...



I thought of something like that:

Derived D*=new Derived();
D->Base::print();

anyway, thx again
 

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

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,200
Latest member
SCPKatheri

Latest Threads

Top