Created
November 27, 2016 04:11
-
-
Save ejrh/dc1c59befcb203699a30791ab72693ac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <boost/shared_ptr.hpp> | |
#include <boost/make_shared.hpp> | |
class Message { | |
}; | |
class Receiver { | |
public: | |
virtual ~Receiver() { } | |
virtual void receive(Message *msg) = 0; | |
void receive(const boost::shared_ptr<Message>& msg) { }; | |
void receive1(const boost::shared_ptr<Message>& msg) { }; | |
}; | |
class Publisher: public Receiver { | |
public: | |
virtual ~Publisher(); | |
virtual void receive(Message *msg) { } | |
}; | |
int main() { | |
auto m = new Message(); | |
auto m2 = boost::make_shared<Message>(); | |
Publisher p; | |
p.receive(m); // OK - finds virtual method | |
p.receive1(m2); // OK - method isn't overloaded | |
Receiver *r = &p; | |
r->receive(m2); // OK - finds overloaded method | |
p.receive(m2); // COMPILE ERROR - see below | |
/* | |
$ clang -c -Wall -std=c++11 test2.cpp | |
test2.cpp:33:15: error: no viable conversion from 'boost::shared_ptr<Message>' to 'Message *' | |
p.receive(m2); // COMPILE ERROR | |
^~ | |
test2.cpp:19:35: note: passing argument to parameter 'msg' here | |
virtual void receive(Message *msg) { } | |
^ | |
1 error generated. | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment