473,406 Members | 2,713 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.

templates & classes

I have 2 classes with exactly the same members (all static except
dtor/ctor).
Classes have different implementantion in only one static member
function and first class has one more member function.

How can I write this code with templates?
First of all: Thought to write code with templates is correct?

members of classes are static because refer to devices. There is no
reason to be non-static.

Thanks.

2 classes follow:
------------------
class ALDevices
{
static std::vector<std::stringlstDevices;
// AND MOOOOOORE STATICs SHARED IN 2 CLASSES!

public:
ALDevices(); // IMPLEMENTENT DIFFERENT IN 2 CLASSES
static void closeDevice(); // IMPLEMENTENT DIFFERENT IN 2 CLASSES

static int getTotalDevices() { return lstDevices.size(); }
// AND MOOOOOORE STATICs SHARED IN 2 CLASSES!

static ALCcontext *getActiveContext() { return context; } // DOEN'T
EXIST AT ALL IN SECOND CLASS
};
------------------
class ALCaptureDevices
{
static std::vector<std::stringlstDevices;
// AND MOOOOOORE STATICs SHARED IN 2 CLASSES!

public:
ALCaptureDevices(); // IMPLEMENTENT DIFFERENT IN 2 CLASSES
static void closeDevice(); // IMPLEMENTENT DIFFERENT IN 2 CLASSES

static int getTotalDevices() { return lstDevices.size(); }
// AND MOOOOOORE STATICs SHARED IN 2 CLASSES!
};
------------------
Feb 22 '07 #1
16 1825
Hi

chameleon wrote:
I have 2 classes with exactly the same members (all static except
dtor/ctor).
Classes have different implementantion in only one static member
function and first class has one more member function.

How can I write this code with templates?
First of all: Thought to write code with templates is correct?

members of classes are static because refer to devices. There is no
reason to be non-static.
Two suggestions:
1. Redesign your program ;-)
2. Build a class template parameterized by a single type, put all
duplicated members in here. Derive both your classes from the
instantiations of this class template for the respective class.

Markus
Feb 22 '07 #2
chameleon wrote:
I have 2 classes with exactly the same members (all static except
dtor/ctor).
Classes have different implementantion in only one static member
function and first class has one more member function.

How can I write this code with templates?
First of all: Thought to write code with templates is correct?
Seems like overkill. Put all the common stuff into a class, and write
two derived classes.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Feb 22 '07 #3
O/H Pete Becker έγραψε:
chameleon wrote:
>I have 2 classes with exactly the same members (all static except
dtor/ctor).
Classes have different implementantion in only one static member
function and first class has one more member function.

How can I write this code with templates?
First of all: Thought to write code with templates is correct?

Seems like overkill. Put all the common stuff into a class, and write
two derived classes.
The problem here is that: All common stuff is static members.
I want 2 template instantiations, so, 2 static member instances.

If I derive 2 classes from one, both classes share common static members.
Feb 22 '07 #4
chameleon wrote:
O/H Pete Becker έγραψε:
>chameleon wrote:
>>I have 2 classes with exactly the same members (all static except
dtor/ctor).
Classes have different implementantion in only one static member
function and first class has one more member function.

How can I write this code with templates?
First of all: Thought to write code with templates is correct?

Seems like overkill. Put all the common stuff into a class, and write
two derived classes.

The problem here is that: All common stuff is static members.
Why is it a problem?
I want 2 template instantiations, so, 2 static member instances.
Why?
If I derive 2 classes from one, both classes share common static
members.
So?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 22 '07 #5
chameleon wrote:
O/H Pete Becker έγραψε:
>chameleon wrote:
>>I have 2 classes with exactly the same members (all static except
dtor/ctor).
Classes have different implementantion in only one static member
function and first class has one more member function.

How can I write this code with templates?
First of all: Thought to write code with templates is correct?

Seems like overkill. Put all the common stuff into a class, and write
two derived classes.

The problem here is that: All common stuff is static members.
I want 2 template instantiations, so, 2 static member instances.

