Make a virtual function that returns a generic member function pointer











up vote
0
down vote

favorite












I want to write an abstract class named COMMUNICATOR that has a virtual function get_func().



class COMMUNICATOR : public QObject{
public:
virtual <what type???> get_func() = 0;
};


Now suppose INT_SENDER, INT_RECEIVER, STR_SENDER, and STR_RECEIVER all derive from COMMUNICATOR. They each provide their own implementation of get_func(), and they can be used like this:



COMMUNICATOR *int_sender = new INT_SENDER;
COMMUNICATOR *int_receiver = new INT_RECEIVER;

COMMUNICATOR *str_sender = new STR_SENDER;
COMMUNICATOR *str_receiver = new STR_RECEIVER;

//INT_SENDER::get_func() and INT_RECEIVER::get_func() both return a member function pointer of signature void(*)(int)
connect( int_sender, int_sender->get_func(), int_receiver, int_receiver->get_func() );

//STR_SENDER::get_func() and STR_RECEIVER::get_func() both return a member function pointer of signature void(*)(std::string)
connect( str_sender, str_sender->get_func(), str_receiver, str_receiver->get_func() );


What I want to achieve with this design are:



(1) To give all classes that derive from COMMUNICATOR the same interface: the get_func() function



(2) Using COMMUNICATOR and its derived classes does not require dealing with the signature of the function returned by get_func(). Instead, the function pointers are passed directly into connect();



Is there some sort of hack to achieve what I want, like making get_func() return some type-erasing wrapper object from which I can dump a function pointer into connect()? Or am I attempting the impossible here?










share|improve this question




















  • 3




    return type is part of the function signature, if you want to use overriding, you need to provide a common base for all return types
    – user463035818
    Nov 22 at 10:59










  • This looks like an awfully wrong way to say something simple, like sender.connect(receiver).
    – n.m.
    Nov 22 at 11:06










  • Try to use NVI Idom (Non-Virtual Interface) : make get_func public which calls a privated virtual one and override that latter in each derived subclass.
    – Blood-HaZaRd
    Nov 22 at 11:07










  • More to the point, why should functions returned by get_func have different signatures? Can you illustrate, with a (pseudo)code example, the importance of having different signatures?
    – n.m.
    Nov 22 at 11:11






  • 1




    I think qt connect function needs to know about the declaration of the functions (signal, slot and it params ) in order to connect them together. There is a moc program ran after to analyze all connect function then generate the real 'connect' codes.
    – Tung Le Thanh
    Nov 22 at 12:53















up vote
0
down vote

favorite












I want to write an abstract class named COMMUNICATOR that has a virtual function get_func().



class COMMUNICATOR : public QObject{
public:
virtual <what type???> get_func() = 0;
};


Now suppose INT_SENDER, INT_RECEIVER, STR_SENDER, and STR_RECEIVER all derive from COMMUNICATOR. They each provide their own implementation of get_func(), and they can be used like this:



COMMUNICATOR *int_sender = new INT_SENDER;
COMMUNICATOR *int_receiver = new INT_RECEIVER;

COMMUNICATOR *str_sender = new STR_SENDER;
COMMUNICATOR *str_receiver = new STR_RECEIVER;

//INT_SENDER::get_func() and INT_RECEIVER::get_func() both return a member function pointer of signature void(*)(int)
connect( int_sender, int_sender->get_func(), int_receiver, int_receiver->get_func() );

//STR_SENDER::get_func() and STR_RECEIVER::get_func() both return a member function pointer of signature void(*)(std::string)
connect( str_sender, str_sender->get_func(), str_receiver, str_receiver->get_func() );


What I want to achieve with this design are:



(1) To give all classes that derive from COMMUNICATOR the same interface: the get_func() function



(2) Using COMMUNICATOR and its derived classes does not require dealing with the signature of the function returned by get_func(). Instead, the function pointers are passed directly into connect();



Is there some sort of hack to achieve what I want, like making get_func() return some type-erasing wrapper object from which I can dump a function pointer into connect()? Or am I attempting the impossible here?










