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

Discussion in 'C++' started by Haircuts Are Important, May 13, 2013.

  1. 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)
    {
    //...
    }
    };
     
    Haircuts Are Important, May 13, 2013
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, May 13, 2013
    #2
    1. Advertising

  3. 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;
    }
     
    Haircuts Are Important, May 15, 2013
    #3
  4. 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...

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, May 15, 2013
    #4
  5. 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;
    };
     
    Haircuts Are Important, May 16, 2013
    #5
  6. 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).

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, May 16, 2013
    #6
  7. On May 16, 10:28 am, Victor Bazarov <> wrote:
    > 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?


    It compiles. Here is how class A is used:

    class Z1 : public Z2::Z3
    {
    private:
    A Z4;
    }

    > Is class A is not a class but a template, maybe?


    A is a class.

    Thanks,
     
    Haircuts Are Important, May 16, 2013
    #7
  8. E is C and F is a struct called AA!

    Thanks,
     
    Haircuts Are Important, May 16, 2013
    #8
  9. Haircuts Are Important

    Ike Naar Guest

    Re: Looking for intuitive explanation of complex templateclass!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    On 2013-05-16, Haircuts Are Important <> wrote:
    > On May 16, 10:28?am, Victor Bazarov <> 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.
     
    Ike Naar, May 16, 2013
    #9
  10. On May 16, 4:48 pm, Ike Naar <> 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!
     
    Haircuts Are Important, May 17, 2013
    #10
  11. Haircuts Are Important

    Ike Naar Guest

    Re: Looking for intuitive explanation of complex templateclass!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    On 2013-05-17, Haircuts Are Important <> wrote:
    > On May 16, 4:48 pm, Ike Naar <> 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
     
    Ike Naar, May 17, 2013
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. elena

    Analytical or Intuitive?

    elena, Apr 8, 2005, in forum: Java
    Replies:
    25
    Views:
    1,176
    Patricia Shanahan
    Apr 12, 2005
  2. Replies:
    0
    Views:
    388
  3. christopher diggins
    Replies:
    16
    Views:
    792
    Pete Becker
    May 4, 2005
  4. Paul Brown
    Replies:
    7
    Views:
    398
    Christian Bau
    Oct 10, 2003
  5. A L
    Replies:
    1
    Views:
    531
    Alf P. Steinbach /Usenet
    Aug 25, 2010
Loading...

Share This Page