Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > virtual constructor ideom with templates

Reply
Thread Tools

virtual constructor ideom with templates

 
 
Christof Warlich
Guest
Posts: n/a
 
      08-12-2006
Hi all,

I'm using the virtual constructor ideom to hide implementation details
of services from an application that uses these services, i.e.:

// serviceInterface.h
class SomeService {
public:
static SomeService *create(void);
virtual void useService(void) = 0;
};
// serviceInterface.h ends

// application.cc
#include "serviceInterface.h"
int main(void) {
SomeService *someService = SomeService::create();
someService->useService();
return 0;
}
// application.cc ends

// serviceImplementation.cc
#include "serviceInterface.h"
// numerous implementation specific include files may be needed here
class SomeServiceImplementation: public SomeService {
public:
void useService(void) {
// do something useful
}
private:
// numerous implementation specific types may be needed here
};
SomeService *SomeService::create(void) {
return new SomeServiceImplementation;
};
// serviceImplementation.cc ends

This works pretty well, avoiding that the compiler of the application
needs to know the include paths of all the files that the service
implememtation may need.

But when I want to offer a service with templates (see slightly modified
example below), this approach doesn't seem to work any longer: It
compiles and links fine as long as everything is put into a single file,
but if I do the file split as suggested, the compiler says:

In function `main':application.cc: undefined reference to
`SomeService<int>::create()'

This is because the compiler does not see the implementation (template)
of SomeService<T>::create() when it needs to instantiate it for type
int. Ok, I know that the template implementation has to be put into the
header file as well for this very reason, but if I put it into
serviceInterface.h, I also have to put the class definition of class
SomeServiceImplementation there, which would finally destroy the
"virtual constructor ideom" I'd liked to implement.

Is there any way to work arround this, keeping both the application and
the service implementation independent from each other, at best only
connected through the interface definition that the application is
supposed to be used? I'm desperate, because this independency was a
fundamental design goal of the services being offered.

Thanks for any help,

Christof

// serviceInterface.h
template<typename T> class SomeService {
public:
static SomeService<T> *create(void);
virtual void useService(T t) = 0;
};
// serviceInterface.h ends

// application.cc
#include "serviceInterface.h"
int main(void) {
SomeService<int> *someService = SomeService<int>::create();
someService->useService(10);
return 0;
}
// application.cc ends

// serviceImplementation.cc
#include "serviceInterface.h"
template<typename T> class SomeServiceImplementation: public
SomeService<T> {
public:
void useService(T t) {
// do something useful
}
};
template<typename T> SomeService<T> *SomeService<T>::create(void) {
return new SomeServiceImplementation<T>;
};
// ServiceImplementation.cc ends
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-13-2006
Christof Warlich wrote:
> [..link error with templates..]
> This is because the compiler does not see the implementation
> (template) of SomeService<T>::create() [..]
>
> Is there any way to work arround this, [..]


This is covered in the FAQ.

V


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help on constructor/constructor initialization list with inheritanceusing templates balaji C++ 3 08-10-2011 12:06 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Monster Templates - Question about Submitting Templates Fred HTML 1 09-26-2005 01:09 AM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57