share|improve this question




















  • 3




    return type is part of the function signature, if you want to use overriding, you need to provide a common base for all return types
    – user463035818
    Nov 22 at 10:59










  • This looks like an awfully wrong way to say something simple, like sender.connect(receiver).
    – n.m.
    Nov 22 at 11:06










  • Try to use NVI Idom (Non-Virtual Interface) : make get_func public which calls a privated virtual one and override that latter in each derived subclass.
    – Blood-HaZaRd
    Nov 22 at 11:07










  • More to the point, why should functions returned by get_func have different signatures? Can you illustrate, with a (pseudo)code example, the importance of having different signatures?
    – n.m.
    Nov 22 at 11:11






  • 1




    I think qt connect function needs to know about the declaration of the functions (signal, slot and it params ) in order to connect them together. There is a moc program ran after to analyze all connect function then generate the real 'connect' codes.
    – Tung Le Thanh
    Nov 22 at 12:53













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to write an abstract class named COMMUNICATOR that has a virtual function get_func().



class COMMUNICATOR : public QObject{
public:
virtual <what type???> get_func() = 0;
};


Now suppose INT_SENDER, INT_RECEIVER, STR_SENDER, and STR_RECEIVER all derive from COMMUNICATOR. They each provide their own implementation of get_func(), and they can be used like this:



COMMUNICATOR *int_sender = new INT_SENDER;
COMMUNICATOR *int_receiver = new INT_RECEIVER;

COMMUNICATOR *str_sender = new STR_SENDER;
COMMUNICATOR *str_receiver = new STR_RECEIVER;

//INT_SENDER::get_func() and INT_RECEIVER::get_func() both return a member function pointer of signature void(*)(int)
connect( int_sender, int_sender->get_func(), int_receiver, int_receiver->get_func() );

//STR_SENDER::get_func() and STR_RECEIVER::get_func() both return a member function pointer of signature void(*)(std::string)
connect( str_sender, str_sender->get_func(), str_receiver, str_receiver->get_func() );


What I want to achieve with this design are:



(1) To give all classes that derive from COMMUNICATOR the same interface: the get_func() function



(2) Using COMMUNICATOR and its derived classes does not require dealing with the signature of the function returned by get_func(). Instead, the function pointers are passed directly into connect();



Is there some sort of hack to achieve what I want, like making get_func() return some type-erasing wrapper object from which I can dump a function pointer into connect()? Or am I attempting the impossible here?










share|improve this question















I want to write an abstract class named COMMUNICATOR that has a virtual function get_func().



class COMMUNICATOR : public QObject{
public:
virtual <what type???> get_func() = 0;
};


Now suppose INT_SENDER, INT_RECEIVER, STR_SENDER, and STR_RECEIVER all derive from COMMUNICATOR. They each provide their own implementation of get_func(), and they can be used like this:



COMMUNICATOR *int_sender = new INT_SENDER;
COMMUNICATOR *int_receiver = new INT_RECEIVER;

COMMUNICATOR *str_sender = new STR_SENDER;
COMMUNICATOR *str_receiver = new STR_RECEIVER;

//INT_SENDER::get_func() and INT_RECEIVER::get_func() both return a member function pointer of signature void(*)(int)
connect( int_sender, int_sender->get_func(), int_receiver, int_receiver->get_func() );

//STR_SENDER::get_func() and STR_RECEIVER::get_func() both return a member function pointer of signature void(*)(std::string)
connect( str_sender, str_sender->get_func(), str_receiver, str_receiver->get_func() );


What I want to achieve with this design are:



(1) To give all classes that derive from COMMUNICATOR the same interface: the get_func() function



(2) Using COMMUNICATOR and its derived classes does not require dealing with the signature of the function returned by get_func(). Instead, the function pointers are passed directly into connect();



Is there some sort of hack to achieve what I want, like making get_func() return some type-erasing wrapper object from which I can dump a function pointer into connect()? Or am I attempting the impossible here?







