473,474 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

template & void argument

Thanks Victor for your previous answer!

I have the following problem:

I would like to implement an interface class that defines a pure virtual
method that could take any parameter as input, void inlcuded.
The first idea was to use a template, but in that case void is not admited
as a type right?

i.e.
template<typename T>
class A {

virtual do_something(T) = 0;

};

Then, one of the derive classes can't just do :

do_something(){...}

Because the compiler will complain for void not being a type...

Any work-around this?

Cheers

Manuel

--
================================================== ======================
Manuel Diaz-Gomez | ATLAS Bldg. 32/SB-008 tel. +41 22 76 76304
CERN EP Division
CH-1211 Geneva 23
SWITZERLAND
================================================== ======================
Jul 22 '05 #1
5 2055
"Manuel Maria Diaz Gomez" <Ma*********************@cern.ch> wrote in message news:<c6**********@sunnews.cern.ch>...
Thanks Victor for your previous answer!

I have the following problem:

I would like to implement an interface class that defines a pure virtual
method that could take any parameter as input, void inlcuded.


Just overload the method with one that has no arguments ...

For example, (re-writing your sans errors 8*)

template<typename T>
class A {
public: // or protected
virtual void do_something(T) = 0; // you forgot the return type

virtual void do_something() =0; // no argument version

};
class empty {};

class foo : public A<empty> {
public:
virtual void do_something() {}
};

Now the above should work fine, and be called through the no-argument
virtual function in the interface.

HTH, Dave Moore
Jul 22 '05 #2
Dave Moore wrote:

"Manuel Maria Diaz Gomez" <Ma*********************@cern.ch> wrote in message news:<c6**********@sunnews.cern.ch>...
Thanks Victor for your previous answer!

I have the following problem:

I would like to implement an interface class that defines a pure virtual
method that could take any parameter as input, void inlcuded.


Just overload the method with one that has no arguments ...

For example, (re-writing your sans errors 8*)

template<typename T>
class A {
public: // or protected
virtual void do_something(T) = 0; // you forgot the return type

virtual void do_something() =0; // no argument version

};
class empty {};

class foo : public A<empty> {
public:
virtual void do_something() {}
};

Now the above should work fine, and be called through the no-argument
virtual function in the interface.

HTH, Dave Moore


Suppose that the problem requires that there is only 1 do_something method that
exactly and only corresponds to the type T.

In your example, for type int, the template expands to:

virtual void do_something(int) = 0;
void do_something() = 0; // not wanted for non-void types
Jul 22 '05 #3
On Thu, 29 Apr 2004 12:07:17 +0200, "Manuel Maria Diaz Gomez"
<Ma*********************@cern.ch> wrote:
Thanks Victor for your previous answer!

I have the following problem:

I would like to implement an interface class that defines a pure virtual
method that could take any parameter as input, void inlcuded.
The first idea was to use a template, but in that case void is not admited
as a type right?

i.e.
template<typename T>
class A {

virtual do_something(T) = 0;

};

Then, one of the derive classes can't just do :

do_something(){...}

Because the compiler will complain for void not being a type...

Any work-around this?


Specialize on void (which is a bit of a pain, I know):

template<>
class A<void>
{
virtual do_something() = 0;
};

You can always add a base class that doesn't need void specialization
to avoid duplicating any work between A<T> and A<void>.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #4
Julie <ju***@nospam.com> wrote in message news:<40***************@nospam.com>...
Dave Moore wrote:

"Manuel Maria Diaz Gomez" <Ma*********************@cern.ch> wrote in message news:<c6**********@sunnews.cern.ch>...
Thanks Victor for your previous answer!

I have the following problem:

I would like to implement an interface class that defines a pure virtual
method that could take any parameter as input, void inlcuded.


Just overload the method with one that has no arguments ...

For example, (re-writing your sans errors 8*)

template<typename T>
class A {
public: // or protected
virtual void do_something(T) = 0; // you forgot the return type

virtual void do_something() =0; // no argument version

};
class empty {};

class foo : public A<empty> {
public:
virtual void do_something() {}
};

Now the above should work fine, and be called through the no-argument
virtual function in the interface.

HTH, Dave Moore


Suppose that the problem requires that there is only 1 do_something method that
exactly and only corresponds to the type T.


