Hi,

I am trying to create a programe like this:

n1 = NAttrib()
n2 = NAttrib()
out = NAttrib()

NAttrib class can be considered as general purpose attribute.

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.

c1 = ComplexAttrib(arguments)
c2 = ComplexAttrib(arguments)

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

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

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

out = n1 + n2

This should call add operator function defined in FloatAttribute class & out should become FloatAttribute

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

Here we are setting ComplexAttribute instances in our 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

I think you should use a combination of inheritance and templates. Look them up.

You could use inheritance (OOP) but it would be very wasteful for such small attributes.

Basically, what you need is a class template like Boost.Variant as your "NAttrib" implementation, while your FloatAttribute and ComplexAttribute can remain very simple (no template, no inheritance). This is fairly advanced and uses templates and overloading in non-trivial ways, of course.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.