Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conversion between 2 types with different const qualifiers

This is a short example of the code I want to use:

template <class T>
class B
{
public :
    bool func1(const T& t)
    {
        // do something
    }
};


class A
{
    B<int*> b;
public:
    void func2(const int* a)
    {
        b.func1(a);
    }
};

I'm getting this error :

error C2664: 'B::func1' : cannot convert parameter 1 from 'const int *' to 'int *const &'

is a way to solve this without changing the functions declarations and without using const_cast?

Edit:

some information behind the problem

  1. B is actually a container class I wrote (lets say a list)

  2. A is a class using that list

  3. func1 is a function that need to find if an element is in the list or not

  4. func2 is a function that recieves an element to remove from the list

like image 401
Adam Avatar asked Dec 07 '25 01:12

Adam


1 Answers

When int* is used to instantiate B, the function

void func1(const T& t) {}

is equivalent to:

void func1(int* const& t) {}

A parameter of type const int* is not compatible with int* const&.

You need to rethink your functions a bit.

Update

Use of B<int> instead of B<int*> in A might be what you are looking for.

class A
{
      B<int> b;
   public:
      void func2(const int* a)
      {
         b.func1(*a);
      }
};
like image 168
R Sahu Avatar answered Dec 08 '25 15:12

R Sahu