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

  • Thread starter Haircuts Are Important
  • Start date
H

Haircuts Are Important

Can anyone explain to me in a line by line fashion the below example.
What are all the details!

template<class T, class one, class two>
class favorite2: public favorite1<T>
{
protected:
typedef void (T::* newthing)(one,two);
newthing keeptrying;
public:
favorite (newthing c):keeptrying (c){}
virtual void operator()(one p1, two p2)
{
//...
}
};
 
V

Victor Bazarov

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
 
H

Haircuts Are Important

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!

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>
{
T* W;
virtual void X (const p1& U) = 0;
}
 
V

Victor Bazarov

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

V
 
H

Haircuts Are Important

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;
};
 
V

Victor Bazarov

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

V
 
I

Ike Naar

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

Haircuts Are Important

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!
 
I

Ike Naar

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
 

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,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top