472,352 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 software developers and data experts.

template<class Child> class Parent with protected constructors

I have several classes that all keep track of static data. However,
the manner that they keep track of static data is identical, and so
I'm using the template<class Child> class Parent { ... }; idiom (don't
know the name of it, if there is one). The problem is that I don't
want any of my classes to have public constructors. They should be
created by a static member function.

This is what I want to do, save for the fact that this doesn't
compile:

template <class Child>
class Foo {
protected:
static Child * foo;
Foo() { }
public:
static Child * get() { if(foo==NULL) return foo=new Child(); return
foo; }
};

class Bar : public Foo<Bar> {
protected:
Bar() { }
};

class Baz : public Foo<Baz> {
protected:
Baz() { }
};

Bar * Foo<Bar>::foo = NULL;
Baz * Foo<Baz>::foo = NULL;

main() {
Bar & bar = Bar::get();
Baz & baz = Baz::get();
}

The definition of "get" is flagged as an error, because the
constructor is protected. I've tried many different things, but
nothing gives me what I want.

In my actual code, I'm keeping a static set of all the instances of
the class, but I produced this simpler snippet for the sake of
discussion.
Jul 22 '05 #1
4 2631
On 4 Mar 2004 09:45:19 -0800, gr*********@hotmail.com (Grey Plastic) wrote:
I have several classes that all keep track of static data. However,
the manner that they keep track of static data is identical, and so
I'm using the template<class Child> class Parent { ... }; idiom (don't
know the name of it, if there is one). The problem is that I don't
want any of my classes to have public constructors. They should be
created by a static member function.

This is what I want to do, save for the fact that this doesn't
compile:

template <class Child>
class Foo {
protected:
static Child * foo;
Foo() { }
public:
static Child * get() { if(foo==NULL) return foo=new Child(); return
foo; }
};

class Bar : public Foo<Bar> {
protected:
Bar() { }
};

class Baz : public Foo<Baz> {
protected:
Baz() { }
};

Bar * Foo<Bar>::foo = NULL;
Baz * Foo<Baz>::foo = NULL;

main() {
Bar & bar = Bar::get();
Baz & baz = Baz::get();
}

The definition of "get" is flagged as an error, because the
constructor is protected. I've tried many different things, but
nothing gives me what I want.
Here's a modified version (comments indicate additions).
Compiles with: Comeau, MSVC 7,7.1
NOT: gcc (sigh), Borland, MSVC 6

#include <iostream> // just for NULL, really
using namespace std;

template <class Child>
class Foo {
protected:
static Child * foo;
Foo() { }
public:
static Child * get() {
if(foo==NULL) return foo=new Child();
return foo; }
};

class Bar : public Foo<Bar> {
friend class Foo; // added this
protected:
Bar() { }
};

class Baz : public Foo<Baz> {
friend class Foo; // added this
protected:
Baz() { }
};

//template<class Bar>
template<> // added this
Bar * Foo<Bar>::foo = NULL;

//template<class Bar>
template<> // added this
Baz * Foo<Baz>::foo = NULL;

int main() { // returns int
Bar & bar = *Bar::get(); // added *
Baz & baz = *Baz::get(); // added *
return 0; // added this
}
HTH,
-leor

In my actual code, I'm keeping a static set of all the instances of
the class, but I produced this simpler snippet for the sake of
discussion.


Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
Leor Zolman wrote:
On 4 Mar 2004 09:45:19 -0800, gr*********@hotmail.com (Grey Plastic) wrote:
....

Here's a modified version (comments indicate additions).
Compiles with: Comeau, MSVC 7,7.1
NOT: gcc (sigh), Borland, MSVC 6

#include <iostream> // just for NULL, really
using namespace std;

template <class Child>
class Foo {
protected:
static Child * foo;
Foo() { }
public:
static Child * get() {
if(foo==NULL) return foo=new Child();
return foo; }
};

class Bar : public Foo<Bar> {
friend class Foo; // added this
I don't believe that this is valid C++ code anyway. There is no
class Foo as it is not a template.

This might be more correct:

friend class Foo<bar>;

If you want all Foo's to access Bar you can do this:

template <class Child> friend class Foo;

protected:
Bar() { }
};

class Baz : public Foo<Baz> {
friend class Foo; // added this
.... ditto ...
protected:
Baz() { }
};

//template<class Bar>
template<> // added this
Bar * Foo<Bar>::foo = NULL;
Bar * Foo<Bar>::foo = 0; // :-)