If I derive 2 classes from one, both classes share common static members.
They share whatever you put in the base class, and they don't share
whatever you put in the derived classes. If it's not shared, don't put
it in the base.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Feb 22 '07 #6
Victor Bazarov wrote:
chameleon wrote:
>O/H Pete Becker έγραψε:
>>chameleon wrote:
I have 2 classes with exactly the same members (all static except
dtor/ctor).
Classes have different implementantion in only one static member
function and first class has one more member function.

How can I write this code with templates?
First of all: Thought to write code with templates is correct?

Seems like overkill. Put all the common stuff into a class, and write
two derived classes.
The problem here is that: All common stuff is static members.

Why is it a problem?
>I want 2 template instantiations, so, 2 static member instances.

Why?
>If I derive 2 classes from one, both classes share common static
members.

So?

V
The description of the OP's problem requires that 2 classes do not share
the storage of those static members. In this case, derivation won't
work. static class members can often be thought of as class member as
opposed to object member. The code as it is posted by the OP is fine. I
don't believe template will create any new solution. Check the example
code below:

#include <iostream>
using namespace std;

struct base{
static int x;
static int getx(){ return x;}
};

struct d1 : base {
static int getx(){ return x+1; }
static int xx(){ return x*x; }
};

struct d2 : base {
};

int base::x = 10;

int main(){
d1 a; d2 b;

cout << "d1::x = " << d1::getx() << " d2::x = " << d2::getx() << endl;
cout << "a.x = " << a.getx() << " b.x = " << b.getx() << endl;
a.x = 5;
cout << "d1::x = " << d1::getx() << " d2::x = " << d2::getx() << endl;
cout << "a.x = " << a.getx() << " b.x = " << b.getx() << endl;
d1::x = 15;
cout << "d1::x = " << d1::getx() << " d2::x = " << d2::getx() << endl;
cout << "a.x = " << a.getx() << " b.x = " << b.getx() << endl;

return 0;
}
Feb 22 '07 #7
Fei Liu wrote:
The description of the OP's problem requires that 2 classes do not share
the storage of those static members. In this case, derivation won't
work.
Of course it will. The things that aren't shared don't go in the base class.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Feb 22 '07 #8
"chameleon" <ch******@hotmail.comwrote in message
news:MI******************@twister1.libero.it
O/H Pete Becker έγραψε:
>chameleon wrote:
>>I have 2 classes with exactly the same members (all static except
dtor/ctor).
Classes have different implementantion in only one static member
function and first class has one more member function.

How can I write this code with templates?
First of all: Thought to write code with templates is correct?

Seems like overkill. Put all the common stuff into a class, and write
two derived classes.

The problem here is that: All common stuff is static members.
I want 2 template instantiations, so, 2 static member instances.

If I derive 2 classes from one, both classes share common static
members.
You can easily work around that with templates. A simple example follows.

template <int n>
class Base
{
static int x;
public:
static void Setx(int a_x)
{
x = a_x;
}
static int Getx()
{
return x;
}
};

template <int n>
int Base<n>::x;

class Derived1: public Base<0>
{
};

class Derived2: public Base<1>
{
};
int main()
{
Derived1 d1;
Derived2 d2;

d1.Setx(1);
d2.Setx(2);

cout << "Static member x of d1 is " << d1.Getx() << endl;
cout << "Static member x of d2 is " << d2.Getx() << endl;

return 0;
}

--
John Carson
Feb 23 '07 #9
John Carson wrote:
"chameleon" <ch******@hotmail.comwrote in message
news:MI******************@twister1.libero.it
>O/H Pete Becker έγραψε:
>>chameleon wrote:
I have 2 classes with exactly the same members (all static except
dtor/ctor).
Classes have different implementantion in only one static member
function and first class has one more member function.

How can I write this code with templates?
First of all: Thought to write code with templates is correct?

Seems like overkill. Put all the common stuff into a class, and write
two derived classes.
The problem here is that: All common stuff is static members.
I want 2 template instantiations, so, 2 static member instances.

If I derive 2 classes from one, both classes share common static
members.

