爲什麼不std:cout
打印3
?C++繼承誤解
隨着構建派生對象,具有預定義的m_size
下面的代碼被開發的意圖。但似乎這個想法沒有正確實施,因爲std:cout
只能打印Data1Size
。有人能糾正我嗎?
untitled.pro
TARGET = untitled
TEMPLATE = app
QMAKE_CXXFLAGS += -std=c++0x
SOURCES += main.cpp \
genericdata.cpp
HEADERS += \
genericdata.h
的main.cpp
#include "genericdata.h"
int main(int argc, char *argv[])
{
Data1 data;
return 0;
}
genericdata.h
#ifndef GENERICDATA_H
#define GENERICDATA_H
#include <iostream>
class GenericData
{
int m_size;
protected:
const int Data1Size = 3;
public:
explicit GenericData(int size);
};
class Data1 : public GenericData {
public:
Data1() : GenericData(Data1Size) {}
};
#endif // GENERICDATA_H
genericdata.cpp
#include "genericdata.h"
GenericData::GenericData(int size) :
m_size(size)
{
std::cout << "m_size: " << m_size << std::endl;
}
究竟輸出了什麼?你期望輸出什麼? –
得到了'm_size:57',並期待'm_size:3'。說得通? – KcFnMi
當你單獨使用調試器時發生了什麼? –