Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I use an as-of-yet undeclared member variable in a member function?

Tags:

c++

class

struct

For example:

struct X{
X():a{10} {}
void foo() { a = 10; }

private:
int a;
};

Why does this compile when variable a hasn't been declared yet?

like image 977
ozma Avatar asked Feb 24 '17 09:02

ozma


People also ask

How do you solve undeclared identifier?

To solve it simply declare i outside the loop so that the whole program inside the main function can use it. LIBRARY NOT INCLUDED: If we try to use a data type such as vector without including its library we will get this error. To fix this, make sure that you are using an identifier only after including its library.

What does use of undeclared identifier mean in C++?

The identifier is undeclared: In any programming language, all variables have to be declared before they are used. If you try to use the name of a such that hasn't been declared yet, an “undeclared identifier” compile-error will occur. Example: #include <stdio.h> int main()

What is the difference between member variable and member function?

A class groups a set of values and a set of operations. The values and the operations of a class are called its members. Member variables implement the values and member functions implement the operations.

How do you access member variables?

To get the value of a class member variable, pass the class to one of the appropriate class member variable access functions. To get the value of an instance member variable, pass the object to the appropriate instance member variable access function. For example, in FieldAccess.


1 Answers

The compiler basically does two passes over the class or structure definition. One for the structure/class to parse and handle declarations of members, then one pass for the inline functions.

like image 59
Some programmer dude Avatar answered Nov 03 '22 00:11

Some programmer dude