Converting pointer to const pointer?

I have a function which returns a pointer and this works fine. I now have another function that I want to return a const pointer. How can I convert my pointer to a const pointer?

I have tried
 
  Object* constObjectPointer = workingPointer;


however this doesnt work, I need the const pointer output to return from this function.

 
  const Object* linkedObjects::front() const{
1
2
3
4
5
6
7
8
9
Object* linkedObjects::front() {
  // code
  return ptr;
}

const Object* linkedObjects::front() const {
  // code
  return ptr;
}

It does not matter whether 'ptr' is to const or non-const object for the latter function. The function returns a copy of the value of ptr (an address) as a pointer to constant type Object object.
Last edited on
Like I said before. You are not referring to a const pinter but a const object.

You can have both, like so:

const Object* linkedObjects::front() const{

and so:

Object* linkedObjects::front() {


Apart form that it is hard to tell what actually is correct since you don't provide enough code to judge.
so i have a function that returns an Object* it returns as such,

 
return objectOne.getNode()->getObject();


however when i try using the same return in this function

 
const Object* linkedObjects::front() const{


i get this error and im unsure how to fix it

error: member function 'getNode' not viable: 'this' argument has type 'const ObjectLinkedList', but function is not marked const
i get this error and im unsure how to fix it
You need a function getNode() const since objectOne is in front() const const.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Foo {
 
  T
  funbar() const // if you have a const member function
  {
    this->sob(); // that calls other member
  }

  // then the called member
  U
  sob() const // has to be const too
  {
  }

};

Topic archived. No new replies allowed.