You can easily work around that with templates. A simple example follows.

template <int n>
class Base
{
static int x;
public:
static void Setx(int a_x)
{
x = a_x;
}
static int Getx()
{
return x;
}
};

template <int n>
int Base<n>::x;

class Derived1: public Base<0>
{
};

class Derived2: public Base<1>
{
};
int main()
{
Derived1 d1;
Derived2 d2;

d1.Setx(1);
d2.Setx(2);

cout << "Static member x of d1 is " << d1.Getx() << endl;
cout << "Static member x of d2 is " << d2.Getx() << endl;

return 0;
}
Your solution is correct, but requires assigning each derived class a
unique int. You could instead just directly use the derived class as
the template to the base:

template <class Derived>
class Base
{
static int x;
public:
static void Setx(int a_x)
{
x = a_x;
}
static int Getx()
{
return x;
}
};

template <class Derived>
int Base<Derived>::x;

class Derived1: public Base<Derived1>
{
};

class Derived2: public Base<Derived2>
{
};

--
Alan Johnson
Feb 23 '07 #10
"Alan Johnson" <aw***@yahoo.comwrote in message
news:er**********@aioe.org
John Carson wrote:
>"chameleon" <ch******@hotmail.comwrote in message
news:MI******************@twister1.libero.it
>>O/H Pete Becker έγραψε:
chameleon wrote:
I have 2 classes with exactly the same members (all static except
dtor/ctor).
Classes have different implementantion in only one static member
function and first class has one more member function.
>
How can I write this code with templates?
First of all: Thought to write code with templates is correct?
>
Seems like overkill. Put all the common stuff into a class, and
write two derived classes.
The problem here is that: All common stuff is static members.
I want 2 template instantiations, so, 2 static member instances.

If I derive 2 classes from one, both classes share common static
members.

You can easily work around that with templates. A simple example
follows. template <int n>
class Base
{
static int x;
public:
static void Setx(int a_x)
{
x = a_x;
}
static int Getx()
{
return x;
}
};

template <int n>
int Base<n>::x;

class Derived1: public Base<0>
{
};

class Derived2: public Base<1>
{
};
int main()
{
Derived1 d1;
Derived2 d2;

d1.Setx(1);
d2.Setx(2);

cout << "Static member x of d1 is " << d1.Getx() << endl;
cout << "Static member x of d2 is " << d2.Getx() << endl;

return 0;
}

Your solution is correct, but requires assigning each derived class a
unique int. You could instead just directly use the derived class as
the template to the base:

template <class Derived>
class Base
{
static int x;
public:
static void Setx(int a_x)
{
x = a_x;
}
static int Getx()
{
return x;
}
};

template <class Derived>
int Base<Derived>::x;

class Derived1: public Base<Derived1>
{
};

class Derived2: public Base<Derived2>
{
};

Yes, much nicer. I like it :)

--
John Carson
Feb 23 '07 #11
Pete Becker wrote:
Fei Liu wrote:
>The description of the OP's problem requires that 2 classes do not
share the storage of those static members. In this case, derivation
won't work.

Of course it will. The things that aren't shared don't go in the base
class.
It will what? The storage of derived classes is shared in base. The CRTP
solution posted in this thread is very nice though.
Feb 23 '07 #12
Fei Liu wrote:
Pete Becker wrote:
>Fei Liu wrote:
>>The description of the OP's problem requires that 2 classes do not
share the storage of those static members. In this case, derivation
won't work.

Of course it will. The things that aren't shared don't go in the base
class.
It will what?
"In this case, derivation won't work."

"Of course it will.

What, specifically, do you claim "won't work"? Whatever is defined in
the base class is part of the base class, and whatever is defined in the
derived classes is part of the derived classes. So if you want the
derived classes to hold something in common, put it in the base class.
If you want the derived classes to hold independent things, put them in
the derived classes. The result is that the derived classes both have
whatever is in the base class, and each has whatever is defined for it.

struct base
{
static int i;
static void f();
};

struct derived1: base
{
static int j;
static void g();
};

