Why "const" break the Polymorphism?

J

July

Hello!
consider the following code:

class A {
public:
virtual void f() const{
cout << "A::f()" << endl;
}
};

class B : public A {
public:
void f() {
cout << "B::f()" << endl;
}
};

int main()
{
B b;
A *pa = &b;
pa->f();
}

the output is A::f()
neither b nor pa is const , why A::f() get called?

Will somebody explain this?

Thanks
 
L

Luke Meyers

July said:
class A {
public:
virtual void f() const;{
cout << "A::f()" << endl;
}
};

class B : public A {
public:
void f() {
cout << "B::f()" << endl;
}
};

int main()
{
B b;
A *pa = &b;
pa->f();
}

the output is A::f()
neither b nor pa is const , why A::f() get called?

B::f() does not override A::f(), because the const modifier makes the
signatures different. It's the same as if you had different argument
lists.

Luke
 
G

Guest

July said:
Will somebody explain this?

Compare those two signatures:

virtual void A::f() const;
void B::f();

or without virtual keyword for better understanding:
void A::f() const;
void B::f();

Do those signatures declare the same type of function?
Does B::f override A::f?

Cheers
 
M

mlimber

Luke said:
B::f() does not override A::f(), because the const modifier makes the
signatures different. It's the same as if you had different argument
lists.

IOW, B::f() is not polymorphically accessible to a pointer of type A
since B::f() does not override a virtual function of A. In fact, the
pointer pa can never call B::f() at all, no matter what the const
qualifiers are on pa.

On the other hand, it is curious that the compiler does not complain
about the cv-qualifiers of pa. I can't see why it would even let you
invoke A::f() since pa is non-const.

Cheers! --M
 
N

Neelesh Bodas

mlimber said:
On the other hand, it is curious that the compiler does not complain
about the cv-qualifiers of pa. I can't see why it would even let you
invoke A::f() since pa is non-const.

I didnot get what you want to say. What is the problem in invoking a
const member function on a non-const object?
 
E

Earl Purple

Mateusz said:
Compare those two signatures:

virtual void A::f() const;
void B::f();

or without virtual keyword for better understanding:
void A::f() const;
void B::f();

Do those signatures declare the same type of function?
Does B::f override A::f?
No, in fact B::f hides A::f. That means if you have a const pointer or
reference to B and call f() it will fail to compile because it does not
inherit A::f() const
..
 
M

mlimber

Neelesh said:
I didnot get what you want to say. What is the problem in invoking a
const member function on a non-const object?

Sorry. I wasn't thinking clearly. Now retreating to screw head on
straight.

Cheers! --M
 
G

Guest

Earl said:
No, in fact B::f hides A::f. That means if you have a const pointer or
reference to B and call f() it will fail to compile because it does not
inherit A::f() const

That's what I was trying to explain.
Seems, I wasn't very clear.

Cheers
 
M

Massimo Soricetti

Earl Purple ha scritto:
No, in fact B::f hides A::f. That means if you have a const pointer or
reference to B and call f() it will fail to compile because it does not
inherit A::f() const

(noob question)
This means that a virtual function must never be declared const?
OR that if a virtual function is declared const, every function trying
to override it must be declared const also?
 
G

Gavin Deane

Massimo said:
Earl Purple ha scritto:

(noob question)
This means that a virtual function must never be declared const?
OR that if a virtual function is declared const, every function trying
to override it must be declared const also?

The second one. There is absolutely no problem with const virtual
functions.

The issue is that the const modifier forms part of the function's type.
And for virtual function overriding to work, the type of the derived
class function must match the type of the base class function exactly.
This means the return types must be the same [*], the number and types
of the parameters must be the same and the const-ness and volatile-ness
must be the same.

[*] With the exception of covariant return types.
http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8

Gavin Deane
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top