Well, that wasn't in the original deal, 8*) but I think it is
reasonable to assume that you will know which version (with or without
argument) you want to override, so you can simply declare the other
one private in the derived class.

class foo2 : public A<int> {
public:
virtual void do_something(int); // override version with argument
private:
void do_something(); // hide version without argument
};

It is not an air-tight solution, but without seeing a more in-depth
example, I can't think of anything else. The TMP wizards might be
able to come up with a more elegant solution I suppose.

Dave Moore
Jul 22 '05 #5
dt*****@rijnh.nl (Dave Moore) wrote in message news:<30*************************@posting.google.c om>...
Julie <ju***@nospam.com> wrote in message news:<40***************@nospam.com>...
Dave Moore wrote:

"Manuel Maria Diaz Gomez" <Ma*********************@cern.ch> wrote in message news:<c6**********@sunnews.cern.ch>...
> Thanks Victor for your previous answer!
>
> I have the following problem:
>
> I would like to implement an interface class that defines a pure virtual
> method that could take any parameter as input, void inlcuded.

Just overload the method with one that has no arguments ...

For example, (re-writing your sans errors 8*)

template<typename T>
class A {
public: // or protected
virtual void do_something(T) = 0; // you forgot the return type

virtual void do_something() =0; // no argument version

};
class empty {};

class foo : public A<empty> {
public:
virtual void do_something() {}
};

Now the above should work fine, and be called through the no-argument
virtual function in the interface.

HTH, Dave Moore


Suppose that the problem requires that there is only 1 do_something method that
exactly and only corresponds to the type T.


Well, that wasn't in the original deal, 8*) but I think it is
reasonable to assume that you will know which version (with or without
argument) you want to override, so you can simply declare the other
one private in the derived class.

class foo2 : public A<int> {
public:
virtual void do_something(int); // override version with argument
private:
void do_something(); // hide version without argument
};

It is not an air-tight solution, but without seeing a more in-depth
example, I can't think of anything else. The TMP wizards might be
able to come up with a more elegant solution I suppose.


Hmmm .. actually, it may not be a solution at all, because the no-arg
version is still public in the base class, so it could be called
through a pointer to the base class at run-time. I'm not sure what
would happen in this case if the object pointed-to was a derived type
with a private version of the function, but I guess it wouldn't be
good. Actually, I don't have much experience with run-time
polymorphism, so maybe I should keep my mouth (er, keyboard?) shut and
let the experts answer these questions. 8*)

OTOH, I *do* learn a lot when I get shot down by those more
knowledgable than me ... I just don't want to screw up the person I
was trying to help in the first place.

Dave Moore
Jul 22 '05 #6

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

Similar topics

2
by: Elven | last post by:
Hi! I was trying to find the solution to this problem, but I don't think I could quite come up with the correct keywords to find it, since I'm pretty sure it's been asked before. In short,...
2
by: xuatla | last post by:
The following is just a sample code to demostrate my question: ----------- template <typename T> class C { public: friend void f1(double i=2) { std::cout << i; } ; };
4
by: Vijai Kalyan | last post by:
I was decomposing a task into different policies. Essentially, there is a general option obtained from a server and user options obtained from configuration variables. The two options are...
5
by: krema2ren | last post by:
Hi Does some of you know how to declare getCollection() in the cpp file? My code snippet below produces following compile error: foo.cpp(48) : error C2143: syntax error : missing ';' before...
2
by: Siegfried Heintze | last post by:
I'm running on g++ v2.95 and 3.2. Why is it that the print functions work fine without any extra template parameters, but the SizeOfCod2 function requires that the bold code be uncommented? ...
3
by: a | last post by:
Hi, I'm trying to create a helper class that will allow me to use a pointer or reference to a class without knowing if it is a pointer or reference. Conceptual example: //******************...
2
by: Glenn G. Chappell | last post by:
I am trying to write two constructors for the same class. One takes an iterator and so is a template. The other takes a particular type by reference to const. class Foo { public:...
5
by: krishnaroskin | last post by:
Hey all, I've been running into a problem with default values to template'd functions. I've boiled down my problem to this simple example code: #include<iostream> using namespace std; //...
2
by: nassim.bouayad.agha | last post by:
Hello, here is a code snippet showning my problem : template<typename _K> class TClass1 { public: void Process(const _K& arg) const {
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.