struct derived2: base
{
static int k;
static void h();
};

Now, base has two members, i and f. derived1 has four members. It
inherits i and f from base, and it has the two in its definition, j and
g. derived2 has four members. It also inherits i and f from base, and it
has two in its definition, k and h.

So, once again: the members that are the same for both derived types
should be defined in base, and the members that are different should be
defined in their respective derived classes.
The storage of derived classes is shared in base. The CRTP
solution posted in this thread is very nice though.
Using templates where inheritance is more appropriate isn't "nice."

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Feb 23 '07 #13
Pete Becker wrote:
Fei Liu wrote:
>Pete Becker wrote:
>>Fei Liu wrote:
The description of the OP's problem requires that 2 classes do not
share the storage of those static members. In this case, derivation
won't work.

Of course it will. The things that aren't shared don't go in the base
class.
It will what?

"In this case, derivation won't work."

"Of course it will.

What, specifically, do you claim "won't work"? Whatever is defined in
the base class is part of the base class, and whatever is defined in the
derived classes is part of the derived classes. So if you want the
derived classes to hold something in common, put it in the base class.
If you want the derived classes to hold independent things, put them in
the derived classes. The result is that the derived classes both have
whatever is in the base class, and each has whatever is defined for it.
Clearly you don't understand the OP's spec, hint it's a device driver.
Your logical representation fails to meet the requirement of the
physical model.
>
struct base
{
static int i;
static void f();
};

struct derived1: base
{
static int j;
static void g();
};

struct derived2: base
{
static int k;
static void h();
};

Now, base has two members, i and f. derived1 has four members. It
inherits i and f from base, and it has the two in its definition, j and
g. derived2 has four members. It also inherits i and f from base, and it
has two in its definition, k and h.

So, once again: the members that are the same for both derived types
should be defined in base, and the members that are different should be
defined in their respective derived classes.
>The storage of derived classes is shared in base. The CRTP solution
posted in this thread is very nice though.

Using templates where inheritance is more appropriate isn't "nice."
Again you demonstrate that you do not understand the OP's problem.
Feb 23 '07 #14
Fei Liu wrote:
Pete Becker wrote:
>Fei Liu wrote:
>>Pete Becker wrote:
Fei Liu wrote:
The description of the OP's problem requires that 2 classes do not
share the storage of those static members. In this case, derivation
won't work.

Of course it will. The things that aren't shared don't go in the
base class.

It will what?

"In this case, derivation won't work."

"Of course it will.

What, specifically, do you claim "won't work"? Whatever is defined in
the base class is part of the base class, and whatever is defined in
the derived classes is part of the derived classes. So if you want the
derived classes to hold something in common, put it in the base class.
If you want the derived classes to hold independent things, put them
in the derived classes. The result is that the derived classes both
have whatever is in the base class, and each has whatever is defined
for it.

Clearly you don't understand the OP's spec, hint it's a device driver.
Your logical representation fails to meet the requirement of the
physical model.
On the contrary: I have read it carefully, and suggested an approach
that will do exactly what he said he wants.

Once again: what, specifically, do you claim "won't work"? No more
handwaving. Point out the exact problem, so that readers of this thread
will have a chance at understanding what it is that you think can't be
done through inheritance.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Feb 23 '07 #15
"Pete Becker" <pe**@versatilecoding.comwrote in message
news:Fr******************************@giganews.com
Fei Liu wrote:
>Pete Becker wrote:
>>Fei Liu wrote:
Pete Becker wrote:
Fei Liu wrote:
>The description of the OP's problem requires that 2 classes do
>not share the storage of those static members. In this case,
>derivation won't work.
>
Of course it will. The things that aren't shared don't go in the
base class.
>
It will what?

"In this case, derivation won't work."

"Of course it will.

What, specifically, do you claim "won't work"? Whatever is defined
in the base class is part of the base class, and whatever is
defined in the derived classes is part of the derived classes. So
if you want the derived classes to hold something in common, put it
in the base class. If you want the derived classes to hold
independent things, put them in the derived classes. The result is
that the derived classes both have whatever is in the base class,
and each has whatever is defined for it.

