VoyForums

VoyUser Login optional ] [ Main index ] [ Post a new message ] [ Search | Check update time ]
Subject: asdf


Author:
asfd
[ Next Thread | Previous Thread | Next Message | Previous Message ]
Date Posted: 13:35:21 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 ]


VoyUser Login ] Not required to post.
Post a public reply to this message | Go post a new public message
* Notice: Posting problems? [ Click here ]
* HTML allowed in marked fields.
Message subject (required):

Name (required):

  Expression (Optional mood/title along with your name) Examples: (happy, sad, The Joyful, etc.) help)

  E-mail address (optional):

* Type your message here:


Notice: Copies of your message may remain on this and other systems on internet. Please be respectful.


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