473,406 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

virtual constructor ideom with templates

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 Tclass 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 Tclass SomeServiceImplementation: public
SomeService<T{
public:
void useService(T t) {
// do something useful
}
};
template<typename TSomeService<T*SomeService<T>::create(void) {
return new SomeServiceImplementation<T>;
};
// ServiceImplementation.cc ends
Aug 12 '06 #1
1 1447
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
Aug 13 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Stijn Oude Brunink | last post by:
Hello, I have the following trade off to make: A base class with 2 virtual functions would be realy helpfull for the problem I'm working on. Still though the functions that my program will use...
8
by: puzzlecracker | last post by:
Can the template method be virtual and what are the consequences? Can the template inline functions be virtual and what are the consequences?
4
by: Xavier | last post by:
Hi, I have a question, in a "dreaded diamond" situation, regarding the following code: ---- Begin code #include <iostream> using namespace std;
7
by: Vyacheslav Lanovets | last post by:
Hello, All! One of our target platforms has only 32 MB of virtual memory (Windows CE), so we decided to explicitly load some of our dlls. But the classes created inside such dlls are created...
16
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
3
by: Dmitry Prokoptsev | last post by:
Hello, I need to write a class for exceptions which can be thrown, caught, stored and thrown once again. I have written the following code: --- begin --- #include <string> class Exception...
4
by: Stefan Nikolaus | last post by:
Hello, I've run into problems with defining a template, which inherits from a base template and the usage of virtual methods in those. I want to initialize a member variable depending on which...
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
8
by: Belebele | last post by:
Suppose that I want to write a (concrete) interface class (without virtual functions) to classes that contain a method template // -------------------------------------- class Interface {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.