Conversion from Int** to Const Int**

Conversion from int** to const int**

it is because you are trying to convert from int** to const int**

int ** v = new int * [10]; // v is int**
foo(v); //but foo takes const int**
  • int ** is: "a pointer to a pointer to an integer".
  • const int ** is: "a pointer to a pointer to a constant integer".

The use of const is a contract and you cannot meet this contract by going through the indirection of two references.

From the standard:

const char c = 'c';
char* pc;
const char** pcc = &pc; // not allowed (thankfully!)
^^^ here the bundit is hidden under const: "I will not modify"
*pcc = &c; // *pcc is "pointer to const" right? so this is allowed...
*pc = 'C'; // would allow to modify a const object, *pc is char right?

so it would be possible to modify const char always, just use procedure above.

And also:

char *s1 = 0;
const char *s2 = s1; // OK...
char *a[MAX]; // aka char **
const char * const*ps = a; // no error!

nice cite from the link below:

By way of analogy, if you hide a criminal under a lawful disguise, he
can then exploit the trust given to that disguise. That's bad.

http://www.parashift.com/c++-faq-lite/constptrptr-conversion.html

related to this is also invalid conversion Derived** → Base**. If it were legal to convert Derived** → Base**, the Base** could be dereferenced (yielding a Base*), and the Base* could be made to point to an object of a different derived class, which could cause serious problems. See why:

class Vehicle {
public:
virtual ~Vehicle() { }
virtual void startEngine() = 0;
};

class Car : public Vehicle {
public:
virtual void startEngine();
virtual void openGasCap();
};

class NuclearSubmarine : public Vehicle {
public:
virtual void startEngine();
virtual void fireNuclearMissle();
};

int main()
{
Car car;
Car* carPtr = &car;
Car** carPtrPtr = &carPtr;
Vehicle** vehiclePtrPtr = carPtrPtr; // This is an error in C++
NuclearSubmarine sub;
NuclearSubmarine* subPtr = ⊂
*vehiclePtrPtr = subPtr;
// This last line would have caused carPtr to point to sub !
carPtr->openGasCap(); // This might call fireNuclearMissle()!
...
}

http://www.parashift.com/c++-faq-lite/derivedptrptr-to-baseptrptr.html

consider:

class Vehicle {
public:
virtual ~Vehicle() { }
virtual void startEngine() = 0;
};
class Car : public Vehicle {
public:
virtual void startEngine(){printf("Car engine brummm\n");}
virtual void openGasCap(){printf("Car: open gas cap\n");}
virtual void openGasCap2(){printf("Car: open gas cap2\n");}
virtual void openGasCap3(){printf("Car: open gas cap3\n");}
virtual void openGasCap4(){printf("Car: open gas cap4\n");}
};
class NuclearSubmarine : public Vehicle {
public:
int i;
virtual void startEngine(){printf("Nuclear submarine engine brummm\n");}
virtual void fireNuclearMissle3(){printf("Nuclear submarine: fire the missle3!\n");}
virtual void fireNuclearMissle(){printf("Nuclear submarine: fire the missle!\n");}
virtual void fireNuclearMissle2(){printf("Nuclear submarine: fire the missle2!\n");}
};
int main(){
Car car; Car* carPtr = &car;
Car** carPtrPtr = &carPtr;
//Vehicle** vehiclePtrPtr = carPtrPtr; // This is an error in C++, But:
Vehicle** vehiclePtrPtr = reinterpret_cast<Vehicle**>(carPtrPtr);
NuclearSubmarine sub; NuclearSubmarine* subPtr = ⊂
*vehiclePtrPtr = subPtr; // carPtr points to sub !
carPtr->openGasCap(); // Nuclear submarine: fire the missle3!
carPtr->openGasCap2(); // Nuclear submarine: fire the missle!
carPtr->openGasCap3(); // Nuclear submarine: fire the missle2!
//carPtr->openGasCap4(); // SEG FAULT
}

How to convert int to const int to assign array size on stack?

How should I typecast n1 to treat it as a const int?

You cannot, not for this purpose.

The size of the array must be what is called an Integral Constant Expression (ICE). The value must be computable at compile-time. A const int (or other const-qualified integer-type object) can be used in an Integral Constant Expression only if it is itself initialized with an Integral Constant Expression.

A non-const object (like n1) cannot appear anywhere in an Integral Constant Expression.

Have you considered using std::vector<int>?

[Note--The cast is entirely unnecessary. Both of the following are both exactly the same:

const int N = n1;
const int N = const_cast<const int&>(n1);

--End Note]

Invalid conversion from int** to const int**

I was mistaken about the constness of the method being the reason for the error. As Ben points out, the const-ness of the method is irrelavent, since that applies only to the value of the exterior pointer [to pointers to ints], which can be copied to a mutable version trivially.

In order to protect the data (which is your preferred outcome) you should make both the ints and the pointers to ints constant:

int const * const * Class::Access() const
{
return pp_array;
}

Will work.

If you prefer to have the const in front you can also write the declaration like so:

const int * const * Class::Access() const;

but since the second const applies to the pointers, it must be placed to the right (like the const which applies to the method) of the asterisk.

Cannot Convert from const int* to int*

The error's quite clear - a pointer conversion can't remove a const qualifier; otherwise, you could violate the constness:

int* variable = value; // Not allowed - but if it were...
*variable = 42; // BOOM! changed a constant.

If you don't want to be able to change the value being pointed at, then keep it const

const int* variable = value;

If you do want to change it, then don't make it const in the first place:

void SomeFunction(int* value)

I know I can solve this problem by using const_cast

That's a bad idea - you'll get undefined behaviour if you abuse const_cast and try to modify a constant object. You should use const where you can, but not when you want to modify something.

error: cannot convert 'int* (*)[10]' to 'const int* (*)[10]'

There are two errors in your code.

  1. Your passing in a int* array[][], not a const int* array[][]. So change the function parameter to int* array[][10].
  2. You are subjected to array decay. To resolve this, you should pass in the array by reference. To this your function parameter should be like this: int* (&arrayPtr)[][10].

Your error is self explanatory, it says what's wrong. If you read it carefully, it says that your passing int* (*)[10] to a const int* (*)[10] type container.

invalid conversion from ‘const int*’ to ‘int*’ in template function

Shouldn't void printInfo in class A look like

void printInfo(const int* a)

Is this correct?

No, the problem ist that you declare var as A<int*> in B, so A's

void printInfo(const T& a); 

is really

void printInfo( int* const& a); 

and not

void printInfo( int const* & a); 

So, for the call in B to work you need to declare var as A<int const*>. See compiling version here.



Related Topics



Leave a reply



Submit