why can't we call a function on a const object?

saeed abbasi

the problem is in main where I want to call set() function on display() function which return a reference to const.

#include "stdafx.h"
#include "iostream"
#include "string"

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ostream;

class screen
{
public:
typedef string::size_type pos;
screen() = default;
screen(pos ht, pos wd, char c) :height(ht), width(ht),contents(ht*wd, c){}
screen(pos ht, pos wd) :height(ht), width(wd), contents(ht*wd, ' '){}
char get()const{ return contents[cursor]; }
inline char get(pos r, pos c)const;
screen &move(pos r, pos c);
screen &set(char);
screen &set(pos r, pos c,char);
const screen& display(ostream &os)const;
    pos cursor;
    pos height, width;
    string contents;
};
const screen& screen::display(ostream &os)const
{
     os<< contents;
     return *this;
}

char screen::get(pos r, pos c)const
{
    pos row = r*width;
    return contents[row + c];
}
screen& screen::move(pos r, pos c)
{
    pos row = r*width;
    cursor = row + c;
    return *this;
}
screen& screen::set(char c)
{
    contents[cursor] = c;
    return *this;
}
screen& screen::set(pos r, pos c, char ch)
{
    pos row = r*width;
    contents[row + c] = ch;
    return *this;
}

int main()
{
    screen myscreen(50, 50,'0');
    myscreen.display(cout).set('#');//the problem is here!


    return 0;
}

I just want to know why set('#') cannot be called on display(cout) which returns a reference to const. It is possible to set('#').display(cout) but not display(cout).set('#') How constness of an abject can have such a effect on a call?

Steephen

If you call a member function, there is an implicit this pointer passing as a parameter to the function. In your case, the this pointer is const, and it can't be modified inside your set function. So it failed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why can't we add properties to a function object inside function body like we do for object literals in javascript?

From Java

Why can we use `std::move` on a `const` object?

From Dev

Why can't we call the method of the class using the class's object outside a method?

From Java

Why can't we call Thread#sleep() directly inside a Lambda function?

From Dev

Why can't we call Thread#sleep() directly inside a Lambda function?

From Dev

c++: why can't we convert char ** to const char **

From Dev

Why can't I call a non-const member function on an element in an std::set?

From Dev

Why we can call `__mod__` function with string in Python?

From Dev

why can't we do call and apply on setTimeout?

From Dev

Why can't we call protected destructors from a derived class?

From Dev

Why can't we call uniform() before applyBindings()?

From Dev

Why can't we call uniform() before applyBindings()?

From Dev

Why can't we define function inside the main function?

From Dev

Why can't I call my function?

From Dev

Why can't I call my function?

From Dev

Why can't I call this function in React?

From Dev

Why can't I use this function with inside a function with const?

From Dev

Why can't a 'Base Class object' call it's own virtual function? C++

From Dev

Why can't we access an object using its address in Python?

From Dev

Why we use const and & in this function declaration?

From Dev

Why can't we define main function in static inner classes?

From Dev

Why can't we use/set function keys as password keys?

From Dev

Why can't we use setAtrribute function in jquery?

From Dev

Can we pass an object as an argument in object call?

From Dev

Can we pass an object as an argument in object call?

From Dev

Why can't I instantiate an object in a function

From Dev

Why can we non-const reference to a temporary object and prolong its lifetime?

From Dev

Why can't I mark this member function as const?

From Dev

Why pointer to non-const member function can't point const member function and opposite?

Related Related

  1. 1

    Why can't we add properties to a function object inside function body like we do for object literals in javascript?

  2. 2

    Why can we use `std::move` on a `const` object?

  3. 3

    Why can't we call the method of the class using the class's object outside a method?

  4. 4

    Why can't we call Thread#sleep() directly inside a Lambda function?

  5. 5

    Why can't we call Thread#sleep() directly inside a Lambda function?

  6. 6

    c++: why can't we convert char ** to const char **

  7. 7

    Why can't I call a non-const member function on an element in an std::set?

  8. 8

    Why we can call `__mod__` function with string in Python?

  9. 9

    why can't we do call and apply on setTimeout?

  10. 10

    Why can't we call protected destructors from a derived class?

  11. 11

    Why can't we call uniform() before applyBindings()?

  12. 12

    Why can't we call uniform() before applyBindings()?

  13. 13

    Why can't we define function inside the main function?

  14. 14

    Why can't I call my function?

  15. 15

    Why can't I call my function?

  16. 16

    Why can't I call this function in React?

  17. 17

    Why can't I use this function with inside a function with const?

  18. 18

    Why can't a 'Base Class object' call it's own virtual function? C++

  19. 19

    Why can't we access an object using its address in Python?

  20. 20

    Why we use const and & in this function declaration?

  21. 21

    Why can't we define main function in static inner classes?

  22. 22

    Why can't we use/set function keys as password keys?

  23. 23

    Why can't we use setAtrribute function in jquery?

  24. 24

    Can we pass an object as an argument in object call?

  25. 25

    Can we pass an object as an argument in object call?

  26. 26

    Why can't I instantiate an object in a function

  27. 27

    Why can we non-const reference to a temporary object and prolong its lifetime?

  28. 28

    Why can't I mark this member function as const?

  29. 29

    Why pointer to non-const member function can't point const member function and opposite?

HotTag

Archive