473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Using an Initialized member in a function

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

Oct 26 '06 #1
6 1378
"2005" <uw*****@yahoo.comwrites:
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
Oct 26 '06 #2

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.

Oct 26 '06 #3

Dave Steffen wrote:
"2005" <uw*****@yahoo.comwrites:
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?

Oct 26 '06 #4
"2005" <uw*****@yahoo.comwrote:
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.
Oct 26 '06 #5
"2005" <uw*****@yahoo.comwrites:
Dave Steffen wrote:
"2005" <uw*****@yahoo.comwrites:
[...]
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
Oct 26 '06 #6

2005 wrote:
Dave Steffen wrote:
"2005" <uw*****@yahoo.comwrites:
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?

Oct 27 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: pvdm | last post by:
Hi, I am writing an app which encapsulates a multimedia timer. I implemented a TimerProc as static member function and a static member variable pThis as pseudo this variable to access in the...
4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
138
by: ambika | last post by:
Hello, Am not very good with pointers in C,but I have a small doubt about the way these pointers work.. We all know that in an array say x,x is gonna point to the first element in that...
10
by: Juke All | last post by:
When I compile the code (below), I get this error: cannot convert parameter 1 from 'int' to 'union dna' Without saying: FOO x; x.val = 100; ....is it possible to use a union as a function...
14
by: Clint Olsen | last post by:
I was wondering if it's considered undefined behavior to use a member of a union when it wasn't initialized with that member. Example: typedef unsigned long hval_t; hval_t hval_init(void) {...
7
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
68
by: Jim Langston | last post by:
I remember there was a thread a while back that was talking about using the return value of a function as a reference where I had thought the reference would become invalidated because it was a...
2
by: Peng Yu | last post by:
Hi, I'm wondering if there is a way to initialized a member array just as the initialization of a member variable? Or I have to initialized the array inside the function body of the constructor?...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.