Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.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++;_C++_Oop - Fatal编程技术网

C++ 在C++;

C++ 在C++;,c++,oop,C++,Oop,我不知道如何在超类上没有默认构造函数的情况下进行对象初始化 #include <iostream> class A { protected: A(std::string title, int xpos, int ypos); }; class B : A { protected: //Is that correct? A* m_pA(std::string title, int xpos, int ypos); //Why not just

我不知道如何在超类上没有默认构造函数的情况下进行对象初始化

#include <iostream>

class A
{
protected:  
    A(std::string title, int xpos, int ypos);
};

class B : A
{
protected:
    //Is that correct?
    A* m_pA(std::string title, int xpos, int ypos); 
    //Why not just A* m_pA;? 
public:
    B(std::string title, int xpos, int ypos);
};

B::B(std::string title, int xpos, int ypos) : m_pA(title, xpos, ypos)
{
    //does nothing yet.     
}
#包括
甲级
{
受保护的:
A(std::字符串标题,intxpos,intypos);
};
B类:A
{
受保护的:
//对吗?
A*m_pA(std::string title,intxpos,intypos);
//为什么不只是一个妈妈;?
公众:
B(std::字符串标题,intxpos,intypos);
};
B::B(std::string title,intxpos,intypos):m_pA(title,xpos,ypos)
{
//什么也不做。
}
正如您所见,我正试图初始化B的构造函数中类型为
A
m_pA
对象,VC向我抛出:

“mu pA”不是类“B”的非静态数据成员或基类

您可以看到编译的示例和错误

我想知道为什么以及如何在没有默认构造函数的情况下初始化类的成员对象,以及为什么我错了


提前谢谢

在构造子类时,需要显式初始化基类

B(std::string title, int xpos, int ypos)
    : A(title, xpos, ypos)
{}

您可能还应该通过const引用传递这些字符串,否则您将生成一堆不必要的副本。

您的代码有一些错误。首先,要让继承的类访问受保护的变量/函数,继承的类必须是友好的。其次,私有变量m_pA是指针。不能像初始化a实例那样初始化指向a的指针。请查看以下代码:

class B : A
{
protected:
    A* p;
    A a;
public:
    B(std::string title, int xpos, int ypos)
         : A(title, xpos, ypos)          // init base
         , p(new A(title, xpos, ypos))   // init pointer
         , a(title, xpos, ypos)          // init member
    {}
};
#include <iostream>

class A
{
    friend class B;
protected:  
    A();
    A(std::string title, int xpos, int ypos);
};

A::A()
{
//Do something
}
A::A(std::string title, int xpos, int ypos)
{
//Do something
}

class B : A
{
protected:
    A* m_pA;    
public:
    B(std::string title, int xpos, int ypos);
};

B::B(std::string title, int xpos, int ypos)
{
    m_pA = new A(title, xpos, ypos);
}

int main() {

    return 0;
}
#包括
甲级
{
B级朋友;
受保护的:
A();
A(std::字符串标题,intxpos,intypos);
};
A::A()
{
//做点什么
}
A::A(std::string title,intxpos,intypos)
{
//做点什么
}
B类:A
{
受保护的:
A*m_pA;
公众:
B(std::字符串标题,intxpos,intypos);
};
B::B(std::字符串标题,intxpos,intypos)
{
m_pA=新A(标题、XPO、YPO);
}
int main(){
返回0;
}

您可以在此处验证:

您真的打算从继承,并拥有指向的成员指针吗?无论如何,受保护的声明是一个方法。您仍然需要初始化基本
A*m_pA(std::string title,intxpos,intypos)
是B类中的一个函数,返回指向a的指针。yea@sp2danny你可能是对的,我的头,只是太困惑了。D:不管怎样,如果我想要一个类型为a的对象,我如何初始化它?我只想打电话给B,A将被初始化,这是目标,询问可能不清楚。。。你说得对!而且,正如前面所说的,
mupa
不是一个非静态成员对象,因此尝试在ctor init列表中初始化它是一个错误。无论如何,您希望将参数传递给那里的
A
基类子对象。。。。(将列表中的
m_-pA
替换为
A
)彻底去除m_-pA。将构造函数更改为
B::B(std::string title,int xpos,int ypos):A(title,xpos,ypos){}
至少可以解决所有问题…@Aesthete类似于
const std::string&title
?@eRafaelNunes-yes,这就是我所说的引用。还要记住,默认情况下继承是私有的,所以如果这不是您真正想要的,那么您必须像
class B:public A那样继承。祝你好运特别是,构造函数在调用基类构造函数之前初始化数据成员。更广泛地说,OP的问题是,他认为他必须为基类提供一个数据成员,而基类在派生类中是隐式的。@NicholasM:不是。按照标准的构建顺序:虚拟基础、其他基础、成员,按照其定义的阅读顺序,而不是init列表的阅读顺序。它旨在展示他可能使用的所有东西的用例want@drescherjm字体请详细说明,怎么了?我收回我的评论。必须更清楚地阅读答案,尤其是在观看体育比赛时。你的答案为我提供了灵活的m_pA初始化,这正是我所需要的,我尝试了你所做的,但没有朋友类B,我不知道:
对于继承类访问受保护的变量/函数,继承的类必须是友好的
。很高兴我提供了代码帮助。您还可以尝试删除“friend class B”,并将B的类声明更改为:“class B:protected A”,始终使用多种方法剥猫皮。