Not sure but related to Template Class

Hi,

This is my first post and I am learning c++. I am trying to create a programe like this:

1
2
3
n1 = NAttrib()
n2 = NAttrib()
out = NAttrib()


NAttrib class can be considered as general purpose attribute.

1
2
f1 = FloatAttrib(1.0)
f2 = FloatAttrib(2.0)


FloatAttribute class is like float type but with some extra spices. It has operator overloading functions such as add, multiply etc.

1
2
c1 = ComplexAttrib(arguments)
c2 = ComplexAttrib(arguments)


ComplexAttrib is a custom data type. It has operator overloading functions such as add, multiply etc.

1
2
n1.Set(f1)
n2.Set(f2)


Here we are setting FloatAttribute instances in out general purpose attribute.

out = n1 + n2
This should call add operator function defined in FloatAttribute class & out should become FloatAttribute

1
2
n1.Set(c1)
n2.Set(c2)

Here we are setting ComplexAttribute instances in out general purpose attribute.

out = n1 + n2
This should call add operator function defined in ComplexAttribute class & out should become ComplexAttribute

I am reading couple of books and also googling but still I am not sure how do I implement.
How to create NAttrib, FloatAttribute and ComplexAttribute class?

I am not sure but Template Class could be a solution?

Cheers
Last edited on
closed account (zb0S216C)
Possibly something like this:

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
template <typename AttribType>
class Attribute 
{
    public:
        Attribute(const AttribType &Init = AttribType())
            : AttribData__(Init) { }

        Attribute(const Attribute<AttribType> &Attrib)
            : AttribData__(Attrib()) { }

    protected:
	AttribType AttribData__;

    public:
        AttribType &operator () ()
        { return(this->AttribData__); }

        AttribType &operator () (const AttribType &New)
        { return(this->AttribData__ = New); }

        AttribType &operator () () const
        { return(this->AttribData__); }
};

int main()
{
    Attribute<int> MyIntAttribute(10);

    int MyValue(MyIntAttribute());
}

Wazzak
Last edited on
Topic archived. No new replies allowed.