0

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;
}
4
  • Do you understand that NULL and 0x0 are the same value? Commented Oct 6, 2014 at 3:12
  • 1
    0x0 means zero, which (usually) is the address used to represent null. Commented Oct 6, 2014 at 3:13
  • What do you mean constructor isn't recognized? You can't step into it in the debugger? Commented Oct 6, 2014 at 3:17
  • You aren't initializing the pointers, you're assigning to them. Commented Oct 6, 2014 at 3:18

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Well, technically NULL is not required to be zero by the C specification -- though it is required to compare equal to 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.