Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

element pointer in class element

Status
Not open for further replies.

horst123

Programmer
Mar 21, 2005
18
DE
Hi,

i've got a problem. i want to implement a class called element, which has a pointer called ele, like this:

class element {
int i_a;
element* p_ele;
}

but if i write: element p_new = new element; the pointer p_ele won't point to NULL but to a location, where i_a is initialized whith 0. can sombody explain that to me? i want to have a null pointer if i create a new element...

thanx
 
Write a constructor for your class (here's a primary role of ctors to initialize members). Default class constructor does not initialize members at all (warning: garbage initial values!).
Code:
class Element
{
public:
  Element():p_ele(0) {}
  Element* p_ele; // or better private with get/set members
  ...
};
Apropos: your class has only private members, nonody can access them...
Traditionally user-defined class names are with capital 1st letter (it's not the language rule;)...
 
As for the "why":

Dynamic memory is like bowling shoes. When someone gets done with it, they give it back to the system, and then anyone else can grab it. Whenever you grab some memory, you don't know who was using it last, or where their "feet" have been.

Constructors are like socks. Always wear socks.
 
Hi,

you don't get my problem i think, perhaps i described bad...

it's like that: i have this element class. okay if i wirte element* ele = new ele there is memory allocated on the stack or elsewhere.

but my problem is, that automatically there is memory allocated for the next pointer without any next = new element;

and i can write next -> anything without null pointer exception.


any idea why?
 
I'm afraid now I truly don't understand you.
You have an element of what? Is it linked list of these elements? If so, you must think about list class (or simply use std::list<element>).
Now (in your snippet) you have a class with private data members without member functions - black box without values and behaviour...

Well, suppose we have:
Code:
element* p = new element;
A new element object was allocated in the heap (dynamic memory pool). That's all. Where is your next element (referenced via p_ele pointer) in this moment? Can you explain it? What the value we must set to it in this moment (except 0, of course)?
If you want a list class with add_new() or what else member function - do that...
 
Have you actually initialised the pointer anywhere

ArkM
Code:
Element():p_ele(0) {}

or with a constructor such as
Code:
Element::Element()
{
  p_ele = 0;
  i_a = 0;
}

otherwise it could easily be pointing to memory but not memory that you control

i_a may be 0 when you run the code but you can't guarantee it unless you explicitly set it to 0 somewhere



"If it could have gone wrong earlier and it didn't, it ultimately would have been beneficial for it to have." : Murphy's Ultimate Corollary
 
Surely you wanted that
Code:
template<class T> class ExoticPtr
{
  T*	p;
public:
  ExoticPtr():p(0) {}
  T* operator ->() { return p?p:(p=new T); }
  operator T*() { return p; }
  T& operator *() { return *(p?p:(p=new T)); }
  T* operator =(T* pp) { return p = pp; }
};

struct Exot
{
  int	i;
  ExoticPtr<Exot> next;
// Now for illustration only members:
	Exot():next() { i = ++n; }
	operator int() const { return i; }
	static int	n;
};

int	Exot::n = 0;

int main(int argc, char* argv[])
{
	Exot	e;

	cout << e << endl;

	int x = e.next->i;

	cout << *e.next << endl;
	cout << "Exots: " << Exot::n << endl;
	
	Exot*	pe = e.next->next;

	if (pe)
		cout << *pe << endl;
	else
		cout << "null" << endl;
	cout << "Exots: " << Exot::n << endl;	

	return 0;
}
?..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top