Note that there are some explanatory texts on larger screens.

plurals
  1. PODatatypes and polymorphism
    primarykey
    data
    text
    <p>I have a design question. I want custom datatypes implementing an interface. For example, using templates is simply (maybe next design isn't correct -because I can do a generic class instead of the next- but clarifies my goal):</p> <pre><code>template &lt;typename T&gt; class IDatatype { public: virtual T getData() const = 0; virtual void setData(T pData) = 0; }; class MyChar: public IDatatype&lt;char&gt; { public: void setData(char pData){...} char getData() const{...} private: char _data; }; class MyInt: public IDatatype&lt;int&gt; { public: void setData(int pData){...} int getData() const{...} private: int _data; }; IDatatype&lt;int&gt; *data = new MyInt(); // parametrized interface, bad idea :( data-&gt;getData(); // it works ok </code></pre> <p>From previous classes, it is easy to get the attribute corresponding to each _data class member. My question: </p> <blockquote> <p>Is there any way (change design, etc.) to implement generic setter and getter in IDatatype and for any type and thus manipulate the _data attribute of each class without using templates in the interface?</p> </blockquote> <p>For example: </p> <pre><code>class IDatatype { public: // pure virtual getters and setters for specialized _data fields. Here is my design question. }; class MyChar: public IDatatype { public: void setData(char pData){...}; char getData(){...}; private: char _data; }; class MyInt: public IDatatype { public: void setData(int pData){...}; int getData(){...}; private: int _data; }; IDatatype *intData = new MyInt(); // no parametrized interface! intData-&gt;getData(); // how can I create this method from IDatatype? IDatatype *charData = new MyChar(); charData-&gt;getData(); // the same here </code></pre> <p>NOTE: I have no good english, apologize for any errors :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload