template question using named constructor

B

bob holder

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
 
J

John Harrison

bob holder said:
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
 
C

Claudio Jolowicz

bob holder said:
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 said:
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.
 
B

bob holder

Claudio Jolowicz said:
bob holder said:
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 said:
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.

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
 
S

Siemel Naran

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.
 
B

bob holder

Siemel Naran said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top