Re: How to make obj of temp in temp ???



"IceColdFire" <IceColdFire@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:799EED05-7526-4158-9BF9-75BB2EB4B4F5@xxxxxxxxxxxxxxxx
> Hi,
> How do I make objects of class D :::
> ---
> template<class T>
> class C
> {
> public:
> T i;
> C(T arg):i(arg){ }
> template<class U>
> class D
> {
> U j;
> D(U arg):i(arg){ }
> };
> };
> ----
> in main()
> ???
> Urgent !!!
> a.a.cpp

First of all, in it's present form you cannot. The class you have defined
within your C class, D, has no public constructor and therefore cannot be
instantiated. Then you need to fix the typo in the variable initialization
in D's constructor. Then you could declare a variable of class D by doing
this, although I see no good reason or benefit for implementing anything
like this...

template< class T >
class C
{
public:
T i;
C( T arg ) : i( arg ) { }
template< class U >
class D
{
public:
U j;
D( U arg ) : j( arg ) { }
};
};

C<int>::D<double> d( 5.2 );

You are effectively taking up memory that you cannot access and/or use,
class C's integer (in this case) cannot be accessed from d. Nor could any
member functions if they existed, unless of course if they are static
members.

But maybe I just cannot see a good reason, does anyone else see a benefit to
this?

--
============
Frank Hickman
Microsoft MVP
NobleSoft, Inc.
============
Replace the _nosp@m_ with @ to reply.


.



Relevant Pages

  • Re: How to make obj of temp in temp ???
    ... Then you need to fix the typo in the variable initialization ... > template< class T> ... > member functions if they existed, unless of course if they are static ... > But maybe I just cannot see a good reason, does anyone else see a benefit to ...
    (microsoft.public.vc.language)
  • RE: Update links within a spreadsheet
    ... Currently I have set up an excel spreadsheet that allows ... >> system into the template, then make adjustments to the current information ... For some reason the only way that I can get the numbers ... >> to update is when I go to the formula bar of that cell and then hit enter. ...
    (microsoft.public.excel.programming)
  • Re: template member functions of a templated class
    ... I want a function which takes a range of iterators ... > able to do this with one, or at most two, templated member functions. ... C is not a template. ... See a good book on a better explanation of what InputIterator ...
    (comp.lang.cpp)
  • template member functions of a templated class
    ... (which dereference to type T, or in some cases to type T*), and adds all ... able to do this with one, or at most two, templated member functions. ... C is not a template. ... I looked up the STL vector constructor which takes a range of iterators, ...
    (comp.lang.cpp)
  • Re: Style problem
    ... there is a reason for that. ... styles in the template, but there are a few missing. ... I had previously created a whole heap of styles in Word using a font ... change this font from Adobe Garamond to just regular garamond. ...
    (microsoft.public.mac.office.word)