Passing child class objects to std::function parameter
I have the following message class psuedo-code (third-party, cannot be changed).
messages.h:
class AbstractMessage {
// pure virtual
};
class MessageA : public AbstractMessage {
// implements everything
const static std::string messageName = "MessageA";
};
class MessageB : public AbstractMessage {
// implements everything
const static std::string messageName = "MessageB";
};
In my application code, I am attempting to write a function to register an external function to handle any message:
application.cpp:
bool register_handler(std::string messageType, std::function<void(AbstractMessage&)>messageHandler) {
// add the string and function to a map to be called
}
However, I need to be able to pass in MessageA or MessageB functions. For example:
main.cpp:
void processMessageA(MessageA &msg) {
// do something
}
Application myApp;
This becomes problematic when I attempt to do the following:
std::function<void(MessageA&)> f(processMessageA);
myApp.register_handler("MessageA", f);
MessageA should be a valid AbstractMessage via inheritance, but I get the following error:
'Error C2664: 'bool Application::register_handler(std::string,const std::function<void (AbstractMessage &)> &)': cannot convert argument 2 from 'std::function<void(MessageA &)>' to 'const std::function<void (AbstractMessage &)> &
Is this possible in C++? In Java I would use a functional interface, and pass any valid method via this::
. Inheritance would validate the method parameters.
c++ function inheritance stl
add a comment |
I have the following message class psuedo-code (third-party, cannot be changed).
messages.h:
class AbstractMessage {
// pure virtual
};
class MessageA : public AbstractMessage {
// implements everything
const static std::string messageName = "MessageA";
};
class MessageB : public AbstractMessage {
// implements everything
const static std::string messageName = "MessageB";
};
In my application code, I am attempting to write a function to register an external function to handle any message:
application.cpp:
bool register_handler(std::string messageType, std::function<void(AbstractMessage&)>messageHandler) {
// add the string and function to a map to be called
}
However, I need to be able to pass in MessageA or MessageB functions. For example:
main.cpp:
void processMessageA(MessageA &msg) {
// do something
}
Application myApp;
This becomes problematic when I attempt to do the following:
std::function<void(MessageA&)> f(processMessageA);
myApp.register_handler("MessageA", f);
MessageA should be a valid AbstractMessage via inheritance, but I get the following error:
'Error C2664: 'bool Application::register_handler(std::string,const std::function<void (AbstractMessage &)> &)': cannot convert argument 2 from 'std::function<void(MessageA &)>' to 'const std::function<void (AbstractMessage &)> &
Is this possible in C++? In Java I would use a functional interface, and pass any valid method via this::
. Inheritance would validate the method parameters.
c++ function inheritance stl
Let's say you manage to cast yourvoid(MessageA&)
to avoid(AbstractMessage&)
. What happens when you pass in aMessageB
to that function?
– super
Nov 26 '18 at 23:23
@super I‘m following. What would be the alternative or best practice for generic message handler mapping? I tried using a template, but had issues using it as a map key to determine which handler to call.
– Alan
Nov 26 '18 at 23:37
A better design would not need to know the exact type, the interface (AbstractMessage&
) should be enough. But if you third party code is preventing that, casting is your only option AFAIK.
– super
Nov 26 '18 at 23:46
@super Now I’m not following, my current scenario is I have the interface AbstractMessage&.
– Alan
Nov 27 '18 at 0:10
add a comment |
I have the following message class psuedo-code (third-party, cannot be changed).
messages.h:
class AbstractMessage {
// pure virtual
};
class MessageA : public AbstractMessage {
// implements everything
const static std::string messageName = "MessageA";
};
class MessageB : public AbstractMessage {
// implements everything
const static std::string messageName = "MessageB";
};
In my application code, I am attempting to write a function to register an external function to handle any message:
application.cpp:
bool register_handler(std::string messageType, std::function<void(AbstractMessage&)>messageHandler) {
// add the string and function to a map to be called
}
However, I need to be able to pass in MessageA or MessageB functions. For example:
main.cpp:
void processMessageA(MessageA &msg) {
// do something
}
Application myApp;
This becomes problematic when I attempt to do the following:
std::function<void(MessageA&)> f(processMessageA);
myApp.register_handler("MessageA", f);
MessageA should be a valid AbstractMessage via inheritance, but I get the following error:
'Error C2664: 'bool Application::register_handler(std::string,const std::function<void (AbstractMessage &)> &)': cannot convert argument 2 from 'std::function<void(MessageA &)>' to 'const std::function<void (AbstractMessage &)> &
Is this possible in C++? In Java I would use a functional interface, and pass any valid method via this::
. Inheritance would validate the method parameters.
c++ function inheritance stl
I have the following message class psuedo-code (third-party, cannot be changed).
messages.h:
class AbstractMessage {
// pure virtual
};
class MessageA : public AbstractMessage {
// implements everything
const static std::string messageName = "MessageA";
};
class MessageB : public AbstractMessage {
// implements everything
const static std::string messageName = "MessageB";
};
In my application code, I am attempting to write a function to register an external function to handle any message:
application.cpp:
bool register_handler(std::string messageType, std::function<void(AbstractMessage&)>messageHandler) {
// add the string and function to a map to be called
}
However, I need to be able to pass in MessageA or MessageB functions. For example:
main.cpp:
void processMessageA(MessageA &msg) {
// do something
}
Application myApp;
This becomes problematic when I attempt to do the following:
std::function<void(MessageA&)> f(processMessageA);
myApp.register_handler("MessageA", f);
MessageA should be a valid AbstractMessage via inheritance, but I get the following error:
'Error C2664: 'bool Application::register_handler(std::string,const std::function<void (AbstractMessage &)> &)': cannot convert argument 2 from 'std::function<void(MessageA &)>' to 'const std::function<void (AbstractMessage &)> &
Is this possible in C++? In Java I would use a functional interface, and pass any valid method via this::
. Inheritance would validate the method parameters.
c++ function inheritance stl
c++ function inheritance stl
asked Nov 26 '18 at 22:39
AlanAlan
269520
269520
Let's say you manage to cast yourvoid(MessageA&)
to avoid(AbstractMessage&)
. What happens when you pass in aMessageB
to that function?
– super
Nov 26 '18 at 23:23
@super I‘m following. What would be the alternative or best practice for generic message handler mapping? I tried using a template, but had issues using it as a map key to determine which handler to call.
– Alan
Nov 26 '18 at 23:37
A better design would not need to know the exact type, the interface (AbstractMessage&
) should be enough. But if you third party code is preventing that, casting is your only option AFAIK.
– super
Nov 26 '18 at 23:46
@super Now I’m not following, my current scenario is I have the interface AbstractMessage&.
– Alan
Nov 27 '18 at 0:10
add a comment |
Let's say you manage to cast yourvoid(MessageA&)
to avoid(AbstractMessage&)
. What happens when you pass in aMessageB
to that function?
– super
Nov 26 '18 at 23:23
@super I‘m following. What would be the alternative or best practice for generic message handler mapping? I tried using a template, but had issues using it as a map key to determine which handler to call.
– Alan
Nov 26 '18 at 23:37
A better design would not need to know the exact type, the interface (AbstractMessage&
) should be enough. But if you third party code is preventing that, casting is your only option AFAIK.
– super
Nov 26 '18 at 23:46
@super Now I’m not following, my current scenario is I have the interface AbstractMessage&.
– Alan
Nov 27 '18 at 0:10
Let's say you manage to cast your
void(MessageA&)
to a void(AbstractMessage&)
. What happens when you pass in a MessageB
to that function?– super
Nov 26 '18 at 23:23
Let's say you manage to cast your
void(MessageA&)
to a void(AbstractMessage&)
. What happens when you pass in a MessageB
to that function?– super
Nov 26 '18 at 23:23
@super I‘m following. What would be the alternative or best practice for generic message handler mapping? I tried using a template, but had issues using it as a map key to determine which handler to call.
– Alan
Nov 26 '18 at 23:37
@super I‘m following. What would be the alternative or best practice for generic message handler mapping? I tried using a template, but had issues using it as a map key to determine which handler to call.
– Alan
Nov 26 '18 at 23:37
A better design would not need to know the exact type, the interface (
AbstractMessage&
) should be enough. But if you third party code is preventing that, casting is your only option AFAIK.– super
Nov 26 '18 at 23:46
A better design would not need to know the exact type, the interface (
AbstractMessage&
) should be enough. But if you third party code is preventing that, casting is your only option AFAIK.– super
Nov 26 '18 at 23:46
@super Now I’m not following, my current scenario is I have the interface AbstractMessage&.
– Alan
Nov 27 '18 at 0:10
@super Now I’m not following, my current scenario is I have the interface AbstractMessage&.
– Alan
Nov 27 '18 at 0:10
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53490199%2fpassing-child-class-objects-to-stdfunction-parameter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53490199%2fpassing-child-class-objects-to-stdfunction-parameter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Let's say you manage to cast your
void(MessageA&)
to avoid(AbstractMessage&)
. What happens when you pass in aMessageB
to that function?– super
Nov 26 '18 at 23:23
@super I‘m following. What would be the alternative or best practice for generic message handler mapping? I tried using a template, but had issues using it as a map key to determine which handler to call.
– Alan
Nov 26 '18 at 23:37
A better design would not need to know the exact type, the interface (
AbstractMessage&
) should be enough. But if you third party code is preventing that, casting is your only option AFAIK.– super
Nov 26 '18 at 23:46
@super Now I’m not following, my current scenario is I have the interface AbstractMessage&.
– Alan
Nov 27 '18 at 0:10