Clearly you don't understand the OP's spec, hint it's a device
driver. Your logical representation fails to meet the requirement of
the physical model.

On the contrary: I have read it carefully, and suggested an approach
that will do exactly what he said he wants.

Once again: what, specifically, do you claim "won't work"? No more
handwaving. Point out the exact problem, so that readers of this
thread will have a chance at understanding what it is that you think
can't be done through inheritance.

Speaking for myself, I didn't think much about the OP's spec, I just wanted
to answer the OP's question (just stating a fact here, not making a case).

My read was that the OP was mainly interested in code re-use. Thus the OP
didn't want identical lists of members in the derived classes. Using a
template to create two distinct bases was a way to get all those members
into the derived classes and make the static variables distinct, while only
typing them once (just why the OP wanted distinct static variables I don't
know).

--
John Carson
Feb 24 '07 #16
John Carson wrote:
"Pete Becker" <pe**@versatilecoding.comwrote in message
news:Fr******************************@giganews.com
>Fei Liu wrote:
>>Pete Becker wrote:
Fei Liu wrote:
Pete Becker wrote:
>Fei Liu wrote:
>>The description of the OP's problem requires that 2 classes do
>>not share the storage of those static members. In this case,
>>derivation won't work.
>Of course it will. The things that aren't shared don't go in the
>base class.
>>
It will what?
"In this case, derivation won't work."

"Of course it will.

What, specifically, do you claim "won't work"? Whatever is defined
in the base class is part of the base class, and whatever is
defined in the derived classes is part of the derived classes. So
if you want the derived classes to hold something in common, put it
in the base class. If you want the derived classes to hold
independent things, put them in the derived classes. The result is
that the derived classes both have whatever is in the base class,
and each has whatever is defined for it.
Clearly you don't understand the OP's spec, hint it's a device
driver. Your logical representation fails to meet the requirement of
the physical model.
On the contrary: I have read it carefully, and suggested an approach
that will do exactly what he said he wants.

Once again: what, specifically, do you claim "won't work"? No more
handwaving. Point out the exact problem, so that readers of this
thread will have a chance at understanding what it is that you think
can't be done through inheritance.


Speaking for myself, I didn't think much about the OP's spec, I just wanted
to answer the OP's question (just stating a fact here, not making a case).
Indeed. And you may have noticed that I didn't jump in and insist that
you're wrong. In fact, I suspect that a template may be the right answer
to the question that wasn't asked. But given the original problem
statement, all that's needed is to hoist one data member and one member
function into the derived class, and that doesn't call for a template.
Low-level design follows from actual specifications, not from guesses.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Feb 24 '07 #17

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

Similar topics

1
by: Vince C. | last post by:
Hi all, I've created XML documents that are described with a schema. I'm using those documents to create web pages. All my web pages contain a fixed header and a variable document part. The...
5
by: Ray Gardener | last post by:
Would templates be necessary if C++ had all numeric types inherit from a base "number" class and had all types inherit from a base "object" class? (Sorry if this has already been discussed to...
22
by: E. Robert Tisdale | last post by:
According to the C++ FAQ Lite: http://www.parashift.com/ What is "genericity"? Yet another way to say, "class templates." Not to be confused with "generality" (which just means avoiding...
1
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of...
20
by: verec | last post by:
One problem I've come accross in designing a specific version of auto_ptr is that I have to disntiguish between "polymorphic" arguments and "plain" ones, because the template has to, internally,...
11
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
11
by: Peter Oliphant | last post by:
Is there any plan to support templates with managed code in the (near) future? For instance, VS.NET 2005... : )
8
by: sagi.perel | last post by:
I have tried to compile the following code on Win & Unix. Doesn't work on either. <----- CODE -----> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <vector> #include...
7
by: Chris | last post by:
Hi All, This is a weird one but I am hoping someone can help or has some pointers, a recipe how to do the following: I have to move some code from c++ to objective-c and to do this I must...
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
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,...
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...

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.