473,399 Members | 4,254 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,399 software developers and data experts.

Best way to use a derived member as a template parameter to a base

What I want to do (and can't...) is

Template <typename Tclass base
{
// whatever
}

class derived: public base<derived::myVals>
{
Enum myVals
{
// this isn't relevant either
}
}

....so that I can have a function in the base that takes the enum as a
parameter.

At the moment I'm running with a helper class that contains the enum
(and nothing else); but it's ugly, because it separates the enum from
its proper class. What do people think is the best way around this?

Ta
Andy
Aug 14 '08 #1
4 1431
Andy Champ wrote:
What I want to do (and can't...) is

Template <typename Tclass base
{
// whatever
}

class derived: public base<derived::myVals>
{
Enum myVals
{
// this isn't relevant either
}
}

...so that I can have a function in the base that takes the enum as a
parameter.

At the moment I'm running with a helper class that contains the enum
(and nothing else); but it's ugly, because it separates the enum from
its proper class. What do people think is the best way around this?
No way around this. You cannot use a member of the class *before*
defining the class itself (and the member).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 14 '08 #2
On Aug 14, 3:28 pm, Andy Champ <no....@nospam.comwrote:
What I want to do (and can't...) is

Template <typename Tclass base
{
// whatever

}

class derived: public base<derived::myVals>
{
Enum myVals
{
// this isn't relevant either
}

}

...so that I can have a function in the base that takes the enum as a
parameter.

At the moment I'm running with a helper class that contains the enum
(and nothing else); but it's ugly, because it separates the enum from
its proper class. What do people think is the best way around this?
Use a namespace to group the enum and the derived class.

Cheers! --M
Aug 14 '08 #3
On Aug 14, 9:28*pm, Andy Champ <no....@nospam.comwrote:
What I want to do (and can't...) is

Template <typename Tclass base
{
// whatever

}

class derived: public base<derived::myVals>
{
* * * * Enum myVals
* * * * {
* * * * * * * * // this isn't relevant either
* * * * }

}

...so that I can have a function in the base that takes the enum as a
parameter.

At the moment I'm running with a helper class that contains the enum
(and nothing else); *but it's ugly, because it separates the enum from
its proper class. *What do people think is the best way around this?
One other way to do this is to put the enum in a separate wrapper
class before you define your Derived class. Something like:

struct MyValues
{
enum MyValuesEnum
{
// Your values here.
};
};

class Derived : public Base<MyValues::MyValuesEnum>
{
// You're class members here.
};

Good luck.
Aug 15 '08 #4
mlimber wrote:
>
Use a namespace to group the enum and the derived class.

Cheers! --M
I've nearly taken your advice. Something like this:

namespace espace
{
enum base { base1, base2, etc };
}

template <typename Tclass base
{
public:
void someFunc(T param);
....
}

namespace espace
{
enum derived1
{
value1 = base1,
value2 = 1000 /* to keep it well clear of base */
etc
}
}

class derived1: public base<espace::derived1>
{
....
}

namespace espace
{
enum derived2
{
value1 = base2,
value2 = 1000 /* to keep it well clear of base */
etc
}
}

class derived2 public base<espace::derived2>
{
....
}

I can use derived1::someFunc with espace::derived1 and
derived2::someFunc with espace::derived2, and it type checks - so this
is good enough.

Thanks guys

Andy
Aug 15 '08 #5

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

Similar topics

1
by: Art | last post by:
This is partially an academic question, but I'm trying to understand templates better. I have a base class that uses template parameters to define the behavior of its class. I want to subclass this...
4
by: Matthias von Faber | last post by:
Hello, The code below compiles fine on VS 6.0 and .NET 2005, but G++ complains about mData not having been declared in the Derived class: template<typename T> class Base {
9
by: Mirko Puhic | last post by:
Is there a way to properly do this? struct Derived; struct Base{ Derived der; };
0
by: ivan.leben | last post by:
I am writing this in a new thread to alert that I found a solution to the problem mentioned here: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7970afaa089fd5b8 and to avoid...
2
by: montyshasta | last post by:
Take this code as a base case, it compiles successfully: struct R { int i; }; class S : public R { void F(void) {i = 0;} };
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
9
by: Naomi | last post by:
I need to make software engineering decision to do with using a derived data type in a container class. So for example, if I have an Edge class, and I want to make a Edge object which contains two...
4
by: softwaredoug | last post by:
Here is some test code I've been attempting to compile (Visual Studio 2003) test.h: class Base { protected: Base() {} public:
10
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.