polymorphic method override

S

stf

Why does this code not compile?

#include <iostream>

using namespace std;

class A {
public:
virtual void myMethod(const string& s) const;
};

class B : public A {
public:
virtual void myMethod(int x) const;
};

void A::myMethod(const string& s) const {
cerr << "(A) myMethod: s is: " << s << endl;
}
void B::myMethod(int x) const {
cerr << "(B) myMethod: x = " << x << endl;
}

int main() {
std::string s("my string one");
B b;
b.myMethod(s);
return 0;
}

The error is:

$ g++ -Wall ./test-5.cpp -o test-5.out ./test-5.cpp: In function
‘int main()’:
../test-5.cpp:25: error: no matching function for call to
‘B::myMethod(std::string&)’
../test-5.cpp:18: note: candidates are: virtual void B::myMethod(int)
const

It looks that if I change:

b.myMethod(s);

to:

b.A::myMethod(s);

then it works OK. However this is bulls*t of course, because later
if someone implements B::myMethod(const string& s) const, the
purpose of virtual method will be defeated.

How to cope with this??!

Thanks!

STF
 
A

Alf P. Steinbach

Why does this code not compile?

#include<iostream>

using namespace std;

class A {
public:
virtual void myMethod(const string& s) const;
};

class B : public A {
public:
virtual void myMethod(int x) const;
};

void A::myMethod(const string& s) const {
cerr<< "(A) myMethod: s is: "<< s<< endl;
}
void B::myMethod(int x) const {
cerr<< "(B) myMethod: x = "<< x<< endl;
}

int main() {
std::string s("my string one");
B b;
b.myMethod(s);
return 0;
}

The error is:

$ g++ -Wall ./test-5.cpp -o test-5.out ./test-5.cpp: In function
‘int main()’:
./test-5.cpp:25: error: no matching function for call to
‘B::myMethod(std::string&)’
./test-5.cpp:18: note: candidates are: virtual void B::myMethod(int)
const

This is a FAQ.

It's often a good idea to check the FAQ (or a textbook) before posting.

See http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.9


Cheers & hth.,

- Alf
 
S

Salt_Peter

Why does this code not compile?

#include <iostream>

using namespace std;

class A {
   public:
       virtual void myMethod(const string& s) const;

};

class B : public A {
   public:
       virtual void myMethod(int x) const;

};

void A::myMethod(const string& s) const {
   cerr << "(A) myMethod: s is: " << s << endl;}

void B::myMethod(int x) const {
   cerr << "(B) myMethod: x = " << x << endl;

}

int main() {
   std::string s("my string one");
   B b;
   b.myMethod(s);
   return 0;

}

The error is:

$ g++ -Wall ./test-5.cpp -o test-5.out ./test-5.cpp: In function
‘int main()’:
./test-5.cpp:25: error: no matching function for call to
‘B::myMethod(std::string&)’
./test-5.cpp:18: note: candidates are: virtual void B::myMethod(int)
const

It looks that if I change:

b.myMethod(s);

to:

b.A::myMethod(s);

then it works OK. However this is bulls*t of course, because later
if someone implements B::myMethod(const string& s) const, the
purpose of virtual method will be defeated.

How to cope with this??!

what you get is an overide, not an overload.
The result you get is expected.
Now try it with a pointer to base.
 

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
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top