Using an Initialized member in a function

Discussion in 'C++' started by 2005, Oct 26, 2006.

  1. 2005

    2005 Guest

    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
    2005, Oct 26, 2006
    #1
    1. Advertising

  2. 2005

    Dave Steffen Guest

    "2005" <> writes:

    > 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?

    ----------------------------------------------------------------------
    Dave Steffen, Ph.D.
    Software Engineer IV Disobey this command!
    Numerica Corporation - Douglas Hofstadter
    dgsteffen (usually at) numerica dot us
    Dave Steffen, Oct 26, 2006
    #2
    1. Advertising

  3. 2005

    Salt_Peter Guest

    2005 wrote:
    > 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.
    Salt_Peter, Oct 26, 2006
    #3
  4. 2005

    2005 Guest

    Dave Steffen wrote:
    > "2005" <> writes:
    >
    > > 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.
    >


    Thanks;

    So what or how can I assign the m_ticketNum value to this new node?
    2005, Oct 26, 2006
    #4
  5. 2005

    Daniel T. Guest

    "2005" <> wrote:

    > 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;
    }

    --
    To send me email, put "sheltie" in the subject.
    Daniel T., Oct 26, 2006
    #5
  6. 2005

    Dave Steffen Guest

    "2005" <> writes:

    > Dave Steffen wrote:
    > > "2005" <> writes:

    [...]
    > > > 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.
    > >

    >
    > 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?

    ----------------------------------------------------------------------
    Dave Steffen, Ph.D.
    Software Engineer IV Disobey this command!
    Numerica Corporation - Douglas Hofstadter
    dgsteffen (usually at) numerica dot us
    Dave Steffen, Oct 26, 2006
    #6
  7. 2005

    Salt_Peter Guest

    2005 wrote:
    > Dave Steffen wrote:
    > > "2005" <> writes:
    > >
    > > > 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.
    > >

    >
    > 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?
    Salt_Peter, Oct 27, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    5
    Views:
    589
    John Carson
    Jul 30, 2003
  2. slide_o_mix
    Replies:
    0
    Views:
    407
    slide_o_mix
    Oct 15, 2003
  3. ww
    Replies:
    4
    Views:
    377
    James Kanze
    Oct 26, 2007
  4. Peng Yu
    Replies:
    2
    Views:
    328
    Linlin Yan
    Sep 4, 2008
  5. , India
    Replies:
    5
    Views:
    1,766
    chisholm
    Feb 15, 2011
Loading...

Share This Page