What is the reason not to allow the default value for a variable as a non-static method or class member in C ++?

I wanted to know why the default value for a variable for a class method cannot be a non-stationary method or a member of the same class.

Is there a reason for this? Could the compiler give the method a position in the non-static default class?

I tried to quickly find the answer, but I could not find a good answer.

EDIT: here is an example.

It is legal:

 class ClassTemp
{
  static int s_member;

  int MagicOperation(int defaultValue = s_member)
  {
    return defaultValue;
  }
};

But this is not so:

class ClassTemp
{
  int m_member;

  int MagicOperation(int defaultValue = m_member)
  {
    return defaultValue;
  }
};
+3
source share
4 answers

( "", "" ), . , , .

, , . " C++" , - . , , , .

+5

, 'this' . ,

+1

, , , "" - , this .

-, , :

push provided arguments...
push (this->member)
push this

, , .

0

If I understand your question, it is because the compiler knows if there is a non-static variable that you initialize this var method - therefore the requirement is that it be static, therefore it is guaranteed to exist when the var method is initialized. This is not a search issue.

0
source

Source: https://habr.com/ru/post/1724763/


All Articles