c++ - pointer initialization with template typename -


i've got class inherite template class. initialize pointer template argument. how can that?

algorithm.h:

#ifndef algorithm_h #define algorithm_h  #include <iostream> using namespace std;  template <typename t>  class algorithm { protected:     t data;     t result;  //(*)     int datasize;     int resultsize; public:     algorithm(){}     algorithm(t in, int insize){         cout<<"algorithm constructor!"<<endl;         data = in;         datasize = insize;         resultsize = datasize;         result = new t;    //(**)         (int = 0; i<this->resultsize; i++){             this->result[i] = 0;             cout<<"i: "<<i<<" *(this->result+i) = "<<this->result[i]<<endl;         }     }  #endif // algorithm_h 

error in (**) line:

/home/user/projects/algorithms/algorithm.h:23: error: cannot convert 'float**' 'float*' in assignment result = new t; ^

i change line (*) not favourite solution inconsistent data - rather so. how can initialize feel result table 0s then?

if don't want change (*) line t* result, can use std::remove_pointer<> type trait (c++11 or later)

result = new typename std::remove_pointer<t>::type(); // single element value-initialized 

or (if want array, want)

result = new typename std::remove_pointer<t>::type [resultsize]; // array of resultsize elements 

finally, can value-initialize array as

result = new typename std::remove_pointer<t>::type [resultsize]{}; // value-initialized array 

however find solution awkward (to least), , more clear if use t* result instead.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -