| |

VerySource

 Forgot password?
 Register
Search
View: 1304|Reply: 11

C ++ template problem

[Copy link]

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-13 13:00:01
| Show all posts |Read mode
I want to define a map object, where the second parameter of map is also a template type, how should it be defined
For example:
template <class T>
class CTypeObject
{
...
}

template class <T> map <string, CTypeObject <T >> m_Object;

This definition is wrong, but how to define it correctly?
Reply

Use magic Report

0

Threads

41

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 Unknown

Post time: 2020-1-17 13:09:02
| Show all posts
Defined in the template class?
template <class T>
class test
{
map <string, CTypeObject <T>> mobj;
}
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-17 19:45:01
| Show all posts
map is in another class.
Reply

Use magic Report

0

Threads

41

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 United States

Post time: 2020-1-17 21:36:01
| Show all posts
Another class?
Don't understand your question?
If it is not defined in a template class, then it should be fine to write the specific type name
Reply

Use magic Report

0

Threads

41

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-1-18 16:36:01
| Show all posts
Your definition is wrong
map <string, CTypeObject <write specific type>> m_Object;
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-19 10:00:01
| Show all posts
Just because my type is uncertain, I want to write it as any type
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-19 10:54:02
| Show all posts
My code is as follows:
template <class T>
class CTypeObject: public CObject
{
public:
CTypeObject () {}
~ CTypeObject () {}

virtual T&GetValue ()
{
return m_tValue;
}
virtual void SetValue (T&tValue)
{
m_tValue = tValue;
}

virtual void Init () = 0;

protected:
T m_tValue;
};

class CIntegerObject: public CTypeObject <int>
{
public:
CIntegerObject (int nValue) {m_tValue = nValue;}
~ CIntegerObject () {}

virtual void Init () {m_tValue = 0;}
};

///////////////////////////////////////////////////////// /////////////////////////

class CStringObject: public CTypeObject <string>
{
public:
CStringObject (string&strValue) {m_tValue = strValue;}

virtual void Init () {m_tValue = "";}
};

///////////////////////////////////////////////////////// /////////////////////////

class CFieldType
{
public:
CFieldType () {}
~ CFieldType () {}

template <class T>
void Register (string strFieldName, CTypeObject <T> * pTypeObject)
{
// FieldTypeMap :: iterator itr = m_FieldTypeMap.find (strFieldName);
// if (itr == m_FieldTypeMap.end ())
// {
// m_FieldTypeMap.insert (make_pair (strFieldName, pTypeObject));
//}
}
void InitObject ()
{
// for (FieldTypeMap :: iterator itr = m_FieldTypeMap.begin (); itr! = m_FieldTypeMap.end (); itr ++)
// {
// CTypeObject <T> * pTypeObject = itr-> second;
// pTypeObject-> Init ();
//}
}

private:
// template class map <string, CTypeObject <T> *> m_FieldTypeMap;
// FieldTypeMap;

};
Mainly want to use CTypeObject as an abstract base class
Reply

Use magic Report

0

Threads

36

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-1-20 09:45:01
| Show all posts
Your usage is similar to template template parameter, std :: map does not support this usage.
Reply

Use magic Report

1

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-23 10:45:01
| Show all posts
It doesn't seem to work.Neither of your CTypeObject <int> and CTypeObject <string> are derived from the same class.How can you use an abstract base class.


Is it OK to define it like this

template <class T>
class CTypeObject: BaseClass
{
...
}

 map <string, BaseClass> m_Object;
Reply

Use magic Report

0

Threads

8

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-1-23 13:09:02
| Show all posts
I don't know if I can meet your requirements, but the compilation is correct:

..........
///////////////////////////////////////////////////////// /////////////////////////
template <class T>
class CFieldType
{
public:
    typedef map <string, CTypeObject <T> *> FieldTypeMap;
    CFieldType () {}
    ~ CFieldType () {}
    
    // template <class T>
    void Register (string strFieldName, CTypeObject <T> * pTypeObject)
    {
        FieldTypeMap :: iterator itr = m_FieldTypeMap.find (strFieldName);
        if (itr == m_FieldTypeMap.end ())
        {
            m_FieldTypeMap.insert (make_pair (strFieldName, pTypeObject));
        }
    }
    void InitObject ()
    {
        for (FieldTypeMap :: iterator itr = m_FieldTypeMap.begin (); itr! = m_FieldTypeMap.end (); itr ++)
        {
            CTypeObject <T> * pTypeObject = itr-> second;
            pTypeObject-> Init ();
        }
    }
    
private:
    FieldTypeMap m_FieldTypeMap;
    
};
void main ()
{
    CFieldType <string> sMap;
    sMap.InitObject ();
    string x ("X");
    CStringObject * ps = new CStringObject (x);
    sMap.Register (x, ps);
}
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list