I am creating a library which allows the user to store a pointer and later retrieve the pointer.
I have a class which stores the pointer of a generic type.
template <typename generic_type>
class A
{
public:
A() {}
A(generic_type *value) : data(value) {}
generic_type *getData()
{
return data;
}
private:
generic_type *data;
};
I want to also store the instances of these class template in a vector, so i inherited from a base class.
class B
{
};
template <typename generic_type>
class A : public B
{
...
};
class test_class
{
};
std::vector<B *> list;
// -------- example -------
// test_class *test = new test_class();
// list.push_back(new A<test_class>(test));
// list.push_back(new A<test_class>(test));
// list.push_back(new A<test_class>(test));
When retrieving from the list i will get a pointer to base class.
To call the functions in derived class A, it will have to be cast.
// example
B *bp = list.at(0);
A<test_class> *ap = static_cast<A<test_class> *>(bp);
I do not want the user to manually have to cast the base pointer themselves, but be able to call the getData()
function in A
which will return the actual data based on the generic_type
.
// preferred API usage
B *bp = list.at(0); // they will get B pointer in another way but shown here for simplicity
test_class *t = bp->getData();
Looking around for calling child function from parent class, i came upon CRTP, which allowed me to call the function in A
;but now i am unable to store it in a vector as B
will be templated. So i derived B
from another class referencing code from this post.
This allowed me to store it in a vector but i cant seem to find a way to call the function with generic return type and return it.
class C
{
public:
virtual void func() = 0;
};
template <typename derived>
class B : public C
{
public :
void func() // cant change to templated return type as virtual function cant be templated
{
derived *p = static_cast<derived *>(this);
p->getData(); // how to return the value from this ?
}
};
template <typename generic_type>
class A : public B<A<generic_type>>
{
...
};
std::vector<C *> list;
// -------- example -------
// this allows me to do
C *cp = list.at(0);
cp->func(); // will call getData() indirectly
// but am unable to get the returned data
I am not familiar with newer features or design patterns as i have not used c++ for a few years. Is it possible to get the returned data? or is there a different method or pattern i could use to achieve the desired API usage scenario?