Constructor argument and member with the same name

I am wondering if the following code is valid. Static analysis gives an error for this constructor.

Summary: The member variable "A" is initialized by itself.

Summary: The member variable "B" is initialized by itself.

Summary: The member variable 'C' is initialized by itself.

class Foo
{
public:
    Foo(int A, int B, int C);
private:
    int A;
    int B;
    int C;
}

Foo::Foo(int A, int B, int C) : 
A(A),
B(B),
C(C)
{}

I know this is not a good practice and probably needs to be changed, however I would like to know if the static analysis warning is false and the member variables will be correctly initialized.

+4
source share
3 answers

They will be correctly initialized as indicated in the initialization list.

, :

Georgioss-MacBook-Pro:~ gsamaras$ g++ -Wall main.cpp
main.cpp:6:9: warning: private field 'A' is not used [-Wunused-private-field]
    int A;
        ^
main.cpp:7:9: warning: private field 'B' is not used [-Wunused-private-field]
    int B;
        ^
main.cpp:8:9: warning: private field 'C' is not used [-Wunused-private-field]
    int C;
        ^
3 warnings generated.

, , .


: - , -, ++?


, .

. , (, , / ). , .

+2

, : ; , , .

, , .

+2

, . A(A) 1- A , A; A ( ) , A ( ).

,

$15.6.2/2 [class.base.init]:

mem-initializer-id , , , .

$15.6.2/15 [class.base.init]:

- --init-mem-initializer , mem-initializer. [:

class X {
  int a;
  int b;
  int i;
  int j;
public:
  const int& r;
  X(int i): r(a), b(i), i(i), j(this->i) { }
};

X​::​r X​::​a, X​::​ b i, X​::​i i X​::​j X​::​i; , X . - ] [: mem- , this mem- . - ]

+2

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


All Articles