template question using named constructor

Discussion in 'C++' started by bob holder, Apr 21, 2004.

  1. bob holder

    bob holder Guest

    Hi folks, I have a question. Can I use a named constructor with templates?
    If yes I am not sure the syntax. For example:

    template <size_t N> class ABC {

    public:
    typedef ABC<N> *handle;

    static handle create(size_t s) { return new ABC<s>(); }

    private:
    ABC();
    };

    main () {
    ABC<5>::handle my_abc;

    my_abc = ABC<5>.create(5); // compile error this line
    }

    I need to control the construction process, hence the need to hide the
    default ctor.

    thanks,
    bob
    bob holder, Apr 21, 2004
    #1
    1. Advertising

  2. "bob holder" <holder25AThotmailDOTcom> wrote in message
    news:...
    > Hi folks, I have a question. Can I use a named constructor with templates?
    > If yes I am not sure the syntax. For example:
    >
    > template <size_t N> class ABC {
    >
    > public:
    > typedef ABC<N> *handle;
    >
    > static handle create(size_t s) { return new ABC<s>(); }
    >
    > private:
    > ABC();
    > };
    >
    > main () {
    > ABC<5>::handle my_abc;
    >
    > my_abc = ABC<5>.create(5); // compile error this line
    > }
    >
    > I need to control the construction process, hence the need to hide the
    > default ctor.
    >
    > thanks,
    > bob
    >


    Like this

    template <size_t N> class ABC
    {
    public:
    typedef ABC<N> *handle;
    static handle create() { return new ABC<N>(); }
    };

    int main ()
    {
    ABC<5>::handle my_abc;

    my_abc = ABC<5>::create();
    }

    john
    John Harrison, Apr 21, 2004
    #2
    1. Advertising

  3. On Wed, 21 Apr 2004, John Harrison wrote:

    >
    >"bob holder" <holder25AThotmailDOTcom> wrote in message
    >news:...
    >> Hi folks, I have a question. Can I use a named constructor with templates?
    >> If yes I am not sure the syntax. For example:
    >>


    [snip]

    >> I need to control the construction process, hence the need to hide the
    >> default ctor.
    >>
    >> thanks,
    >> bob
    >>

    >
    >Like this
    >


    #include <cstdlib>

    >template <size_t N> class ABC
    >{
    >public:
    > typedef ABC<N> *handle;
    > static handle create() { return new ABC<N>(); }


    private: //I think you forgot this bit
    ABC() {/*whatever*/}

    >};
    >
    >int main ()
    >{
    > ABC<5>::handle my_abc;
    >
    > my_abc = ABC<5>::create();
    >}
    >


    Bob, do you really want to use a template here? The obvious solution
    would have been:

    class ABC
    {
    public:
    ABC(size_t);
    };

    This automatically hides the default constructor.

    --
    Claudio Jolowicz
    http://www.doc.ic.ac.uk/~cj603
    Claudio Jolowicz, Apr 21, 2004
    #3
  4. bob holder

    bob holder Guest

    "Claudio Jolowicz" <> wrote in message
    news:p...
    > On Wed, 21 Apr 2004, John Harrison wrote:
    >
    > >
    > >"bob holder" <holder25AThotmailDOTcom> wrote in message
    > >news:...
    > >> Hi folks, I have a question. Can I use a named constructor with

    templates?
    > >> If yes I am not sure the syntax. For example:
    > >>

    >
    > [snip]
    >
    > >> I need to control the construction process, hence the need to hide the
    > >> default ctor.
    > >>
    > >> thanks,
    > >> bob
    > >>

    > >
    > >Like this
    > >

    >
    > #include <cstdlib>
    >
    > >template <size_t N> class ABC
    > >{
    > >public:
    > > typedef ABC<N> *handle;
    > > static handle create() { return new ABC<N>(); }

    >
    > private: //I think you forgot this bit
    > ABC() {/*whatever*/}
    >
    > >};
    > >
    > >int main ()
    > >{
    > > ABC<5>::handle my_abc;
    > >
    > > my_abc = ABC<5>::create();
    > >}
    > >

    >
    > Bob, do you really want to use a template here? The obvious solution
    > would have been:
    >
    > class ABC
    > {
    > public:
    > ABC(size_t);
    > };
    >
    > This automatically hides the default constructor.
    >
    > --
    > Claudio Jolowicz
    > http://www.doc.ic.ac.uk/~cj603
    >
    >
    >


    Thank you both John and Claudio. Just needed to get the syntax straight.

    Claudio, you are correct that the default ctor needs to be declared as you
    listed. I trimmed down the snippet a bit too much. I do want a template for
    other reasons not obvious by such a small post.

    Thanks to you both.
    bob
    bob holder, Apr 21, 2004
    #4
  5. bob holder

    Siemel Naran Guest

    "bob holder" <holder25AThotmailDOTcom> wrote in message
    news:...

    > template <size_t N> class ABC {
    >
    > public:
    > typedef ABC<N> *handle;
    >
    > static handle create(size_t s) { return new ABC<s>(); }
    >
    > private:
    > ABC();
    > };


    This is not possible. Non-type template arguments must be integral
    constants known at compile time, but you could read 's' from a file and then
    call create(s).

    You can have just

    static handle create() { return new ABC<N>(); }

    The other thing is that ABC<6> does not derive from ABC<5>, but your code
    suggests casting an ABC<s> into an ABC<N> which is illegal.

    But it's not clear what you're asking for.
    Siemel Naran, Apr 21, 2004
    #5
  6. bob holder

    bob holder Guest

    "Siemel Naran" <> wrote in message
    news:IjBhc.7850$...
    > "bob holder" <holder25AThotmailDOTcom> wrote in message
    > news:...
    >
    > > template <size_t N> class ABC {
    > >
    > > public:
    > > typedef ABC<N> *handle;
    > >
    > > static handle create(size_t s) { return new ABC<s>(); }
    > >
    > > private:
    > > ABC();
    > > };

    >
    > This is not possible. Non-type template arguments must be integral
    > constants known at compile time, but you could read 's' from a file and

    then
    > call create(s).
    >
    > You can have just
    >
    > static handle create() { return new ABC<N>(); }
    >
    > The other thing is that ABC<6> does not derive from ABC<5>, but your code
    > suggests casting an ABC<s> into an ABC<N> which is illegal.
    >
    > But it's not clear what you're asking for.
    >
    >


    Right you are Siemel. I understood the compile time constant issue, I just
    stumbled over the syntax.

    I do not require ABC<6> to derive from ABC<5>. ABC does not mean abstract
    base class here. I should have chosen a better name, perhaps xyzzy.

    Thanks,
    bob
    bob holder, Apr 22, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexander Stippler

    template constructor in non-template class

    Alexander Stippler, Dec 1, 2004, in forum: C++
    Replies:
    2
    Views:
    9,712
    Tom Widmer
    Dec 1, 2004
  2. Steve
    Replies:
    10
    Views:
    896
    Matthias Kaeppler
    Jul 23, 2005
  3. benben
    Replies:
    1
    Views:
    410
    Alf P. Steinbach
    Feb 15, 2006
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,222
  5. cinsk
    Replies:
    35
    Views:
    2,602
    James Kanze
    Oct 11, 2010
Loading...

Share This Page