Reputation: 3
I am trying to initialize a new object of a class Dlist. After a new object is declared the pointers first and last are supposed to be NULL. When I declare the Dlist temp first and last however- the constructor isn't being recognized and the compiler is giving them values like 0x0. I'm not sure why the the constructor being recognized.
// dlist.h
class Dlist {
private:
// DATA MEMBERS
struct Node
{
char data;
Node *back;
Node *next;
};
Node *first;
Node *last;
// PRIVATE FUNCTION
Node* get_node( Node* back_link, const char entry, Node* for_link );
public:
// CONSTRUCTOR
Dlist(){ first = NULL; last = NULL; } // initialization of first and last
// DESTRUCTOR
~Dlist();
// MODIFIER FUNCTIONS
void append( char entry);
bool empty();
void remove_last();
//CONSTANT FUNCTIONS
friend ostream& operator << ( ostream& out_s, Dlist dl);
};
#endif
// implementation file
int main()
{
Dlist temp;
char ch;
cout << "Enter a line of characters; # => delete the last character." << endl
<< "-> ";
cin.get(ch);
temp.append(ch);
cout << temp;
return 0;
}
Upvotes: 0
Views: 247
Reputation: 395
0x0 is NULL. Also, initialization of class members is more efficiently done via the constructor's initialization list:
Dlist()
: first(nullptr)
, last(nullptr)
{ /* No assignment necessary */ }
When a class is constructed, the initialization list is applied to the memory acquired for the object before the body of the constructor is executed.
Upvotes: 1