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?
c++ qt design-patterns c++14 type-erasure
|
show 5 more comments
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?
c++ qt design-patterns c++14 type-erasure
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, likesender.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
|
show 5 more comments
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?
c++ qt design-patterns c++14 type-erasure
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
c++ qt design-patterns c++14 type-erasure
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, likesender.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
|
show 5 more comments
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, likesender.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
|
show 5 more comments
active
oldest
votes
active
oldest
votes
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.
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.
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%2f53429420%2fmake-a-virtual-function-that-returns-a-generic-member-function-pointer%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
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