1
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)
Kentaro Okuda
1,6252 gold badges14 silver badges17 bronze badges
asked Nov 18, 2019 at 19:45

2 Answers 2

1

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.

answered Nov 18, 2019 at 20:32
Sign up to request clarification or add additional context in comments.

Comments

0
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.

answered Nov 18, 2019 at 19:52

1 Comment

I've meant to say Shouldn't void printInfo in class A look like void printInfo(const int* a). Then why does it trying to convert pointer to const into pointer to non const when functions parameters in both classes are the same

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.