 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Grey Plastic Guest
|
Posted: Thu Mar 04, 2004 5:45 pm Post subject: template<class Child> class Parent with protected constructo |
|
|
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.
|
|
| Back to top |
|
 |
Leor Zolman Guest
|
Posted: Thu Mar 04, 2004 6:12 pm Post subject: Re: template<class Child> class Parent with protected constr |
|
|
On 4 Mar 2004 09:45:19 -0800, [email]greyplastic (AT) hotmail (DOT) com[/email] (Grey Plastic) wrote:
| Quote: | 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
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
| Quote: |
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
[email]leor (AT) bdsoft (DOT) com[/email]
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
|
|
| Back to top |
|
 |
Gianni Mariani Guest
|
Posted: Thu Mar 04, 2004 7:46 pm Post subject: Re: template<class Child> class Parent with protected constr |
|
|
Leor Zolman wrote:
| Quote: | On 4 Mar 2004 09:45:19 -0800, [email]greyplastic (AT) hotmail (DOT) com[/email] (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
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;
| Quote: | protected:
Bar() { }
};
class Baz : public Foo<Baz> {
friend class Foo; // added this
|
.... ditto ...
| Quote: | 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 *.
| Quote: |
//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
|
|
| Back to top |
|
 |
Leor Zolman Guest
|
Posted: Thu Mar 04, 2004 9:49 pm Post subject: Re: template<class Child> class Parent with protected constr |
|
|
On 04 Mar 2004 14:46:57 EST, Gianni Mariani <gi2nospam (AT) mariani (DOT) ws> wrote:
| Quote: | 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...
| Quote: | //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
[email]leor (AT) bdsoft (DOT) com[/email]
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
|
|
| Back to top |
|
 |
pw4getter Guest
|
Posted: Fri Mar 05, 2004 3:42 pm Post subject: Re: template<class Child> class Parent with protected constr |
|
|
[email]greyplastic (AT) hotmail (DOT) com[/email] (Grey Plastic) wrote in message news:<1de04d38.0403040945.6727fe08 (AT) posting (DOT) google.com>...
| Quote: | 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
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..
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|