Using an Initialized member in a function

2

2005

Hi

I have initialized a member as below:
class CNode {
public:
CNode() : m_pNext(0), m_ticketNum(0) {}
----
private:
int m_ticketNum; // ticket number of car
CarNode *m_pNext;
};

In a function I am creating a new node, say

CNode *pCurNode;

I also wish to assign values to m_pNext & m_ticketNum - How? What is
the syntax?

Thanks
 
D

Dave Steffen

2005 said:
Hi

I have initialized a member as below:
class CNode {
public:
CNode() : m_pNext(0), m_ticketNum(0) {}

Idiomatically, that should be m_pNext(NULL), to emphasize to human
readers that you're initializing a pointer to null. That's just a
stylistic issue.
----
private:
int m_ticketNum; // ticket number of car
CarNode *m_pNext;
};

In a function I am creating a new node, say

CNode *pCurNode;

I also wish to assign values to m_pNext & m_ticketNum - How? What is
the syntax?

Umm... you don't. There is no CNode object in existence yet. You
only have a _pointer_ to a CNode. Presumably, someone somewhere
will eventually create an actual CNode object for pCurNode to point
at, and when that object is created, the members will be set. For
example,

pCurNode = new CNode();

dynamically creates a CNode object using the default ctor you
defined above.

Based on this question, and some other postings from you, I'm
inclined to think you need a better C++ textbook. Which one are you
using?
 
S

Salt_Peter

2005 said:
Hi

I have initialized a member as below:
class CNode {
public:
CNode() : m_pNext(0), m_ticketNum(0) {}
----
private:
int m_ticketNum; // ticket number of car
CarNode *m_pNext;
};

In a function I am creating a new node, say

CNode *pCurNode;

Thats not a new node, its just a dumb pointer.int main()
{
CNode node(0,

I also wish to assign values to m_pNext & m_ticketNum - How? What is
the syntax?

Thanks

You keep asking the same question. The above class only allows you to
create a default node where the ticket number and pointer_to_ next are
0. Also, the ctor is initializing the members in reverse order
(corrected below). Add a parametized ctor to initialize the private
members with supplied parameters.

class CNode
{
int m_ticketNum; // ticket number of car
CarNode *m_pNext;
public:
CNode() : m_ticketNum(0), m_pNext(0) { }
CNode(int num, CNode *p) : m_ticketNum(0), m_pNext(p) { }
};

int main()
{
CNode node; // a default CNode, m_ticketNum = 0
CNode another(1, &node); // a node with m_ticketNum = 1
// and a pointer to node
above
}

Learn how to use your debugger, set breakpoints and observe the ctors
being invoked.
This is trivial stuff, its nowhere near as complicated as what you'll
see later.
 
2

2005

Dave said:
Idiomatically, that should be m_pNext(NULL), to emphasize to human
readers that you're initializing a pointer to null. That's just a
stylistic issue.


Umm... you don't. There is no CNode object in existence yet. You
only have a _pointer_ to a CNode. Presumably, someone somewhere
will eventually create an actual CNode object for pCurNode to point
at, and when that object is created, the members will be set. For
example,

pCurNode = new CNode();

dynamically creates a CNode object using the default ctor you
defined above.

Thanks;

So what or how can I assign the m_ticketNum value to this new node?
 
D

Daniel T.

2005 said:
I have initialized a member as below:
class CNode {
public:
CNode() : m_pNext(0), m_ticketNum(0) {}
----
private:
int m_ticketNum; // ticket number of car
CarNode *m_pNext;
};

In a function I am creating a new node, say

CNode *pCurNode;

The above doesn't create a new node. Either:

CNode curNode;

or

CNode* curNode = new CNode;
I also wish to assign values to m_pNext & m_ticketNum - How? What is
the syntax?

The variables are private so there is no syntax to access them from
outside the class. However from inside the class, you access them by
using their names. For e.g.:

void CNode::assignTicketNumber(int n) {
m_ticketNum = n;
}
 
D

Dave Steffen

2005 said:
Thanks;

So what or how can I assign the m_ticketNum value to this new node?

As Salt_Peter points out, you keep asking the same question over and
over. You need another constructor to do what I _think_ you want.

I ask again: what textbook are you reading that doesn't explain this
stuff? You _are_ reading a textbook, right?
 
S

Salt_Peter

2005 said:
Thanks;

So what or how can I assign the m_ticketNum value to this new node?

You can add a public member function that accesses and modifies the
private variables.
Something like void set(int n, CNode* p) but why do that when you can
have the ctor do it for you?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top