I remember a discussion on this group a while back about using NULL. I
think the conclusion was that NULL was worse than useless and that the
literal 0 was much easier to deal with. It was along the lines of
confusion with the C version (or historic) NULL which was (void *)0 and
the fact the C++ has different semantics with void *.

//template<class Bar>
template<> // added this
Baz * Foo<Baz>::foo = NULL;

int main() { // returns int
Bar & bar = *Bar::get(); // added *
Baz & baz = *Baz::get(); // added *
return 0; // added this
}


g

Jul 22 '05 #3
On 04 Mar 2004 14:46:57 EST, Gianni Mariani <gi*******@mariani.ws> wrote:

class Bar : public Foo<Bar> {
friend class Foo; // added this


I don't believe that this is valid C++ code anyway. There is no
class Foo as it is not a template.

This might be more correct:

friend class Foo<bar>;


Your version has the advantage of compiling with just about everything
under the sun (even MSVC6, gasp). I'm not yet convinced mine was illegal,
though...

//template<class Bar>
template<> // added this
Bar * Foo<Bar>::foo = NULL;


Bar * Foo<Bar>::foo = 0; // :-)

I remember a discussion on this group a while back about using NULL. I
think the conclusion was that NULL was worse than useless and that the
literal 0 was much easier to deal with. It was along the lines of
confusion with the C version (or historic) NULL which was (void *)0 and
the fact the C++ has different semantics with void *.


No doubt; in fact, I ended up changing them all to 0 after I posted so that
I could un-#include the header file in order to get it to compile in strict
mode under certain platforms. I try to post modified code with the fewest
possible changes from the OP's code, though, that aren't critical to the
issue at hand...just to avoid confusion. Not sure it always has that
effect...
-leor

Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #4
gr*********@hotmail.com (Grey Plastic) wrote in message news:<1d**************************@posting.google. com>...
I have several classes that all keep track of static data. However,
the manner that they keep track of static data is identical, and so
I'm using the template<class Child> class Parent { ... }; idiom (don't
know the name of it, if there is one). The problem is that I don't
want any of my classes to have public constructors. They should be
created by a static member function.

This is what I want to do, save for the fact that this doesn't
compile:

template <class Child>
class Foo {
protected:
static Child * foo;
Foo() { }
public:
static Child * get() { if(foo==NULL) return foo=new Child(); return
foo; }
};

class Bar : public Foo<Bar> {
protected:
Bar() { }
};

class Baz : public Foo<Baz> {
protected:
Baz() { }
};

Bar * Foo<Bar>::foo = NULL;
Baz * Foo<Baz>::foo = NULL;

main() {
Bar & bar = Bar::get();
Baz & baz = Baz::get();
}

The definition of "get" is flagged as an error, because the
constructor is protected. I've tried many different things, but
nothing gives me what I want.

In my actual code, I'm keeping a static set of all the instances of
the class, but I produced this simpler snippet for the sake of
discussion.


That idiom called "Singleton". The remaining posts actually answered
all the questions, so I'm off..
Jul 22 '05 #5

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

Similar topics

3
by: Math Preregistration System | last post by:
I'm using a std::list as a container for some pointers to objects, for example list< C* > lst; I would like to sort them using two different...
0
by: Dan Trowbridge | last post by:
He everyone, I just getting started with .NET and I am having a porting problem. I get and error in code that lookes like this (really stripped...
5
by: tuko | last post by:
The following snipet gives a linker error. I don't get it... template<class T> class tran { public: public: private: }; template<class T>...
7
by: Dan Trowbridge | last post by:
He everyone, I am just getting started with .NET and I am having a porting problem. I get and error in code that lookssomething like this...
4
by: Simon | last post by:
Hi, I am trying to create a round kind of function, (to round float and double to int). In the H file I can do the following with not too much...
4
by: Gary li | last post by:
Hi, all I find "template template" class cann't been compiled in VC6 but can ok in Redhat9. I write a test program like as: template<...
1
by: Birthe Gebhardt | last post by:
Dear all, I could not find the way to handle 'not normal' list objects, for example using remove_if, find etc. Example : class Todo { public...
4
by: Grizlyk | last post by:
Hello. Why were base class "typedefs" hidden by template<and explicit usage of them does not work too? Try open only one of the lines in the...
10
by: jason.cipriani | last post by:
Is there any difference between declaring a template parameter as a "typename" or a "class"? E.g. template <class TT f() { } template <typename...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.