LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-14-2012, 08:37 PM   #1
AWSmith
Member
 
Registered: Jun 2012
Posts: 32

Rep: Reputation: Disabled
Can I use a non-virtual function with a template class?


K... So I am writing a new TCP based protocol with the socket API from alhem.net. Fine. 8)

My problem is that one of the really cool classes it has is implemented as a TEMPLATE. I want to use some custom classes in MY class. Yea. Let me give an example:

SocketHandler h;
ListenSocket<CMServerSocket> l(h);
l.functioninCMServerSocketThatIsNotaVirtualofListenSocketsTargetClassWhichiscalledSocket(int someinteger);

the last line fails and the compiler complains that the function is not in the class ListenSocket<CMServerSocket>.

So.... If I want to have some functions in CMServerSocket that are NOT virtuals (ie. OnRead, OnConnect), how can I do it?
 
Old 07-16-2012, 08:29 AM   #2
a4z
Member
 
Registered: Feb 2009
Posts: 862

Rep: Reputation: 325Reputation: 325Reputation: 325Reputation: 325
don't know alhem.net,

but try like this

Code:
template<typename T>
class MyListenSocket : public ListenSocket<T> {
public:
... what ever is required like constructors ...

void whatYouImplement(int whatever){...}
};

MyListenSocket<CMServerSocket> l(h);

l.whatYouImplement(type whatever);
 
1 members found this post helpful.
Old 07-16-2012, 06:45 PM   #3
AWSmith
Member
 
Registered: Jun 2012
Posts: 32

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by a4z View Post
don't know alhem.net,

but try like this

Code:
template<typename T>
class MyListenSocket : public ListenSocket<T> {
public:
... what ever is required like constructors ...

void whatYouImplement(int whatever){...}
};

MyListenSocket<CMServerSocket> l(h);


l.whatYouImplement(type whatever);

Many thanks to you. Yes, that is sadly the only way I can see to do it. I just don't want to have to modify the API I am using. Sure, its open source so I COULD add the template function. But I am no library programmer so I am not sure if it would end up breaking something else. BUT...... I suspect that you are correct in that this is the only way to add a function to a template class. It just seems like a waste of object oriented mental energy to create a class that you can not use to interact with other classes.

Or maybe I just need to do away with the template and re-write it as a solid class... or maybe use a different class from their API.
 
Old 07-16-2012, 06:59 PM   #4
AWSmith
Member
 
Registered: Jun 2012
Posts: 32

Original Poster
Rep: Reputation: Disabled
A4z:

I looked at the header file for the template class and the template is the only thing it declares AND it includes the implementation as well so its easy enough to modify to allow my own functions in it. Its even short enough that I may just rewrite my class to include the bits of the other one that I need.

Many many thanks for the nod in this direction.
 
Old 07-17-2012, 12:07 AM   #5
a4z
Member
 
Registered: Feb 2009
Posts: 862

Rep: Reputation: 325Reputation: 325Reputation: 325Reputation: 325
it is not sadly that this (derivate from a template) is the only way to extend functionality.
that is how it works and normally you prefer this over changing the original source.

Last edited by a4z; 07-17-2012 at 12:08 AM.
 
1 members found this post helpful.
Old 07-17-2012, 05:45 PM   #6
AWSmith
Member
 
Registered: Jun 2012
Posts: 32

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by a4z View Post
it is not sadly that this (derivate from a template) is the only way to extend functionality.
that is how it works and normally you prefer this over changing the original source.
As you can probably guess, I am a hobbyist programmer. Lots of folks a LOT better at it than I am. I do it for fun really, not to make a living. Its always been a bit of a passion and something I have started doing again. I did a little bit with the freeradius project way back when but it was more diving through code to debug a process. Thats what made me realize how powerful a group of like minded folks can be.

And I have given your comment some serious consideration and I actually agree. I consdered the template class to be 'part of the API' and hence I did not want to mess with it. BUT.... maybe the developers CREATED this template class for the reasons you gave.

Again, many thanks for your input.
 
  


Reply

Tags
c++


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++: How to put a pointer to a function, in a class template, without typedef'ing it? TheIndependentAquarius Programming 3 03-05-2011 07:52 PM
c++: template member function used in a different templated class matze Programming 5 04-10-2008 09:26 AM
"undefined reference" to a template class function btb Programming 3 08-25-2005 05:02 PM
function prototypes in template class' (c++) qwijibow Programming 4 12-13-2004 09:34 AM


All times are GMT -5. The time now is 08:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration