如何访问基类的公共数据成员?

时间:2014-01-12 15:13:26

标签: c++

如何在派生类中访问Base类数据成员? 我想使用已在docElem中初始化customizeCSMWindow()构造函数的subMenuLists::changeWidget()数据成员。

class myWidget
{
public :
    QDomElement docElem; 
    QDomDocument *menuOrderXMLFile;
};

class subMenuLists : public QListWidget , public myWidget
{
        Q_OBJECT
 public slots:
        void changeWidget( int index);
};

class customizeCSMwindow : public QDialog , public myWidget
{
    Q_OBJECT
public :
    subMenuLists *menuList;
    customizeCSMwindow(QString);       

}

customizeCSMwindow::customizeCSMwindow(QString fileName)
{
menuOrderXMLFile = new QDomDocument();
file = new QFile(fileName);
QString errorStr;
int errorLine;
int errorColumn;
if(!menuOrderXMLFile->setContent(file, false , &errorStr, &errorLine,
                             &errorColumn))
   std::cout<<"not found \n";
 else
       docElem = menuOrderXMLFile->documentElement();
}

void subMenuLists::changeWidget(int index)
{
    clear();
// How to access that docElem here??
}

如何访问docElem函数中的void subMenuLists::changeWidget()

编辑

我想解释一下我的问题,看看你是否可以帮助我。我想要的是,在docElem函数中获取cusomizeCSMWidnow construtor中分配的subMenuList::changeWidget()的值。截至目前,当我在docElem函数内访问changeWidget时,它会给出null / uninitialized值。

3 个答案:

答案 0 :(得分:1)

派生类可以访问父级的公共成员和受保护成员。 您只需使用docElem,就好像它是subMenuLists的成员一样。

答案 1 :(得分:1)

customizeCSMwindow类似于subMenuLists类型元素的容器。容器和元素都有成员docElem。您尝试访问容器subMenuLists的{​​{1}}成员docElem

这不能直接起作用。或者,给容器的元素指定容器的指针,或者在调用customizeCSMwindow时给指向容器的指针。

解释元素中容器指针的示例:

subMenuLists::changeWidget

在评论中你问:“还有一件事,这个设计看起来不错吗?或者你能建议任何更好的方法来获得相同的变量,即docElem可以在两个不同的类中访问吗?”

这很大程度上取决于目标。如果#include <string> #include <iostream> class myWidget { public : std::string docElem; }; class customizeCSMwindow; class subMenuLists : public myWidget { customizeCSMwindow* m_pContainer; // Pointer to the container public: subMenuLists(customizeCSMwindow* pContainer) : m_pContainer(pContainer) {} void changeWidget(int index); }; class customizeCSMwindow : public myWidget { public: subMenuLists *menuList; // This pointer makes customizeCSMwindow to a container. customizeCSMwindow(); void setMenuList(subMenuLists* ml) { menuList = ml; } }; customizeCSMwindow::customizeCSMwindow() // Here we set docElem of the **container**. { docElem = " docElem in customizeCSMwindow"; } void subMenuLists::changeWidget(int index) // In the **element** we want to access the docElem of the **container** { // How to access that docElem here?? std::cout << "\nIn changeWidget:" << m_pContainer->docElem << "\n"; } int main() { customizeCSMwindow job; subMenuLists menu(&job); job.setMenuList(&menu); menu.changeWidget(0); } 中可以有多个元素,则应避免使用原始指针。您应该使用menuList代替std::vector。如果这只是某种指针实现pimpl(参见Scott Meyers有效的c ++)那么原始指针就可以了。

关于良好的实施,还有太多的话要说。您需要阅读有关该书的书籍(例如,标准书籍[Stroustrup:C ++]或[Scott Meyers:Effective C ++]或您在讨论中通过谷歌轻松找到的好参考书)。

std::shared_ptr中只有一个可能的多个元素实现。

menuList

答案 2 :(得分:0)

继承的主要目的是使基类的公共(和受保护)数据和函数可用于派生类,就好像数据和函数是在派生类本身中声明一样。

例如:

class BASE {
public:
   int baseData; //the data you want to use
   void workOnBaseData(){
      baseData = 5;
   }
}; 

class DERIVED : public BASE {
public:
   int someData; //data member of the derived class
   void workOnBaseAndSomeData(){
      someData = baseData; //baseData is already known to DERIVED via inheritance
   }
};

执行此代码后,发现someData = 5! 希望这有帮助。

相关问题