How to initialize a class member correctly?

let's say we have this:

class Foo {

    public:
    Foo(const Bar& b) : m_bar(b) {}

    private:
    Bar m_bar;
};

Now, the efficiency of the C ++ FAQ LITE says the following:

Consider the following constructor that initializes the member object "x" using the initialization list: Fred :: Fred (): x (any) {}. The most common benefit of this is improved performance. For example, if an expression of the type is of the same type as the member variable "x", the result of any expression is built directly inside "x" - the compiler does not create a separate copy of the object.

If the constructor is better to have a parameter as a value instead of a reference?

Since I use the constructor initialization list, will there be a difference in performance?

, , ? , Foo (Bar())

- - .

+3
6

, :

?

, , const. : , . , , .

const , . , , .

, ?

, , , , :

class Foo
{
    Bar bar;
    Foo(const &Bar bar_)
    /* bar(Bar()) is implicitly called here,
    before the start of the function body */
    {
        /* Note that we cannot do bar(bar_) now
        as bar has already been constructed.
        So we might do this instead: */
        bar = bar_; // the assignment operator function is called here
    }
};

, , Bar , Bar, Bar , () . .

, , , , . .

+3

, , " , - x_", , b Bar m_bar Bar.

, - .

Josef SoapBox, const. . , , , , , ++ 0x .

+4

, , , . . . Foo(Bar()), .

+2

const, . , const, , .

, , new Foo (Bar()) ++ ( r - , ). , .

+1

++ . .

!

, !

- , ++ 1997 . , , , , . (, , !)

++ () .

2

? -, ?

GCC Microsoft cl.exe .

, Microsofted:

#include <stdio.h>
#include <tchar.h>

class A {
int m;
public:
A(int x=0) : m(x)
{
    _putts(_T("Constructor A"));
}
A(const A &x) : m(x.m)
{
    _putts(_T("Copy A"));
}
int get() const { return m; }
};

class B {
A m;
public:
B(int y=5) : m(y)
{
    _putts(_T("Constructor B"));
}
B(const B &x) : m(x.m)
{
    _putts(_T("Copy B"));
}
B(A x) : m(x)
{
    _putts(_T("Construct B from A value"));
}
int get() const { return m.get(); }
};

class C {
A m;
public:
C(int y=5) : m(y)
{
    _putts(_T("Constructor C"));
}
C(const C &x) : m(x.m)
{
    _putts(_T("Copy C"));
}
C(const A &x) : m(x)
{
    _putts(_T("Construct C from A reference"));
}
int get() const { return m.get(); }
};

int _tmain(int argc, _TCHAR* argv[])
{
    _putts(_T("Hello World"));

int i = 27;
if( argc > 1 )
    i = _tstoi(argv[0]);

A aval(i);
_tprintf(_T("A value is: %d\n"), aval.get());

B bval( aval );
_tprintf(_T("Value is: %d\n"), bval.get());

B bval2( (A(i)) );
_tprintf(_T("Value is: %d\n"), bval2.get());

C cval( aval );
_tprintf(_T("Value is: %d\n"), cval.get());

C cval2( (A(i)) );
_tprintf(_T("Value is: %d\n"), cval2.get());

_putts(_T("Goodbye World"));
return 0;
}

-Microsoft GCC:

#include <cstdio>
#include <cstdlib>
using namespace std;

#define _TCHAR char
#define _T(x) x
#define _tmain main
#define _putts puts
#define _tprintf printf
#define _tstoi atoi

, :

Hello World
Constructor A
A value is: 27
Copy A
Copy A
Construct B from A value
Value is: 27
Constructor A
Copy A
Construct B from A value
Value is: 27
Copy A
Construct C from A reference
Value is: 27
Constructor A
Copy A
Construct C from A reference
Value is: 27
Goodbye World
+1
  • , , .
  • , Bar(), ( , ).

* Note: the way you do this, you cannot do new Foo(Bar()), because you cannot pass a reference to Bar (), you need to have an instance of the object in a variable for this.

0
source

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


All Articles