Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.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++ 指向成员对象和声明顺序的成员指针 #包括 类FoopParent { 公众: FoopParent(int*新的\u p\u条) { p_栏=新的p_栏; } 公众: int*p_巴; }; 类FooChild:公共FooParent { 公众: int-bar; 公众: FooChild(int new_x) :foopparent(&bar) ,酒吧(新)\\关注点 {} }; int main() { 福子福(8),; std::cout_C++_Pointers_Inheritance_Member - Fatal编程技术网

C++ 指向成员对象和声明顺序的成员指针 #包括 类FoopParent { 公众: FoopParent(int*新的\u p\u条) { p_栏=新的p_栏; } 公众: int*p_巴; }; 类FooChild:公共FooParent { 公众: int-bar; 公众: FooChild(int new_x) :foopparent(&bar) ,酒吧(新)\\关注点 {} }; int main() { 福子福(8),; std::cout

C++ 指向成员对象和声明顺序的成员指针 #包括 类FoopParent { 公众: FoopParent(int*新的\u p\u条) { p_栏=新的p_栏; } 公众: int*p_巴; }; 类FooChild:公共FooParent { 公众: int-bar; 公众: FooChild(int new_x) :foopparent(&bar) ,酒吧(新)\\关注点 {} }; int main() { 福子福(8),; std::cout,c++,pointers,inheritance,member,C++,Pointers,Inheritance,Member,如果将指针传递给成员,则在尝试取消引用之前,不会有未定义的行为。如果希望从成员中调用构造函数,请查看基 #include <iostream> class FooParent { public: FooParent(int* new_p_bar) { p_bar = new_p_bar; } public: int* p_bar; }; class FooChild : pub

如果将指针传递给成员,则在尝试取消引用之前,不会有未定义的行为。如果希望从成员中调用构造函数,请查看

#include <iostream>

class FooParent
{
    public:
        FooParent(int* new_p_bar)
        {
            p_bar = new_p_bar;
        }
    public:
        int* p_bar;
};

class FooChild : public FooParent
{
    public:
        int bar;
    public:
        FooChild(int new_x)
        :FooParent(&bar)
        ,bar(new_x) \\ point of concern
            {} 
};

int main()
{ 
    FooChild foo(8);
    std::cout << foo.bar << std::endl;

}


这将不起作用。即使
Bar
将有一个副本c-tor或/和赋值运算符

,构造函数也将被调用。初始值设定项列表中的顺序与构造顺序不同。成员变量按照类中声明的顺序初始化。如果
FooParent
的c-tor只存储地址of
Bar
,并且在
FooParent(&Bar)
Bar(new_x)
之间没有其他初始化,那么这里似乎没有问题,换句话说,它只对指针(或引用)起作用。对对象的copy@borisbn因此,如果我想使用它,我必须实现复制构造函数(和复制赋值运算符)来考虑这一点。是这样吗?不是。复制c-tor和赋值运算符都不会有帮助。它只对指针(或引用)起作用我说的是复制c-tor和复制分配给
FooChild
,这样当它复制
FooChild
时,它会将
p\u bar
分配给新复制的
bar
。这够好吗?哇……对不起。我想到了复制
bar
的c-tor……如果“它将
p\u条
分配给新复制的
class FooParent {
    public:
        FooParent(int* new_p_bar)
        {
            p_bar = new_p_bar;
            *p_bar = 99; // this has no sense
        }
        void set99() {
            *p_bar = 99; // this - has
        }
    public:
        int* p_bar;
};

class FooChild : public FooParent
{
    public:
        int bar;
    public:
        FooChild(int new_x)
        :FooParent(&bar)
        ,bar(new_x) // point of concern
            {} 
};

int main()
{ 
    FooChild foo( 42 );
    std::cout << foo.bar << std::endl;
    foo.set99();
    std::cout << foo.bar << std::endl;
}
class FooParent
{
    public:
        FooParent(Bar new_p_bar)
        {
            p_bar = new_p_bar;
        }
        void set99() {
            p_bar = 99; // this - has
        }
    public:
        Bar p_bar;
};

class FooChild : public FooParent
{
    public:
        Bar bar;
    public:
        FooChild(Bar new_x)
        :FooParent(bar)
        ,bar(new_x) // point of concern
            {} 
};

int main()
{ 
    FooChild foo( 42 );
    std::cout << foo.bar << std::endl;
    foo.set99();
    std::cout << foo.bar << std::endl;
}