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 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
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
"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
"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.
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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>()
|
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)...
|
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...
|
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,...
|
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...
|
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;
|
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...
|
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...
|
by: liam_herron |
last post by:
Here is my code:
#include <iostream>
template<typename T>
class RawPtr
{
protected:
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
| |