VoyForums
[ Show ]
Support VoyForums
[ Shrink ]
VoyForums Announcement: Programming and providing support for this service has been a labor of love since 1997. We are one of the few services online who values our users' privacy, and have never sold your information. We have even fought hard to defend your privacy in legal cases; however, we've done it with almost no financial support -- paying out of pocket to continue providing the service. Due to the issues imposed on us by advertisers, we also stopped hosting most ads on the forums many years ago. We hope you appreciate our efforts.

Show your support by donating any amount. (Note: We are still technically a for-profit company, so your contribution is not tax-deductible.) PayPal Acct: Feedback:

Donate to VoyForums (PayPal):

Login ] [ Main index ] [ Post a new message ] [ Search | Check update time | Archives: 1[2] ]
Subject: fas


Author:
asdf
[ Next Thread | Previous Thread | Next Message | Previous Message ]
Date Posted: 13:38:07 07/29/01 Sun

IN DERIVED CLASS DECLARATION:

class Vehicle_Class_Name:public baseClass
{
private:

public:

void * operator new(size_t s);
void operator delete(void * mem);

}

IN DERIVED CLASS IMPLEMENTATION

void * Vehicle_Class_Name::operator new(size_t s)
{
cout << "Vehicle_Class overloaded new called\n";
int id = SC::getLastID(); // assign a new index to the new object
SC::vpVehicles.push_back(::new Vehicle_Class_Name());
int sz = SC::vpVehicles.size();
SC::vpVehicles[sz-1]->setID(id);
SC::incrLastID();
SC::setNew();
return SC::vpVehicles[sz-1];
}


void Vehicle_Class_Name::operator delete(void * mem)
{
cout << " Vehicle_Class overloaded delete called \n";

vector::iterator first = SC::vpVehicles.begin(), last = SC::vpVehicles.end(), it;

it = find(first, last, (baseClass*)mem);
if(it != last)
{
::delete mem;
*it = NULL; // set mem = NULL
SC::setDelete();
}
else
cerr <<"Nothing can be deleted\n";
}



Note:
1) The derived class must include SC.h, which is from Simulation Controller. Some other header files are also necessary. See the following example.

2) The overloaded operators, “new” and “delete” must be declared and implemented in the derived class.

3) Vehicle_Class_Name can be AircraftCarrier, Aircraft, Destroyer, Cruiser, Battleship, Submarine, and various Weapon class names

4) Object can be created in the following way, eg., in AircraftCarrier class, when necessary:

Vector v1(1, 2, 3), v2(2, 3, 4);
flg = ‘R’ or ‘B’;
Weapon *pw = new Weapon(flg, v1, v2);
delete pw;

In this case, the operators “new” and “delete” here are the overloaded operators in the Weapon class.

Example:
//////////////////////////////////////////////////////////////////////////////////

//aircraft.h
#include "stdafx.h"

#include
#include
#include
#include
#include
#include //find()

using namespace std;

#ifndef _AIRCRAFT_H_
#define _AIRCRAFT_H_

#include "baseclass.h"
#include "Vector.h"
#include "SC.h"

class Aircraft:public baseClass
{
private:
int type;
Vector currPos; // initial position
Vector destPos; // destination position
Vector buffPos;
bool active;
char flag;

public:
Aircraft();
Aircraft(char fl, Vector& cPos, Vector& dPos);
~Aircraft();
char getFlag();
Vector getPosition();
void updatePosition();
int getType();
bool isActive();
void execute(double t);
void * operator new(size_t s);
void operator delete(void * mem);
};


#endif

[ Next Thread | Previous Thread | Next Message | Previous Message ]



Forum timezone: GMT-8
VF Version: 3.00b, ConfDB:
Before posting please read our privacy policy.
VoyForums(tm) is a Free Service from Voyager Info-Systems.
Copyright © 1998-2019 Voyager Info-Systems. All Rights Reserved.