c++ - 来自第三类对象 C++ 的垃圾值

标签 c++

以下是股票来源的实现文件片段:

 stock::stock(char const * const symbol, char const * const name, int sharePrice, date priceDate )
 : m_sharePrice( sharePrice )
 {
    if ( symbol )
    {
        size_t length = strlen( symbol ) +1;
        m_symbol = new char[length];
        strcpy_s( m_symbol, length, symbol );
    }
    else m_symbol = NULL;
    if ( name )
    {
            size_t length = strlen( name ) +1;
       m_name = new char[length];
       strcpy_s( m_name, length, name );
    }
    else m_name = NULL;

    dateObj = &priceDate;
 }

这就是我用 cstrings 分配内存的方法。主要是传递参数,如“symbol,”name,10,date::JANUARY, 17, 1967遇到麻烦。从一个源文件移动到下一个源文件时,我无法保持属性。我在上面的函数中看到,“dateObj”具有我需要的属性。但是当我将它转移到另一个源文件中时值(value)观消失了......

pragma once
#include <ostream>
using namespace std;

class date
  {
  public:
    typedef enum {INVALID, JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE,
              JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER,DECEMBER}
    Month;

    date(Month month, int day, int year);
    date(const date& date);         // copy constructor
    date(void);                              // default constructor
    ~date(void);

    friend ostream& operator<<(ostream& out, const date& d);

 private:
    Month   month;
    int     day;
    int     year;
};

因此,当我需要添加到 hashTable 时,反之亦然,我最终得到的值不多。

bool hashmap::get(char const * const symbol, stock& s) const
{
    int index = 0;
    // search for the stock associated with the symbol.
    while ( index < maxSize )
    {
        if ( hashTable[index].m_symbol == NULL ) 
        {
            index++;
        }
        else if ( strcmp( symbol, hashTable[index].m_symbol  ) == 0 )
        {
        //s = *hashTable; // call assignemnt overload
             s.m_name = hashTable[index].m_name; // has correct value
             s.dateObj = hashTable[index].dateObj; // GARBLE!
             s.m_sharePrice = hashTable[index].m_sharePrice; // correct 
             s.m_symbol = hashTable[index].m_symbol; // correct
             return true;
        }
    }
        return false;
    }

也许我可以在其中一个头文件中添加一些内容以使其更容易。

 #include "date.h"

 using namespace std;

 class stock
 {
  public:
    stock(char const * const symbol, char const * const name, int sharePrice, date priceDate);
    stock(const stock& s);  // copy constructor
    stock(void);                                     // default constructor
    char const * const getSymbol(void) const;
    stock& operator=(const stock& s);
    stock& operator=(stock const * const s);
    ~stock(void);

    // display column headers
    static void displayHeaders(ostream& out);

     friend ostream& operator<<(ostream& out, const stock& s);

     friend class hashmap;
     private:

     date *dateObj;
     char *m_symbol;
     char *m_name;
     int   m_sharePrice;

     static int   maxSize;
  };

 #include "stock.h"

 class hashmap
 {
  public:
hashmap(int capacity);
~hashmap(void);
bool get(char const * const symbol, stock& s) const;

    bool put(const stock& s, int& usedIndex, int& hashIndex, int& symbolHash);
    bool remove(char const * const symbol);

    friend ostream& operator<<(ostream& out, const hashmap& h);

private:
        static int hashStr(char const * const symbol);
    friend class stock;

    stock *hashTable;
 };

这可能是我可以解决日期属性不保留其值的问题的地方。我只是不确定在哪里做什么。

 #include "date.h"

 date::date(Month month, int day, int year)
 {
    this->month = month;
    this->day  = day;
    this->year = year;
 }

date::date(const date& date)
{
}

date::date()
{
    day = this->day;
    year = this->year;  
        month = this->month;
}

date::~date(void)
{

}

ostream& operator<<(ostream& out, const date& d)
{
   out << d.day << d.month << d.year << endl;
        return out;
}

最佳答案

stock::stock(/* snip */ date priceDate ) : m_sharePrice( sharePrice )
{
    /* Snip */
    dateObj = &priceDate;
}

不要那样做。 priceDate 是堆栈中的参数之一。一旦构造器结束,&priceDate 就变成了一个过时的指针。考虑实际复制对象(而不是使用指针,例如声明 date dateObj; 并使用 dateObj = priceDate; 分配)

关于c++ - 来自第三类对象 C++ 的垃圾值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1528609/

相关文章:

java - 从类类型实例化 C++ 中的类作为 std::map 值

c++ - 通过引用将参数传递给 std::async 失败

c++ - 稳定的婚姻问题幸福系数

c++ - 当我创建我的助手类时,我是否过度设计了?

c++ - 互斥量没有正确使用?继续过去的问题

c++ - "if" "elseif" "else"语句有哪些好的做法

c++ - 让用户决定在 C++ 中运行哪个函数的最佳方法?

c++ - 如何在不导致内存泄漏的情况下用另一个动态数组替换一个动态数组?

c++ - 我可以在 C++ 中执行以下操作吗

c++ - 将不同的对象存储为 void* 并从 void* 转换到 void*