Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Looking for intuitive explanation of complex template class!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

90 views
Skip to first unread message
Message has been deleted

Victor Bazarov

unread,
May 13, 2013, 4:08:13 PM5/13/13
to
On 5/13/2013 3:06 PM, Haircuts Are Important wrote:
> Can anyone explain to me in a line by line fashion the below example.

What seems to be the stumbling point?

And what book (or books) are you reading that doesn't (don't) explain at
least some of the elements of this declaration that you find puzzling?
Please post the titles and authors so we could include them in the list
of books to avoid. Thanks!

> What are all the details!

I'm amazed at them too!

>
> template<class T, class one, class two>

Since 'T', 'one' and 'two' are preceded by 'class', they are used in the
context of the 'favorite2' class template as *types*. Does this help?

> class favorite2: public favorite1<T>

This class template inherits from an instantiation of the other one.
Something unclear about it? What?

> {
> protected:
> typedef void (T::* newthing)(one,two);

This is a pointer-to-member declaration folded into a typedef. IOW,
'newthing' is a *typedef-id* (that can be used as a type to declare
variables and arguments, for instance) that is "a pointer to a member of
'T', a non-static function that takes two arguments and returns nothing".

> newthing keeptrying;

Data member declaration. What do you not understand here?

> public:
> favorite (newthing c):keeptrying (c){}

Incorrect, and likely a typo. If this is a constructor (which is what
it looks like), the name has to be 'favorite2', not 'favorite'.

What follows the colon is called the initializer list. Please open your
book and read about it.

> virtual void operator()(one p1, two p2)

Operator function call declaration. What do you not understand here?

> {
> //...
> }
> };
>

V
--
I do not respond to top-posted replies, please don't ask
Message has been deleted

Victor Bazarov

unread,
May 15, 2013, 4:22:49 PM5/15/13
to
On 5/15/2013 4:08 PM, Haircuts Are Important wrote:
> Here's another one that confuses me even more, please explain to me
> what's happening? In particular, I'd like to know what W refers to.
> I'd like an explanation of everything, if possible!

Sorry, I don't have an explanation of everything. You'll have to ask
your course instructor / teacher / professor for that.

>
> [..]
> template <class T, class p1>
> {
> T* W;
> virtual void X (const p1& U) = 0;
> }
>
>

I don't think it's valid code. A declaration is missing after the
closing angle bracket. There's supposed to be something like 'class
blah' or a function declaration (like 'void foo()'). The presence of
the 'virtual' hints that this block (between the two curly braces) is a
class definition, then 'W' is a data member of type 'T' (the first
template argument), and still 'class blah' is missing there. In that
case the closing curly brace is supposed to be followed by a semicolon.

Also, there are other syntax errors in your code. Perhaps you can take
a habit of copy-pasting the code into your messages instead of retyping
it...
Message has been deleted

Victor Bazarov

unread,
May 16, 2013, 10:28:45 AM5/16/13
to
On 5/16/2013 10:01 AM, Haircuts Are Important wrote:
> I found an error in the posted code also. I'll work on deciphering
> this today.
>
> With the two changes, the posted code should have been:
>
> Thanks,
>
> class A:public B::C<D,const E,const F>
> {
> public:
> A():B::C<D, const E, const F> (&D::G){}
> };
>
> template<class T, class p1, class p2>
> class C: public H<T>
> {
> protected:
> typedef void (T::*I)(p1,p2);
> public:
> C (I M):J(M){}
> };
>
> template <class T>
> class H
> {
> protected:
> typedef std::K<int,T*>L;
> typedef typename std::K<int, T*>::iterator M;
> };
>
>
> class D:public N<O, P>
> {
> virtual void Q (const P& R)
> {
> W->X(R.S);
> }
> };
>
> template <class T, class p1>
> class N
> {
> T* W;
> virtual void X (const p1& U) = 0;
> };
>

Does it compile? How do you use class A? I can see that 'N' is used in
'D', 'D' is used in 'A', 'H' is instantiated as the base of 'C', and 'C'
is the base of 'A' (probably from the 'B' namespace), but it's unclear
what are 'E' and 'F'. Is class A is not a class but a template, maybe?
Again, how do you use it?

And, the answer is still the same, the 'W' is a data member of the 'N'
class template. It has the type "pointer to a 'T' (the first template
argument)", so whatever you instantiate the 'N' with, will be the type
of the object to which 'W' is supposed to point. So, when it's
dereferences in 'D::Q', the presumption is that the type 'O' (since
that's the first argument to the 'N' instance from which 'D' inherits)
contains a member 'X' that can be called like a function (with a single
argument of some type (same as the 'S' member of 'P')

If something is unclear, try to formulate and ask specific questions
(instead of "just explain everything to me", since that usually takes a
lifetime and still doesn't achieve the expected result in many cases).
Message has been deleted
Message has been deleted

Ike Naar

unread,
May 16, 2013, 4:48:56 PM5/16/13
to
On 2013-05-16, Haircuts Are Important <clusa...@aol.com> wrote:
> On May 16, 10:28?am, Victor Bazarov <v.baza...@comcast.invalid> wrote:
>> On 5/16/2013 10:01 AM, Haircuts Are Important wrote:
>> > class A:public B::C<D,const E,const F>
>> > {
>> > ? ?public:
>> > ? ? ?A():B::C<D, const E, const F> (&D::G){}
>> > };
>> Does it compile? ?How do you use class A?
>
> It compiles. Here is how class A is used:

Then the code you compiled is not the code you posted.
What you posted has an error on the first line:

class A:public B::C<D,const E,const F>

Class A derives from B::C<D,const E,const F> where
B, D, E and F are undeclared.
Message has been deleted

Andy Champ

unread,
May 17, 2013, 11:07:40 AM5/17/13
to
On 17/05/2013 14:05, Haircuts Are Important wrote:
> The compilable and runable code was not posted in its entirety. It's
> too large, so I summarized it!

One of the standard rules here is to cut down your code to the minimum
_compilable_ code that demonstrates the problem. You'll get more help
that way.

Andy

Ike Naar

unread,
May 17, 2013, 4:51:00 PM5/17/13
to
On 2013-05-17, Haircuts Are Important <clusa...@aol.com> wrote:
> On May 16, 4:48 pm, Ike Naar <i...@ukato.freeshell.org> wrote:
>>
>> Then the code you compiled is not the code you posted.
>> What you posted has an error on the first line:
>>
>> class A:public B::C<D,const E,const F>
>>
>> Class A derives from B::C<D,const E,const F> where
>> B, D, E and F are undeclared.
>
> The compilable and runable code was not posted in its entirety. It's
> too large, so I summarized it!

Good. To summarize the answer to your original question:
> Here's another one that confuses me even more, please explain to me
> what's happening? In particular, I'd like to know what W refers to.
> I'd like an explanation of everything, if possible!

The answer is:

K
0 new messages