using 'this' in template header

i have 1 class:
from here: http://www.codeguru.com/cpp/cpp/cpp_mfc/article.php/c4031/Implementing-a-Property-in-C.htm
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
template <typename Container, typename ValueType, void (Container::*setptr)(ValueType t),ValueType (Container::*getptr)(), int nPropType=3>
class property
{
public:
    property()
    {
        m_cObject= cObject;//aqui esta 1 problema:(
        Set = setptr;
        Get = getptr;
    }

    void setContainer(Container* cObject)
    {
        m_cObject = cObject;
    }

    //-- Overload the '=' sign to set the value using the set
    //   member --
    ValueType operator =(const ValueType& value)
    {
        assert(m_cObject != NULL);
        assert(Set != NULL);
        (m_cObject->*Set)(value);
        return value;
    }


    //-- To make possible to cast the property class to the
    //   internal type --
    operator ValueType()
    {
        assert(m_cObject != NULL);
        assert(Get != NULL);
        return (m_cObject->*Get)();
    }
    friend istream &operator>>( istream  &input, property &d )
    {
        ValueType v;
        input >>v;
        d = v;
        return input;
    }




    friend istream& getline (istream&  is, property &str)
    {
        ValueType v;
        getline(is,v);
        str = v;
        return is;
    }


private:
  Container* m_cObject;  //-- Pointer to the module that
                         //   contains the property --
  void (Container::*Set)(ValueType value);
                         //-- Pointer to set member function --
  ValueType (Container::*Get)();
                         //-- Pointer to get member function --
};

(but i did the version 2;))
my question is: can i put these:

1
2
3
4
void setContainer(Container* cObject)
    {
        m_cObject = cObject;
    }

in header template?
i did these:

1
2
3
4
5
6
7
8
9
10
11
template <typename Container,Container* cObject, typename ValueType, void (Container::*setptr)(ValueType t),ValueType (Container::*getptr)(), int nPropType=3>
class property
{
public:
    property()
    {
        m_cObject= cObject;//aqui esta 1 problema:(
        Set = setptr;
        Get = getptr;
    }
.................

how use it:
property <person2,this,string,&person2::setname,&person2::getname> Name;
(person2 is the class name)
errors messages:

"C:\Users\Joaquim\Documents\CodeBlocks\testevents\main.cpp|31|error: invalid use of 'this' at top level|"

"C:\Users\Joaquim\Documents\CodeBlocks\testevents\main.cpp|31|error: template argument 2 is invalid|"

can anyone advice me? how can i use the 'this' in template header?
Last edited on
closed account (o3hC5Di1)
Hi there,

Template parameters are not usually variables, only typenames (classnames).

template <typename Container,Container* cObject, //<- this won't work

A better option is to pass in the object through the constructor.
For more information on the reason:

http://stackoverflow.com/questions/5687540/non-type-template-parameters/5687553#5687553
http://stackoverflow.com/questions/15896579/why-cant-a-struct-be-passed-as-value-as-template-non-type-parameter

All the best,
NwN
Topic archived. No new replies allowed.