Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 65
  • Last Modified:

20. Can a constant method be called for object-variables? What about usual method - for a constant object?

Can a constant method be called for object-variables? What about usual method - for a constant object?
0
Nusrat Nuriyev
Asked:
Nusrat Nuriyev
  • 9
  • 8
  • 2
3 Solutions
 
jkrCommented:
As a rule of thumb, if you have a constant object, you are only allowed to call 'const' mthods, anything else would defy the purpose of a const object. You can also read all public data, but not write it.
0
 
phoffricCommented:
I didn't understand your question.
Does http://www.experts-exchange.com/Programming/Languages/CPP/Q_28603149.html address them?
0
 
Nusrat NuriyevAuthor Commented:
jkr, could you please show some examples for both cases?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jkrCommented:
Sure, e.g.

class MyClass {
public:
  MyClass() : A(42), B(0) {}
  int A;
  void SetB(int val) { B = val;}
  const int GetB() const { return B;}
protected:
  int B;
};

int main () {

  const MyClass mc1;
  MyClass mc2;

  int test1 = mc1.GetB(); // OK

  int test2 = mc1.A; // OK

  int test3 = mc2.A; // OK

  mc2.A = 0; // OK

  mc2.SetB(16); // OK

  mc1.SetB(23); // compiler error - calling non-const method on a const object

  return 0;
}

Open in new window

0
 
jkrCommented:
Ooops, forgot one like:

  mc1.A = 42; // compiler error - can't assign a value to a const object

Open in new window

0
 
Nusrat NuriyevAuthor Commented:
const int GetB() const { return B;}

What does mean both of this const's?

1.  as I know the second const is responsible for constness of whole object. the state of object? right?
But what about first const, at return value?
0
 
jkrCommented:
Yes, it means that it returns a const value. Let me illustrate it like that:

const /* indicates a const return value */ int GetB() const /* means that this method doesn not alter the object it belongs to */ { return B;}

Open in new window


Declaring a const method to have a non-const return value usually does not make a lot of sense, only e.g. for numeric values that you use to initialize something else.
0
 
Nusrat NuriyevAuthor Commented:
I got this part. But?

const int x = 5;

But does it make sense to return something to the x?
like this?
x = mc1.GetB();
does it make sense?
0
 
Nusrat NuriyevAuthor Commented:
I have changed to this
int GetB() const { return B;}

Open in new window


but still do not get what is the affect of const for this code
int test1 = mc1.GetB(); // OK

Open in new window

or maybe it affects to other code?
0
 
jkrCommented:
It affects the way the compiler can optimize the code. And I have to admit that it does not seem to make a lot of sense for POD types (POD as 'Plain Old Data'), that concept really gets interesting and powerful when it comes to return more complex data that is encapsuled in the class. Think of e.g. a lookup table.
0
 
Nusrat NuriyevAuthor Commented:
For example , this seems to be a look up table, which is just an array , but with  integer constants
static const unsigned short MortonTable256[256] = 
{
  0x0000, 0x0001, 0x0004, 0x0005, 0x0010, 0x0011, 0x0014, 0x0015, 
  0x0040, 0x0041, 0x0044, 0x0045, 0x0050, 0x0051, 0x0054, 0x0055, 
  0x0100, 0x0101, 0x0104, 0x0105, 0x0110, 0x0111, 0x0114, 0x0115, 
  ... 32 lines in total ...
  0x5540, 0x5541, 0x5544, 0x5545, 0x5550, 0x5551, 0x5554, 0x5555
};

Open in new window


Do I understand look-up table right?
Why it's powerfull to use const in lookup? Example, please.
0
 
jkrCommented:
Well, 'powerful' might be the wrong word here. Its cleaner. You would not want any other code to be able to modify the table, would you?
0
 
Nusrat NuriyevAuthor Commented:
Okay :)

that concept really gets interesting and powerful when it comes to return more complex data that is encapsuled in the class.
When you say this, did you mean the const keyword in general rather than absense/presence of const keyword in this code, right?

int GetB() const { return B;}

Open in new window

0
 
jkrCommented:
Interestingly enough in both ways ;o)

Let's try that one:

class CombustionEngineLinkup{
public:

  const unsigned int GetRPM() const { return RPM;}

private:

  unsigned int RPM;
};

class SpeedoMeter() {
public:
  const unsigned int GetCurrentRPM() const {return eng.GetRPM();

protected:
  CombustionEngineLinkup eng;
}

Open in new window

0
 
Nusrat NuriyevAuthor Commented:
stackoverflow.com/questions/8716330/purpose-of-returning-by-const-value
I have written an example, as I understand constness have something with lvalue

class Dollars
{
    private:
        int m_nDollars;
    public:
        Dollars(int nDollars) { m_nDollars = nDollars; }
        friend const Dollars operator+ (const Dollars &d1, const Dollars &d2);
        int GetDollars() { return m_nDollars; }
};

const Dollars operator+ (const Dollars &d1, const Dollars &d2)
{
    return Dollars(d1.m_nDollars + d2.m_nDollars);
}

int main()
{
    Dollars d1(12);
    Dollars d2(8);

    std::cout << "I'm gonna pop some tags" << std::endl
              << "Only got " << (d1 + d2).GetDollars() << " dollars in my pocket" << std::endl
              << "I, I, I'm hunting, lookin' for a come-up" << std::endl
              << "This is fucking awesome." << std::endl;
    return 0;
}

Open in new window



This example will work without const  at return of operator+.
Also I have understand that I could write like this:
operator+(d1,d2).GetDollars()

Open in new window

Now, I understand your words about that  overloading operator does not belong to the class, do you remember, we had such example? :)
0
 
jkrCommented:
Yes, I do remember - and sometimes it's hard to come up with an example for stuff that you work with and have taken for granted over decades ;o)

Just as a side note: As for EE rules, *I* would not be allowed to linkt to SO, since it is considered a competing site...
0
 
Nusrat NuriyevAuthor Commented:
:)

>>> Just as a side note: As for EE rules, *I* would not be allowed to linkt to SO, since it is considered a competing site...
Ah, just I forgot that :)
0
 
jkrCommented:
Well, you as someone asking a question are allowed if I am not mistaken...
0
 
phoffricCommented:
>> "overloading operator does not belong to the class"

As I just happened to see this remark and not knowing its context, I just wanted to make sure we are all on the same page. Namely that overloading operator can belong to the class, but doesn't have to. Ok?

The following set of operators is commonly overloaded for user-defined classes:
    •= (assignment operator)
    •+ - * (binary arithmetic operators)
    •+= -= *= (compound assignment operators)
    •== != (comparison operators)
http://courses.cms.caltech.edu/cs11/material/cpp/donnie/cpp-ops.html
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 8
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now