473,803 Members | 4,400 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static polymorphism -- what's wrong?

Hi all,
Could someone please explain what is wrong with this code (the compiler
complains about "'int_type' is not a member of 'B'"):
struct B;

template <typename DerivedT>
struct A
{
typedef typename DerivedT::int_t ype int_type;

int_type get(void) const { return 0; }
};
struct B : public A<B>
{
typedef int int_type;
};

int main()
{
B b;
return b.get();
}

Oct 13 '05 #1
5 1871

shablool wrote:
Hi all,
Could someone please explain what is wrong with this code (the compiler
complains about "'int_type' is not a member of 'B'"):
struct B;

template <typename DerivedT>
struct A
{
typedef typename DerivedT::int_t ype int_type;

int_type get(void) const { return 0; }
};
struct B : public A<B>
{
typedef int int_type;
};

int main()
{
B b;
return b.get();
}


It seems unlikely that struct A is able to typedef a typedef from one
of its derived classes.

Reversing the typedefs to avoid the circular dependency does fix the
problem:

struct B;

template <typename DerivedT>
struct A
{
typedef int int_type;

int_type get() const { return 0; }
};

struct B : public A<B>
{
using A<B>::int_type ;
};

int main()
{
B b;
return b.get();
}

Greg

Oct 13 '05 #2
shablool wrote:
Hi all,
Could someone please explain what is wrong with this code (the compiler
complains about "'int_type' is not a member of 'B'"):
struct B;

template <typename DerivedT>
struct A
{
typedef typename DerivedT::int_t ype int_type;

int_type get(void) const { return 0; }
};
struct B : public A<B>
{
typedef int int_type;
};

int main()
{
B b;
return b.get();
}


The problem is that B is not defined yet, but you're trying to use it
in A<B>. You can either do what Greg said, or you can break your B
class apart:

struct BAttributes { typedef int int_type; };

template <class T>
struct A
{
typedef typename T::int_type int_type;
int_type get() const { return 0; }
};

struct B : public A<BAttributes> , public BAttributes {};

int main()
{
B b;
return b.get();
}

Note that the multiple inheritance is technically unnecessary here
since A<> duplicates all its types. You would need it only if there
were additional functions, data, or types that A<> did not "copy". BTW,
an easier way to transfer the contents of BAttributes to A<> would be
to treat it like a policy class:

struct BAttributes { typedef int int_type; };

template <class Attributes>
struct A : public Attributes
{
int_type get() const { return 0; }
};

Cheers! --M

Oct 13 '05 #3
Greg wrote:
[snip context]
struct B;

template <typename DerivedT>
struct A
{
typedef int int_type;

int_type get() const { return 0; }
};

struct B : public A<B>
{
using A<B>::int_type ;
};

int main()
{
B b;
return b.get();
}


