473,785 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1400
"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::assignTi cketNumber(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
8867
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 static TimerProc function. timeSetEvent uses TimerProc to set the callback function.
4
4739
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 14882:2003(E) §8.5 says:   To zero-initialize an object of type T means: 5   -- if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
3
24036
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 tables in the database where the code resides. If we move the database with the data tables to a new directory, the links are no longer valid. I tried to update the links by changing the Connect property and refreshing: Set td = db.TableDefs(0)...
138
5292
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 array(i.e)it will have the address of the first element.In the the program below am not able to increment the value stored in x,which is the address of the first element.Why am I not able to do that?Afterall 1 is also a hexadecimal number then...
10
6930
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 parameter, and when calling that function, pass the argument as one of the types of the
14
2189
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) { union hval_init_u { double dbl; hval_t hval; }; union hval_init_u phi = { (sqrt(5) + 1) / 2 }; /* golden ratio */
7
9837
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
68
4655
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 temporary but it was stated that it would not. This has come up in an irc channel but I can not find the original thread, nor can I get any code to work. Foo& Bar( int Val ) { return Foo( Val ); }
2
2544
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? Thanks, Peng #include <iostream>
0
10330
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10153
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9952
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3654
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.