指针继承类的问题

时间:2013-09-03 19:27:20

标签: c++ pointers inheritance

在下面的代码中,SecClass从FirstClass继承'item'。但是,当我在main中运行'ClassC'时,第一项和第二项都是210.如果我从继承类(FirstClass)中删除指针,它将按预期运行。对于指针和继承,我仍然相当摇摇欲坠,所以我在这里缺少一些东西......

// declaration of FirstClass
// FirstClass.h

#include <iostream>

#ifndef FIRSTCLASS_H
#define FIRSTCLASS_H

using namespace std;

class FirstClass
{
 private:
   int *item;

 public:
   FirstClass();
   FirstClass(int thatItem);
   ~FirstClass();

   int getItem();
   void setItem(int thatItem);

};

#endif


// the implementation of FirstClass.h
// FirstClass.cpp

#include "FirstClass.h"

using namespace std;

FirstClass::FirstClass()
{

int *setInitial = new int;
*setInitial = 5;
item = setInitial;
}

FirstClass::FirstClass(int thatItem)
{
item = &thatItem;
}

FirstClass::~FirstClass(){}

int FirstClass::getItem()
{
    return *item;
}

void FirstClass::setItem(int thatItem) 
{
    item = &thatItem;
}


// declaration of SecClass
// SecClass.h

#ifndef SECCLASS_H
#define SECCLASS_H

#include "FirstClass.h"

using namespace std;

class SecClass : public FirstClass
{
 private:
   int *secItem;

 public:
   SecClass();
   SecClass(int newItem, int thatItem);
   ~SecClass();

   int getSecItem();
   void setSecItem(int newItem);

};

#endif


// the implementation of SecClass.h
// SecClass.cpp

#include "SecClass.h"

using namespace std;

SecClass::SecClass()
{

int *setSecInitial = new int;
*setSecInitial = 16;
secItem = setSecInitial;
}

SecClass::SecClass(int newItem, int thatItem) : FirstClass(thatItem)
{
    secItem = &newItem;
}

SecClass::~SecClass(){}

int SecClass::getSecItem()
{
    return *secItem;
}

void SecClass::setSecItem(int newItem) 
{
    secItem = &newItem;
}


// main program
#include <iostream>
#include "FirstClass.h"
#include "SecClass.h"

using namespace std;

int main()
{

FirstClass classA;
cout << "classA item: " << classA.getItem() << endl << endl;

FirstClass classZ(86);
cout << "classZ item: " << classZ.getItem() << endl << endl;

SecClass classB;
cout << "classB first item: " << classB.getItem() << endl;
cout << "classB second item: " << classB.getSecItem() << endl << endl;

SecClass classC(72, 210);
cout << "classC first item: " << classC.getItem() << endl;
cout << "classC second item: " << classC.getSecItem() << endl;


return 0;
}

2 个答案:

答案 0 :(得分:4)

你的构造函数

void FirstClass::setItem(int thatItem) 
{
    item = &thatItem;
}

正在做一件非常糟糕的事情,即存储临时对象的地址(传递给构造函数的int)。

从构造函数返回后,int将被销毁,你的item指针将指向一个已被重用的内存区域。

当你做这类事情时会发生任何事情(包括daemons flying out of your nostrils)所以不要这样做。

答案 1 :(得分:0)

以下是使用指针的修改代码。有用。正如你所提到的,它只是为了 了解指针等。

    class FirstClass
{
 private:
   int *item;

 public:
   FirstClass();
   FirstClass(int *thatItem);
   ~FirstClass();

   int getItem();
};

FirstClass::FirstClass()
{
item = new int(5);
}

FirstClass::FirstClass(int *thatItem)
{
item = thatItem;
}

FirstClass::~FirstClass(){}

int FirstClass::getItem()
{
    return *item;
}

class SecClass : public FirstClass
{
    private:
    int *secItem;

    public:
    SecClass();
    SecClass(int *newItem, int *thatItem);
    ~SecClass();

    int getSecItem();
};

SecClass::SecClass()
{
secItem = new int(16);
}

SecClass::SecClass(int *newItem, int *thatItem) : FirstClass(thatItem)
{
    secItem = newItem;
}

SecClass::~SecClass(){}

int SecClass::getSecItem()
{
    return *secItem;
}

int main()
{

FirstClass classA;
cout << "classA item: " << classA.getItem() << endl << endl;

int *i = new int(86);
FirstClass classZ(i);
cout << "classZ item: " << classZ.getItem() << endl << endl;

SecClass classB;
cout << "classB first item: " << classB.getItem() << endl;
cout << "classB second item: " << classB.getSecItem() << endl << endl;

int *j = new int(72);
int *k = new int(210);
SecClass classC(j,k);
cout << "classC first item: " << classC.getItem() << endl;
cout << "classC second item: " << classC.getSecItem() << endl;


return 0;
}