Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 初始化引用成员:语法不同的原因_C++_Constructor_Reference_Initialization - Fatal编程技术网

C++ 初始化引用成员:语法不同的原因

C++ 初始化引用成员:语法不同的原因,c++,constructor,reference,initialization,C++,Constructor,Reference,Initialization,我有两个类,其中一个类上有另一个对象作为引用成员: class myClass{ public: myClass() { }; }; class mySecondClass { public: mySecondClass(myClass& reference) { myClassReference = reference; //doesn't get accepted by the compiler }; myClass&

我有两个类,其中一个类上有另一个对象作为引用成员:

class myClass{
public:
    myClass() { };
};

class mySecondClass {
public:
    mySecondClass(myClass& reference)
    {
        myClassReference = reference; //doesn't get accepted by the compiler
    };

    myClass& myObjectReference;
};
多亏了你,我才知道 ,mySecondClassmyClass&reference:myObjectReferencereference{};他做这项工作

但是为什么我不能使用myObjectReference=reference

构造函数中的

mySecondClass(myClass& reference)
{
    myClassReference = reference; //doesn't get accepted by the compiler
};
mySecondClass(myClass& reference) : myObjectReference(reference){}
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
有一个使用过的引用myClassReference,它在创建时应该初始化。但是,变量未初始化。这里使用了复制分配运算符

在这个构造函数中

mySecondClass(myClass& reference)
{
    myClassReference = reference; //doesn't get accepted by the compiler
};
mySecondClass(myClass& reference) : myObjectReference(reference){}
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
在mem初始值设定项列表中创建并初始化引用

为了更清楚地考虑下面的演示程序,

#include <iostream>

struct Int
{
    Int() { std::cout << "Int()\n"; }
    Int( int x ) : x( x ) { std::cout << "Int( " << x << " )\n"; }
    Int & operator =( int x )
    {
        std::cout << "Int & operator =( " << x << " )\n";
        this->x = x;

        return *this;
    }

    int x;
};

struct A
{
    A( int x )
    {
        this->value = x;
    }
    Int value;
};

struct B
{
    B( int x ) : value( x ) {}
    Int value;
};

int main() 
{
    A a( 10 );

    std::cout << '\n';

    B b( 10 );

    return 0;
}
也就是说,当构造函数A的主体获取控件时,数据成员已使用Int类的默认构造函数创建。在构造函数的主体中使用赋值运算符

与类A的构造函数相反,类B的构造函数使用带有参数的类Int的构造函数在mem-inkInitializer列表中创建了数据成员

现在假设您正在类a中使用一个引用,那么它是默认初始化的,即它实际上没有被它所引用的任何有效对象初始化。因此,在构造函数的主体中,您试图将一个值赋给一个无效的引用,而该引用没有引用任何地方。因此编译器发出一个错误

构造函数中的

mySecondClass(myClass& reference)
{
    myClassReference = reference; //doesn't get accepted by the compiler
};
mySecondClass(myClass& reference) : myObjectReference(reference){}
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
有一个使用过的引用myClassReference,它在创建时应该初始化。但是,变量未初始化。这里使用了复制分配运算符

在这个构造函数中

mySecondClass(myClass& reference)
{
    myClassReference = reference; //doesn't get accepted by the compiler
};
mySecondClass(myClass& reference) : myObjectReference(reference){}
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
在mem初始值设定项列表中创建并初始化引用

为了更清楚地考虑下面的演示程序,

#include <iostream>

struct Int
{
    Int() { std::cout << "Int()\n"; }
    Int( int x ) : x( x ) { std::cout << "Int( " << x << " )\n"; }
    Int & operator =( int x )
    {
        std::cout << "Int & operator =( " << x << " )\n";
        this->x = x;

        return *this;
    }

    int x;
};

struct A
{
    A( int x )
    {
        this->value = x;
    }
    Int value;
};

struct B
{
    B( int x ) : value( x ) {}
    Int value;
};

int main() 
{
    A a( 10 );

    std::cout << '\n';

    B b( 10 );

    return 0;
}
也就是说,当构造函数A的主体获取控件时,数据成员已使用Int类的默认构造函数创建。在构造函数的主体中使用赋值运算符

与类A的构造函数相反,类B的构造函数使用带有参数的类Int的构造函数在mem-inkInitializer列表中创建了数据成员

现在假设您正在类a中使用一个引用,那么它是默认初始化的,即它实际上没有被它所引用的任何有效对象初始化。因此,在构造函数的主体中,您试图将一个值赋给一个无效的引用,而该引用没有引用任何地方。因此编译器发出一个错误

这是因为{myClassReference=reference;}被认为是 分配给本应已初始化的内容

成员初始化列表:member1{value1},member2{value2}。。。 旨在提供初始值,但不应 之前有效

对于引用的特定情况,情况与

int i=4;
int &r=i; // the reference is initialised (alias for i)
r=5;      // the reference itself is not changed but
          //   the referenced object (i) is assigned
这是因为{myClassReference=reference;}被认为是 分配给本应已初始化的内容

成员初始化列表:member1{value1},member2{value2}。。。 旨在提供初始值,但不应 之前有效

对于引用的特定情况,情况与

int i=4;
int &r=i; // the reference is initialised (alias for i)
r=5;      // the reference itself is not changed but
          //   the referenced object (i) is assigned

我想补充一下,我对C++是很新的,并且我不总是能够理解C++的引用站点,这可能会让这个问题看起来很有意思,我想我对C++是很陌生的,而且我不总是能够理解C++的引用站点,这可能会使这个问题变得更难。编译器基本上认为我想改变变量。引用指向?@SebastianDonnerWolfBach是的,对了,在你的命题中,引用没有显式初始化以指向某个东西,所以编译器基本上认为我想更改引用指向的变量?@SebastianDonnerWolfBach是的,对了,在你的命题中,引用没有显式初始化以指向某个对象