"Bracket initialization." (C ++)

I am learning C ++ at the moment, C ++ Primer plus. But I just wanted to check the cplusplus website and skip the file processing a bit.

I pretty much know the basics of file processing coming from java, php, visual basic. But I came across a rather strange line.

ostream os(&fb);

fb represents the file. I just don't get the syntax for this, but I can understand that it is the same as:

ostream os = &fb;

But I never read about this way of initializing variables.

So I'm interested. Am I just pointless and lose sight of a real useful feature all the time? Is this initialization method only old? Is it something else?

Thanks in advance.

+5
source share
5 answers
+5

. ( ()) . ( =) -. , .

, (LHS) (RHS) ( / ), . , = ().

( LHS - ), -.

  • - : RHS LHS ( : , , ). LHS LHS.

  • : LHS , .

, - ( , ). LHS -, , - .

, explicit, , , .

+6

, , :

#include <iostream>

using namespace std;

class test
{
    public:
        // default constructor.
        test()
        {
            cout<<"Default Ctor called"<<endl;
        }

        // copy constructor.
        test(const test& other)
        {
            cout<<"Copy Ctor called"<<endl;
        }

        // overloaded assignment operator function.
        test& operator=(const test& other)
        {
            cout<<"Overload operator function called"<<endl;
            return *this;
        }
};

int main(void) 
{
    test obj1;  // default constructor called.

    test obj2 = obj1; // copy constructor called.

    test obj3(obj2); // again copy constructor called.

    obj1 = obj2; // overloaded assignment operator function.

    return 0;
}

:

Default Ctor called
Copy Ctor called
Copy Ctor called
Overload operator function called

, ostream .

+5

, , . , fstream :

std::fstream file("filename", ios_base::out);

++ 0x , - .

+1

, & var var , .

-------- -----------------

. , . , .

"The semantics of passing an argument are defined as initialization, so when increment was called, the argument aa became another name for x." This is why I referred to & x as an alias of x.

void increment(int& aa) { aa++; }

void f () {int x = 1; increment (x); }

0
source

All Articles