Home > Net >  Set pointer to templated class in class with different template
Set pointer to templated class in class with different template

Time:03-08

I would like to create a class in C that can store a pointer to another class. I would also like that the pointer always starts will nullptr (preferably not in the constructor).

I have the following example.

template <class one>
class BaseA
{
    public:
    ~BaseA(){}

    void computeSomething(){std::cout << "function1 is being called" << std::endl;}
};

template <class two>
class BaseB
{
    public:
    ~BaseB(){}

    template<class one>
    BaseA<one>* myPtr = nullptr;

    template<class one>
    void setPtr(BaseA<one>& obj ){ myPtr = &obj; }
};

But upon compilation I am getting the error:

 error: data member ‘myPtr’ cannot be a member template

An an end result I was hopping to have something like:

template<class two> 
void iAmAFunction(BaseB<two>& obj)
{
   obj.myPtr->computeSomething();
}

int main(int argCount, char *args[])
{

    BaseB<int> one;
    BaseA<double> two;

    one.setPtr(two);

    iAmAFunction<int>(banana1);

    std::cout << "End " << std::endl;
 
}

How can I fix this?

CodePudding user response:

I would like to create a class in C that can store a pointer to another class. I would also like that the pointer always starts will nullptr

That is:

class BaseA {
    public:      
    int a = 2;
};

class BaseB {
    public:
    BaseA* myPtr = nullptr;
    void setPtr(BaseA* obj ){ myPtr = obj; }
};

I want one templated class (B) to hold a pointer to another templated class A(different templat).

There are no pointers to templates. You can store a pointer to an object. And the type of that can be the instantiation of a class template. However, there are no member templates as the errors says. If BaseA is a class template then you can introduce a common base for all instantiations of the template and store pointers to that:

class Base {};

template <typename T>
class BaseA : Base{
    public:
    int a = 2;
};

class BaseB {
    public:
    Base* myPtr = nullptr;
    void setPtr(Base* obj ){ myPtr = obj; }
};

Now BaseB can be a class template as well, that does not change anything.

CodePudding user response:

Here is possible solution. Would appreciate other opinions if you have any .

class Base {

    void virtual someVirtualFunction(){}
};

template <class one>
class BaseA: public Base
{

    public:
    ~BaseA(){}
    void computeSomething(){std::cout << "function1 is being called" << std::endl;}
    int a = 2;
};

template <class two>
class BaseB
{
    public:
    ~BaseB(){}

    Base* myPtr = nullptr;
    
    void setPtr(Base& obj ){ myPtr = &obj; }
};

template<class two> 
void iAmAFunction(BaseB<two>& obj)
{
   BaseA<double>* check = dynamic_cast<BaseA<double>*>(obj.myPtr);
   if (!check)
   {
       throw std::runtime_error("Not the correct type");
   }

   check->computeSomething();
}


int main(int argCount, char *args[])
{

    BaseB<int> obj1;
    BaseA<double> obj2;

    obj1.setPtr(obj2);

    iAmAFunction<int>(obj1);

    std::cout << "End " << std::endl;
 
}
  • Related