In CPP Warum ist die Initialisierung in einer Klasse gültig, aber nicht zuzuordnenC++

Programme in C++. Entwicklerforum
Anonymous
 In CPP Warum ist die Initialisierung in einer Klasse gültig, aber nicht zuzuordnen

Post by Anonymous »

Nehmen Sie die folgenden Klassen zum Beispiel: < /p>
class Test
{
public:
int a=1;// no errror
};
< /code>
class Test
{
public:
int a;
a=1; // error
};
< /code>
Here the second case gives error. After a few searches the reason i found is that because class only allows declaration and no assignment as class is just supposed to be a blueprint with no run time scope and assignment would require it to have run time scope as it requires a value being placed in a certain memory location, which is done at run time. But this explanation further confused me, now i am left with two more question:
  • Is initialization also a declaration?
  • How is initialization different from assignment, in the sense that assignment also requires putting certain value in the memory location assigned to a variable and initialization also does the same, then how are they different? I know that that they are different syntactically i.e only statements of the form:
int a=1;
< /code>
are considered assignment and statements of the form:
int a;
a=1;
< /code>
are considered assignment, but aren't they the same thing fundamentally i.e putting certain value in memory assigned for a variable? Then why is one valid and the other not valid?
Any help is appreciated. Thanks in advance!!

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post