473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

template question using named constructor

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
Jul 22 '05 #1
5 941

"bob holder" <holder25AThotmailDOTcom> wrote in message
news:Hw********@news.boeing.com...
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
Jul 22 '05 #2
On Wed, 21 Apr 2004, John Harrison wrote:

"bob holder" <holder25AThotmailDOTcom> wrote in message
news:Hw********@news.boeing.com...
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

Jul 22 '05 #3

"Claudio Jolowicz" <cj***@doc.ic.ac.uk> wrote in message
news:Pi*******************************@kiwi.doc.ic .ac.uk...
On Wed, 21 Apr 2004, John Harrison wrote:

"bob holder" <holder25AThotmailDOTcom> wrote in message
news:Hw********@news.boeing.com...
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

Jul 22 '05 #4
"bob holder" <holder25AThotmailDOTcom> wrote in message
news:Hw********@news.boeing.com...
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.
Jul 22 '05 #5

"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:Ij*******************@bgtnsc05-news.ops.worldnet.att.net...
"bob holder" <holder25AThotmailDOTcom> wrote in message
news:Hw********@news.boeing.com...
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
Jul 22 '05 #6

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

Similar topics

4
by: Sebastian Faust | last post by:
Hi, I have 4 questions related to templates. I wanna do something like the following: template<typename T> class Template { public: Template_Test<T>()
3
by: jack | last post by:
Hi there, I have a function F(x, y, z) and I want to calculate f(a) + f(b), where f(x) = F(x, x, z0) z0 is fixed. Suppose somebody wrote a routine called "Compute" which simply computes f(a)...
9
by: blueblueblue2005 | last post by:
Here is a very simple template example, whenever I seperate the header and implementation file, I got "undefined reference to member function error. and if I put the header and definition in one...
8
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int,...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
5
by: Joe Van Dyk | last post by:
Say I have the following class: using std::string; class Player { public: Player() : name(""), age(""), other_stuff("") {} private: string name; string age;
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
3
by: liam_herron | last post by:
Here is my code: #include <iostream> template<typename T> class RawPtr { protected:
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.