c++ qt design-patterns c++14 type-erasure






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 11:21

























asked Nov 22 at 10:57









megamonium

404




404








  • 3




    return type is part of the function signature, if you want to use overriding, you need to provide a common base for all return types
    – user463035818
    Nov 22 at 10:59










  • This looks like an awfully wrong way to say something simple, like sender.connect(receiver).
    – n.m.
    Nov 22 at 11:06










  • Try to use NVI Idom (Non-Virtual Interface) : make get_func public which calls a privated virtual one and override that latter in each derived subclass.
    – Blood-HaZaRd
    Nov 22 at 11:07










  • More to the point, why should functions returned by get_func have different signatures? Can you illustrate, with a (pseudo)code example, the importance of having different signatures?
    – n.m.
    Nov 22 at 11:11






  • 1




    I think qt connect function needs to know about the declaration of the functions (signal, slot and it params ) in order to connect them together. There is a moc program ran after to analyze all connect function then generate the real 'connect' codes.
    – Tung Le Thanh
    Nov 22 at 12:53














  • 3




    return type is part of the function signature, if you want to use overriding, you need to provide a common base for all return types
    – user463035818
    Nov 22 at 10:59










  • This looks like an awfully wrong way to say something simple, like sender.connect(receiver).
    – n.m.
    Nov 22 at 11:06










  • Try to use NVI Idom (Non-Virtual Interface) : make get_func public which calls a privated virtual one and override that latter in each derived subclass.
    – Blood-HaZaRd
    Nov 22 at 11:07










  • More to the point, why should functions returned by get_func have different signatures? Can you illustrate, with a (pseudo)code example, the importance of having different signatures?
    – n.m.
    Nov 22 at 11:11






  • 1




    I think qt connect function needs to know about the declaration of the functions (signal, slot and it params ) in order to connect them together. There is a moc program ran after to analyze all connect function then generate the real 'connect' codes.
    – Tung Le Thanh
    Nov 22 at 12:53








3




3




return type is part of the function signature, if you want to use overriding, you need to provide a common base for all return types
– user463035818
Nov 22 at 10:59




return type is part of the function signature, if you want to use overriding, you need to provide a common base for all return types
– user463035818
Nov 22 at 10:59












This looks like an awfully wrong way to say something simple, like sender.connect(receiver).
– n.m.
Nov 22 at 11:06




This looks like an awfully wrong way to say something simple, like sender.connect(receiver).
– n.m.
Nov 22 at 11:06












Try to use NVI Idom (Non-Virtual Interface) : make get_func public which calls a privated virtual one and override that latter in each derived subclass.
– Blood-HaZaRd
Nov 22 at 11:07




Try to use NVI Idom (Non-Virtual Interface) : make get_func public which calls a privated virtual one and override that latter in each derived subclass.
– Blood-HaZaRd
Nov 22 at 11:07












More to the point, why should functions returned by get_func have different signatures? Can you illustrate, with a (pseudo)code example, the importance of having different signatures?
– n.m.
Nov 22 at 11:11




More to the point, why should functions returned by get_func have different signatures? Can you illustrate, with a (pseudo)code example, the importance of having different signatures?
– n.m.
Nov 22 at 11:11




1




1




I think qt connect function needs to know about the declaration of the functions (signal, slot and it params ) in order to connect them together. There is a moc program ran after to analyze all connect function then generate the real 'connect' codes.
– Tung Le Thanh
Nov 22 at 12:53




I think qt connect function needs to know about the declaration of the functions (signal, slot and it params ) in order to connect them together. There is a moc program ran after to analyze all connect function then generate the real 'connect' codes.
– Tung Le Thanh
Nov 22 at 12:53

















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',
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429420%2fmake-a-virtual-function-that-returns-a-generic-member-function-pointer%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429420%2fmake-a-virtual-function-that-returns-a-generic-member-function-pointer%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

How to ignore python UserWarning in pytest?

Alexandru Averescu