A .357 vanishes under a tweed jacket (dyskordus) wrote in cpp,
A .357 vanishes under a tweed jacket
dyskordus
cpp

Intro/noobish question

Howdy,
I'm a Unix guy who decided to go back to school and get his CS degree. I've done a fair bit of procedural programming in the past (mostly perl, other things too) but not much OO.
Right now I'm working on an assignment where I have a pointer to an array of structs. Each struct contains a boolean value and a pointer to a class. The class isn't really too exciting, it just has a C string and some functions to view/modify it.
When I try to set the value of the C string by directly accessing elements of the array, they all end up having the same value. On the other hand, if I use pointer arithmetic they end up working like I want them too. Unfortunately using pointer arithmetic will be very impractical for what I am trying to do. Code is behind the cut:


Here's the struct:
struct item
{
     bool empty;
     data  * name;
};


Here's the prototype for the data class:
class data
{
public:
	data() : name(NULL) {}				// default constructor
	data(char const * const name);		// constructor
	~data();							// destructor

	data& operator=(const data& data2);

	char const * const getName() const;
	void setName(char const * const name);

private:
	char * name;

	friend ostream& operator<< (ostream& out, const data& outData);
	friend bool operator< (const data& d1, const data& d2);
	friend bool operator== (const data& d1, const data& d2);
};


Here's the constructor for the data class:

data::data(char const * const name)
{

	this->name = new char[strlen(name) + 1];
	strcpy(this->name,name);
}


Here's what I try to do that is not working:
void BST::insert (const data& aData)
{
	this->items[tempcounter].name = new data(aData.getName());
	tempcounter ++;
}


tempcounter is initialized to zero

Here's what does work, but won't really fit in with the rest of the assignment

void BST::insert (const data& aData)
{
         this->items->name = new data(aData.getName());
         items ++;
}


  • Post a new comment

    Error

    default userpic
  • 8 comments