Member, Class, Inline: Initialization question

2

2005

Hi

If I have a class,

class CNode {
public:
CarNode() : m_pNext(0), m_ticketNum(0) {}
~CarNode();

private:
int m_ticketNum; // ticket number of car
CNode *m_pNext; // pointer to next node in stack
};


1) Can I do the following initialization? If not what modification is
needed?
CarNode() : m_pNext(0), m_ticketNum(0) {m_pNext = m_pNext = 0 ;}

2) Does the above means that I do Not need the following?
void CNode::CNode()
{ ...
}

Thanks
 
M

mimi

2005 写é“:
Correctred the typo below:
This means an CNode object is created, it is created with m_pNext
values 0 and m_ticketNum values 0。Sure you can do this。Ignoring your typing error, It is correct in
syntax, but it just like holding a candle to the sun.
 
S

Salt_Peter

2005 said:
Correctred the typo below:

Note that your destructor is *not* implemented.
And yes, you definitely need one, even if it is the compiler generated
d~tor.

The default ctor is already implemented in the CNode class.

class CNode
{
public:
CNode() : m_pNext(0), m_ticketNum(0) { }
...
};

And the above ctor sets both the pointer and integer to NULL and 0
respectfully.
No need for anything in the { } body.

The alternative approach is to seperate declaration and implementation.
I'm skipping the other details on purpose. Note the include guards.
_______________________________
// cnode.h
#ifndef CNODE_H_
#define CNODE_H_ // include guard


class CNode
{
... // private members
public:
CNode(); // <- declaration only
~CNode(); // same
};

#endif /* include guard CNODE_H_ */
_______________________________
// cnode.cpp
#include "cnode.h"

/* ctor */
CNode::CNode() : : m_pNext(0), m_ticketNum(0)
{
}

/* d~tor */
CNode::~CNode()
{
}

________________________________

As a recap, your class should look like this if you don't plan to
seperate interface and implementation. Note that its perfectly valid to
overload the ctor.

class CNode
{
int m_ticketNum; // private
CNode *m_pNext;
public:
CNode() : m_pNext(0), m_ticketNum(0) { } // ok
CNode(CNode* p, int n) : m_pNext(p), m_ticketNum(n) { } // ok
~CNode() { } // ok, declaration + implementation
};

I hope i'm not confusing you.
Most of the guys here are better at explaining this stuff anyways.
 
F

Frederick Gotham

2005 posted:
class CNode {
public:
CarNode() : m_pNext(0), m_ticketNum(0) {}
~CarNode();

private:
int m_ticketNum; // ticket number of car
CNode *m_pNext; // pointer to next node in stack
};


1) Can I do the following initialization? If not what modification is
needed?
CarNode() : m_pNext(0), m_ticketNum(0) {m_pNext = m_pNext = 0 ;}


This initialisation is identical to the previous one.

However, in _this_ constructor, you perform an assignment. I think,
however, that the behaviour of the assignment statement is undefined as you
alter a variable more than once between sequence points.

2) Does the above means that I do Not need the following?
void CNode::CNode()
{ ...
}


Haven't a clue what you're talking about.
 
2

2005

Thanks and pls see a question below:

Salt_Peter said:
2005 wrote:
________________________________

As a recap, your class should look like this if you don't plan to
seperate interface and implementation. Note that its perfectly valid to
overload the ctor.

class CNode
{
int m_ticketNum; // private
CNode *m_pNext;
public:
CNode() : m_pNext(0), m_ticketNum(0) { } // ok
CNode(CNode* p, int n) : m_pNext(p), m_ticketNum(n) { } // ok

Am I correct that the line above doen't initialize the p and n to = 0,
ie p = n = 0
Whereas the 2nd line above does it?
In otherwords, the above 2 lines are correct but the latter line
doesn't initialize n, p to 0?
 
B

BobR

2005 wrote in message
Am I correct that the line above doen't initialize the p and n to = 0,
ie p = n = 0
Whereas the 2nd line above does it?
In otherwords, the above 2 lines are correct but the latter line
doesn't initialize n, p to 0?

CNode cn;
// uses:
// CNode() : m_pNext(0), m_ticketNum(0) { } // ok
// m_pNext and m_ticketNum are initialised to zero.

CNode cn2( &cn, 23);
// uses:
// CNode(CNode* p, int n) : m_pNext(p), m_ticketNum(n) { } // ok
// m_pNext "points" to object 'cn', and m_ticketNum = 23.

Does that help?
 
S

Salt_Peter

2005 said:
Thanks and pls see a question below:




Am I correct that the line above doen't initialize the p and n to = 0,
ie p = n = 0
Whereas the 2nd line above does it?
In otherwords, the above 2 lines are correct but the latter line
doesn't initialize n, p to 0?

Not neccessarily, if i declare a variable as follows, the parametized
ctor does set p and n to 0.

CNode instance(0, 0);

Each line is a distinct constructor with a distinct signature.
The first is a default constructor that initializes the members to 0.
No parameter required.
The second line is a parametized ctor and initializes the members
depending on what is passed to p and n. And you can add as many ctors
as you like / need.

int main()
{
CNode node; // invokes the default ctor since no parameter is
provided.
// all members are initialized to 0
CNode( &node, 9 ); // invokes the parametized ctor and sets the
members accordingly.
}
Try it. Its not a complicated system. Its actually quite simple.
Remember that the following is not a complete ctor, it only declares a
constructor which needs to be implemented somewhere - unless you plan
to never use it.
Class CNode
{
public:
CNode(); // <- semicolon, only a declaration
};

The following both declares *and* implements the ctor:
Class CNode
{
public:
CNode() { } // declaration + definition
};

Which is the same as:
Class CNode
{
public:
CNode(); // <- declaration
};

CNode::CNode() { } // <- definition
___
 
2

2005

BobR said:
2005 wrote in message


CNode cn;
// uses:
// CNode() : m_pNext(0), m_ticketNum(0) { } // ok
// m_pNext and m_ticketNum are initialised to zero.

CNode cn2( &cn, 23);
// uses:
// CNode(CNode* p, int n) : m_pNext(p), m_ticketNum(n) { } // ok
// m_pNext "points" to object 'cn', and m_ticketNum = 23.

Does that help?

Assuming initializing (to NULL) at declaration is a good practice, am I
right that your first method is preferred - unless there is no harm in
since it might be just a protocol?
 
B

BobR

2005 wrote in message
Assuming initializing (to NULL) at declaration is a good practice, am I
right that your first method is preferred - unless there is no harm in
since it might be just a protocol?

It's not a matter of 'preferance'. You instantiate the way *you need to*, the
compiler will try to fit it using the interface available in the class.

Say you really need to do this (for some reason):

CNode cn3( "Hello Mom." );

Try that now....<waiting>... Got a compile error, didn't you? It told you
there was no constructor with that 'signature' (not in those words, of
course).
So, you go back to your class definition and add:

CNode( std::string const &msg ) : m_pNext(0), m_ticketNum(0) {
std::cout << msg << std::endl;
}

Now you can use:
CNode cn3( "Hello Mom." );

Mr. Salt_Peter showed you that:

CNode cn;
... and:
CNode cn2( 0, 0 );

....would bring objects 'cn' and 'cn2' into existance with exactly the same
states. If you *always* did that, you could just eliminate the second
constructor, it would be useless.

We both showed you the more probable use according to the way the class
interface was designed:

CNode cn;
CNode cn2( &cn, 2 );
CNode cn3( &cn2, 3 ); // building an 'list'

Does that help? If not, please re-word your question and post again.
 

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

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top