[Discuss] OT - C++ operator overloading question.
David Wu
david.yk.wu at gmail.com
Wed Aug 30 03:23:54 PDT 2006
Hi guys,
Since I am using g++ and linux as my development enviroment, I thought this
might be ok to ask this question here. If its regarded to be an improper
question to be asked on this list, maybe someone can point me to a good
forume or good mailling list for C++ questions.
I am trying to do simple operator overloading for a user-defined class.
=================NumData.h========================
#ifndef __NUMBER_DATA_CLASS__
#define __NUMBER_DATA_CLASS__
#include <iostream.h>
#include "stdio.h"
using namespace std;
class NumData
{
public:
NumData(int a, long long b);
~NumData();
NumData& operator++();
NumData& operator--();
void displayInfo();
private:
int m_nValue;
long long m_llnValue;
};
#endif
=================================================
===================NumData.cpp====================
#include "opOverload.h"
NumData::NumData(int a, long long b):
m_nValue(a)
, m_llnValue(b)
{
}
NumData::~NumData()
{
}
NumData& NumData::operator++()
{
++(this->m_nValue);
++(this->m_llnValue);
return *this;
}
NumData& NumData::operator--()
{
--m_nValue;
--m_llnValue;
return *this;
}
void NumData::displayInfo()
{
cout << "Integer Value is " << m_nValue << endl;
cout << "Long long Value is " << m_llnValue << endl;
}
==========================================
============main.cpp========================
#include <stdio.h>
#include "opOverload.h"
using namespace std;
int main()
{
NumData* myNumData = new NumData(1, 10);
myNumData->displayInfo();
++myNumData;
myNumData->displayInfo();
if(myNumData != NULL)
{
delete myNumData;
myNumData = NULL;
}
return 0;
}
===============================================
Here is my runtime result,
[root at localhost cppLanguageTests]# ./main
Integer Value is 1
Long long Value is 10
Integer Value is 135153
Long long Value is 0
*** glibc detected *** free(): invalid pointer: 0x08d66014 ***
Aborted
Two things that are wrong!
First, after the ++myNumData call in my main, printing the member value does
not show the corrent value!
Second, deleting myNumData seems to cause seg fault!
hope someone can give me some clues!!
Thanks guys!
More information about the Discuss
mailing list