Ok, that freaks me out. I'd never have thought that you
could derive a class from a template of the same class.
I guess it's because, in this case, A<> only refers to
DerivedT in ways that only need the forward declaration.
If you were to, for example, include a data member in A<>
of type DerivedT, then it would bust. (And the compiler
I use agrees, for what that's worth.)
Socks

Oct 13 '05 #4
Puppet_Sock wrote:
Greg wrote:
[snip context]
struct B;

template <typename DerivedT>
struct A
{
typedef int int_type;

int_type get() const { return 0; }
};

struct B : public A<B>
{
using A<B>::int_type ;
};

int main()
{
B b;
return b.get();
}


Ok, that freaks me out. I'd never have thought that you
could derive a class from a template of the same class.
I guess it's because, in this case, A<> only refers to
DerivedT in ways that only need the forward declaration.
If you were to, for example, include a data member in A<>
of type DerivedT, then it would bust. (And the compiler
I use agrees, for what that's worth.)
Socks


I use that technique for automatic registration with a factory object.
Assuming Factory<> and Singleton<> classes as found in _Modern C++
Design_'s Loki, I have this in a header file:

// Helper classes for object creation by a factory with a default
// ctor. Concrete classes must implement a static function GetID().
template<class AbstractClass, class ConcreteClass>
class DefaultCreator
{
public:
static std::auto_ptr<A bstractClass> Create()
{
// Verify that the given types are actually super and sub classes
STATIC_CHECK( SUPERSUBCLASS( AbstractClass, ConcreteClass ),
types_are_not_s uper_and_subcla ss );

// While the factory will also check at run-time to see if a class
// is registered, we assert on m_registered here to cause a link-
// time failure if we forget to register a concrete command class.
assert( m_registered );

return std::auto_ptr<A bstractClass>( new ConcreteClass );
}
private:
typedef Singleton< Factory<Abstrac tClass> > theFactory;
static const bool m_registered;
};
// Automatically register all classes that inherit from
// DefaultCreator< >. (Note that these static members appear in header
// file because they are themselves templates. The compiler will
// ensure that there is only one instance of each globally.)

template<class AbstractClass, class ConcreteClass>
const bool DefaultCreator< AbstractClass,C oncreteClass>:: m_registered =
DefaultCreator< AbstractClass,C oncreteClass>:: theFactory::Ins tance()
.Register( ConcreteClass:: GetID(), ConcreteClass:: Create );
And I use it like so:

class Base {};

class Derived
: public Base
, public DefaultCreator< Base, Derived>
{
public:
static int GetAssignedID() { return 42; }
// ...
};

Then the stuff _Modern C++ Design_ does to register classes with the
factory is automatic for any class inheriting from DefaultCreator< >.

Cheers! --M

Oct 13 '05 #5
mlimber wrote:
class Derived
: public Base
, public DefaultCreator< Base, Derived>
{
public:
static int GetAssignedID() { return 42; }
// ...
};

[snip]

Oops. Copy-paste-and-simplify error. That function should be:

static int GetID() { return 42; }

Cheers! --M

Oct 13 '05 #6

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

Similar topics

13
10737
by: Axehelm | last post by:
Okay, I'm in a debate over whether or not static methods are a good idea in a general domain class. I'm personally not a fan of static methods but we seem to be using them to load an object. For example if you have an Employee class rather then instantiating an instance you call a static method 'GetEmployees' and it returns a List of Employee objects. I'm looking for what other people are doing and if you feel this is a good or bad...
4
2522
by: PengYu.UT | last post by:
Hi, Some dynamic polymorphism programs can be converted to the equavalent static polymorphism programs. I'm wondering if there are any generall procedures that I can use to do this conversion. Best wishes, Peng
33
3359
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or near-impossible. It seems like it would be useful. In fact, there's a place in my code that I could make good use of it. So why not? Chris
17
2358
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component oriented design (owreilly - Juval Lowy), and it was actually very nice. The book goes on about how we should use Interfaces exposure instead of classes (this is my terminology and english is not my language so I hope you understand what I'm on about...).
15
3045
by: rwf_20 | last post by:
I just wanted to throw this up here in case anyone smarter than me has a suggestion/workaround: Problem: I have a classic producer/consumer system which accepts 'commands' from a socket and 'executes' them. Obviously, each different command (there are ~20 currently) has its own needed functionality. The dream goal here would be to remove all knowledge of the nature of the command at runtime. That is, I don't want ANY switch/cases...
10
2321
by: steve bull | last post by:
I have a class SwatchPanel which takes Swatch as a parameter type. How can I call a static function within the Swatch class? For example the code below fails on TSwatch.Exists. How can I get the call to work? Exists is a method within the Swatch class NOT the SwatchPanel class. Is this possible? Suggestions would be very welcome.
4
1979
by: hack_tick | last post by:
Hi Guys! Is function overloading a kind of static polymorphism?
13
14627
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and Nicolai M. Josuttis. Chapter 14.) How to implement analogue of this technique via static polymorphism? Perhaps there is special design pattern for this purpose...
14
33294
by: Dave Booker | last post by:
It looks like the language is trying to prevent me from doing this sort of thing. Nevertheless, the following compiles, and I'd like to know why it doesn't work the way it should: public class ComponentA { static string s_name = "I am the root class."; public string Name { get {return s_name;} } }
9
5851
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to the static Parse method of the conversion class. if (InValue is string) return T.Parse((string)InValue); else return base.ConvertFrom(context, culture, InValue);
0
9703
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
9564
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10316
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...
0
10069
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...
1
7604
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
6842
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.