Reputation: 77
class A{
private:
T x;
public:
A():x(0){}
A(T x1):x(x1){}
void printInfo(const T& a){
cout<<"Succes1"<<endl;
}
};
class B{
private:
int x;
A<int*> var;
public:
B():x(0){}
B(int x1):x(x1){}
void printInfo(const int * a){
var.printInfo(a);
}
};
The probelms is with
void printInfo(const int * a){
var.printInfo(a);
}
It gives an error, saying invalid conversion from ‘const int*’ to ‘int*’
but works with int *a
or int *const a
Shouldn't void printInfo in class A look like
void printInfo(const int* a)
Is this correct?
cont int *p, //pointer to constant int
int* const p //constant pointer to int
if thats the case there should be error with
printInfo(int* const a)
not with
printInfo(const int * a)
Upvotes: 1
Views: 425
Reputation: 5138
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.
Upvotes: 1
Reputation: 238311
void printInfo(const int * a){
Shouldn't void printInfo in class B look like
void printInfo(const int* a)
Is this correct?
Both are equivalent. In many cases, whitespace is not syntactically relevant.
cont int *p, //pointer to constant int int* const p //constant pointer to int
These are correct.
if thats the case there should be error with
printInfo(int* const a)
not with
printInfo(const int * a)
The error should be when you attempt to implicitly convert a pointer to const into pointer to non-const.
Upvotes: 0