Seeking advice on class template that uses 'this' keyword

I intend to have several derived classes that have the same function that utilizes the this keyword to referred to objects in the derived classes. The issue is that when the function is defined in the base class, this refers to base class objects instead of derived class objects.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
    template< typename Type >
    class ListClass
    {
        protected:
            static std::stack< Type > clipboard;    // For temporarily storing objects for copy-paste.
        public:
            void cpy(); // Copies object to clipboard
    };

    // Define ListClass::clipboard
    template < typename Type >
    std::stack< Type > ListClass< Type >::clipboard;


    // Copies object to clipboard
    template < typename Type >
    void ListClass< Type >::cpy()
    {
        if ( !clipboard.empty() )
        {
            clipboard.pop();
        }

        clipboard.push( *this ); // This is where the problem  
    }


My current solution is to makes ListClass::cpy a static generic function and then have it called inside a derived class function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    template< typename Type >
    class ListClass
    {
        protected:
            static std::stack< Type > clipboard;    // For temporarily storing objects for copy-paste.
        public:
            static void cpy(); // Copies object to clipboard
    };

    // Define ListClass::clipboard
    template < typename Type >
    std::stack< Type > ListClass< Type >::clipboard;


    // Copies object to clipboard
    template < typename Type >
    void ListClass< Type >::cpy( Type obj )
    {
        if ( !clipboard.empty() )
        {
            clipboard.pop();
        }

        clipboard.push( obj );  
    }

    class DerivedClass :
        public ListClass< DerivedClass >
    {
        public:
            void cpy2();  // Calls cpy( Type obj );
            /* additional stuff */
        private:
            /* additional stuff */

    };

    void DerivedClass::cpy2()
    {
        cpy( *this );
    }


Is there a solution such that I don't have to define a cpy2() function for each derived class?
Last edited on
The issue is that when the function is defined in the base class, this refers to base class objects instead of derived class objects.

That is correct, but remember you don't need to always use the *this pointer to access the current (derived) member functions or member variables. If the derived class hasn't overloaded the base class member you can access the base class member by omitting the *this pointer. If the derived class has overloaded the base class member then you need to properly scope the base::member.


Last edited on
Is there something wrong with casting since the template provides you the type you're working with?

1
2
3
4
5
6
7
8
9
10
11
// Copies object to clipboard
template < typename Type >
void ListClass< Type >::cpy()
{
    if (!clipboard.empty())
    {
        clipboard.pop();
    }

    clipboard.push(*static_cast<Type*>(this));
}
Thank you! Casting works. I hadn't considered casting because I haven't had much practice with it.
Topic archived. No new replies allowed.