Skip to main content
gamedev.net
🔒 Locked

delete/deconstructor help

Started by vaneger 3 replies 300+ views
Original Post
vaneger
vaneger

    
#ifndef GENERIC_H_

#define GENERIC_H_

//runtime type flags used by the Generic class

#define RT_int

#define RT_char

#define RT_long

#define RT_float

#define RT_double

#define RT_unsigned_int

#define RT_unsigned_long

/*
#define RT_apstring //replace ap classes with your own

#define RT_apvector //container classes of choice

#define RT_apmatrix //and uncomment this block

#define RT_apstack

#define RT_apqueue

#define RT_LList

#define RT_DyQueue

#define RT_DyStack

*/

class Generic
{
public:
Generic()
{
defined = false;
}
Generic(int type)
{
defined = true;
G_type = type;
}
Generic(int type,void* data)
{
defined = true;
G_type = type;
G_data = data;
}
~Generic()
{
	delete G_data;
}
//make G_data store new data

//assumes user works with same G_type

void modify(void *data)
{
G_data = data;
}
//return a copy of G_data

void *retrieve() const
{
return G_data;
}
//return size for type of data stored

int get_size() const
{
return G_size;
}
//destroy data and return a copy of its data

friend void *pop(Generic data)
{
void *temp = data.retrieve();
delete data;
return temp;
}

private:
int G_size;
int G_type;
void *G_data;
bool defined;
};
//type casting

const char* (Generic &data)
{
return ((char*)data.retrieve());
}
const int* (Generic &data)
{
return ((int*)data.retrieve());
}
const long* (Generic &data)
{
return ((long*)data.retrieve());
}
const float* (Generic &data)
{
return ((float*)data.retrieve());
}
const double* (Generic &data)
{
return ((double*)data.retrieve());
}
const unsigned int* (Generic &data)
{
return ((unsigned int*)data.retrieve());
}
const unsigned long* (Generic &data)
{
return ((unsigned long*)data.retrieve());
}
#endif
    
when it compiles it says cannot convert class generic to " in the pop function when it calls delete on data what im doing wrong? [edited by - Stoffel on July 31, 2002 2:24:13 PM]
Stoffel
Stoffel
The problems with pop are:
- you're passing it by value, so it'll copy your Generic.
- you're deleting an instance, not a pointer, which you can't do

You might want to post the entirety of the error--looks like it got clipped out of your post.

Also, if you're just messing around with things, that's fine, but if this is a central part of a project you're working on, I'd say this entire class is extremely ill-conceived. It's bad enough to strip all strict type checking from C++, but making each variable dynamically allocated? Yikes.

[edited by - Stoffel on July 31, 2002 2:30:56 PM]
vaneger
vaneger
well heres the updated code:

#ifndef GENERIC_H_
#define GENERIC_H_

//runtime type flags used by the Generic class
enum myCast { RT_int, RT_char, RT_long, RT_float, RT_double, RT_unsigned_int,RT_unsigned_long}typeCasts;

class Generic
{
public:
Generic()
{
defined = false;
}
Generic(myCast type)
{
defined = true;
G_type = type;
}
Generic(myCast type,void* data)
{
defined = true;
G_type = type;
G_data = data;
}
~Generic()
{
delete G_data;
}
//make G_data store new data
//assumes user works with same G_type
void modify(void *data)
{
G_data = data;
}
//return a copy of G_data
void *retrieve() const
{
return G_data;
}
//return size for type of data stored
int get_size() const
{
return G_size;
}
//destroy data and return a copy of its data
friend void *pop(Generic &data)
{
void *temp = data.retrieve();
delete &data
return temp;
}
//type casting
const char* Ch_cast(Generic &data)
{
return ((char*)data.retrieve());
}
const int* I_cast(Generic &data)
{
return ((int*)data.retrieve());
}
const long* L_cast(Generic &data)
{
return ((long*)data.retrieve());
}
const float* F_cast(Generic &data)
{
return ((float*)data.retrieve());
}
const double* D_cast(Generic &data)
{
return ((double*)data.retrieve());
}
const unsigned int* Ui_cast(Generic &data)
{
return ((unsigned int*)data.retrieve());
}
const unsigned long* Ul_cast(Generic &data)
{
return ((unsigned long*) data.retrieve());
}
private:
int G_size;
myCast G_type;
void *G_data;
bool defined;
};

#endif

everything except pop should work correctly, what i want it to do is create a deep copy of G_data then free the memory used by G_data and free the memory used by the instance of teh Generic class. thsi is used so ican create a stack that can accept all types of data for later use in a scripting langauge as a learning project for my self.
Stoffel
Stoffel
Please use [ source ] and [ /source ] tags around your code (without spaces).

What happens when I do this?

    
void *someFunc ()
{
Generic gen (RT_int);
return pop (gen);
}

pop will fail because gen is a stack variable, e.g. not on the heap, so you can't delete &get. This is the reason you shouldn't ever take the address of a reference that's passed in as an argument.

I don't think pop is a good function to have, period. Allow users of your class to get the data out, but make them responsible for destroying your generic in the right way.


[edited by - Stoffel on July 31, 2002 4:09:06 PM]
vaneger
vaneger

  
#ifndef GENERIC_H_
#define GENERIC_H_

//runtime type flags used by the Generic class

enum myCast { RT_int, RT_char, RT_long, RT_float, RT_double, RT_unsigned_int,RT_unsigned_long}typeCasts;

class Generic
{
public:
Generic()
{
defined = false;
}
Generic(myCast type)
{
defined = true;
G_type = type;
}
Generic(myCast type,void* data)
{
defined = true;
G_type = type;
G_data = data;
}
~Generic()
{
delete G_data;
}
//make G_data store new data

//assumes user works with same G_type

void modify(void *data)
{
G_data = data;
}
//return a copy of G_data

void *retrieve() const
{
return G_data;
}
//return size for type of data stored

int get_size() const
{
return G_size;
}
//destroy data and return a copy of its data

friend void *pop(Generic &data)
{
void *temp = data.retrieve();
delete &data
return temp;
}
//type casting

friend char* Ch_cast(Generic data)
{
return ((char*)data.retrieve());
}
friend int* I_cast(Generic data)
{
return ((int*)data.retrieve());
}
friend long* L_cast(Generic data)
{
return ((long*)data.retrieve());
}
friend float* F_cast(Generic data)
{
return ((float*)data.retrieve());
}
friend double* D_cast(Generic data)
{
return ((double*)data.retrieve());
}
friend unsigned int* Ui_cast(Generic data)
{
return ((unsigned int*)data.retrieve());
}
friend unsigned long* Ul_cast(Generic data)
{
return ((unsigned long*) data.retrieve());
}
private:
int G_size;
myCast G_type;
void *G_data;
bool defined;
};

#endif


#include"generic.h"
#include<iostream.h>
void main()
{
Generic X();
int Y = 5;
Generic Z(RT_int,&Y);
void *int a = I_cast(Z);
}


well umm im eventualyl going to need to use the pop function my self :/ so thats why im trying to get it right. how do i use a to get the value of Y (which a should now point to ) ?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.