473,769 Members | 6,305 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::m yVals>
{
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 1443
Andy Champ wrote:
What I want to do (and can't...) is

Template <typename Tclass base
{
// whatever
}

class derived: public base<derived::m yVals>
{
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::m yVals>
{
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::m yVals>
{
* * * * 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::de rived1>
{
....
}

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

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

I can use derived1::someF unc with espace::derived 1 and
derived2::someF unc with espace::derived 2, 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
1948
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 to extend the behavior. I also want to contain pointers to derived classes somehow, in a vector, say. My question is: How do I assign derived classes to the base class? Here is an example:
4
1683
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
1662
by: Mirko Puhic | last post by:
Is there a way to properly do this? struct Derived; struct Base{ Derived der; };
0
2033
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 this topic getting lost before people interested in the problem notice it. The important tricks to the solution are two: 1) make the custom classes take a TEMPLATE argument which defines their BASE class 2) EMBED the custom classes in a "Traits"...
2
1778
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
3851
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* populate the variable, but the population is not *required* by the derived class (which must be the case). What would meet the requirements is if I create an abstract method in the base class that populates the member variable. In this case the...
9
2417
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 NumberVertices instead of Vertices, I can either just use a non- templated Edge class which stores pointers to my NumberVertices and leave it up to the programmer to cast back again to NumberVertex (this type of casting only works with...
4
2951
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
4114
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 allowed to access the protected data members of the base object. This surprises me. Can someone explain why this is? I suspect there is a good reason and I am just having a slow day to not come up with it myself. Bob
0
9587
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10211
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9993
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5298
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2815
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.