help with pointer

O

Obelix

It's many time I don't use C++ (now I'm working with java).


La domanda è questa. Come passo un puntatore al costruttore di una classe ?

How can I pass a pointer to construct's class


class CHeader {

const CXML *pXml;


public:

// Costruttori/Distruttori

CHeader(const CXML *pHead)
:pXml(*pHead) {};
}


It's right ?
But, if I would like to not declare pXml as const.... Have someone any idea ?


Thanks,
Obelix.
 
A

anon

Obelix said:
It's many time I don't use C++ (now I'm working with java).


La domanda è questa. Come passo un puntatore al costruttore di una classe ?

How can I pass a pointer to construct's class


class CHeader {

const CXML *pXml;


public:

// Costruttori/Distruttori

CHeader(const CXML *pHead)
:pXml(*pHead) {};
}

Here you have an error. Should be:
:pXml(pHead) {};
 
G

Gavin Deane

It's many time I don't use C++ (now I'm working with java).

La domanda è questa. Come passo un puntatore al costruttore di una classe ?

How can I pass a pointer to construct's class

class CHeader {

const CXML *pXml;

public:

// Costruttori/Distruttori

CHeader(const CXML *pHead)
:pXml(*pHead) {};
}

It's right ?

What does your compiler say about the :pXml(*pHead) part?

pXml is of type pointer to (const) CXML
*pHead (the expression you use to initialise pXml) is of type (const)
CXML

I've put const in parentheses there because const is not part of the
problem. You are trying to initialise a pointer with an object. Did
you mean this?

CHeader(const CXML *pHead) : pXml(pHead) {};
But, if I would like to not declare pXml as const.... Have someone any idea ?

Fix the above error, which has nothing to do with const. After that
then if you remove const from the declaration of pXml then you will
also have to remove const from the declaration of pHead in the
constructor argument list. This makes sense - if pXml is a pointer to
non-const data, then you wouldn't ever want to initialise a CHeader
with a pointer to a const object.

Gavin Deane
 
O

Obelix

Gavin Deane ha scritto:
On 30 Mar, 15:03, Obelix <[email protected]> wrote:
CHeader(const CXML *pHead) : pXml(pHead) {};

ok. an digit error

Fix the above error, which has nothing to do with const. After that
then if you remove const from the declaration of pXml then you will
also have to remove const from the declaration of pHead in the
constructor argument list. This makes sense - if pXml is a pointer to
non-const data, then you wouldn't ever want to initialise a CHeader
with a pointer to a const object.

that's ok. But, If I pass a non const pointer to a class method, is there a
probability that pHead is wrong or not ? In any case can I consider pHead right,
or sometime, pHead can assume a corrupt state ?


Thanks,
Obelix
 
G

Gavin Deane

GavinDeaneha scritto:



ok. an digit error


that's ok. But, If I pass a non const pointer to a class method, is there a
probability that pHead is wrong or not ? In any case can I consider pHead right,
or sometime, pHead can assume a corrupt state ?

I'm not sure this answers your question, but ...

pHead is just a parameter of the constructor. After the constructor
finishes, pHead no longer exists. Presumably you are only ever going
to pass a pointer to a valid CXML object (or a null pointer if your
design allows for that) to the CHeader constructor.

pXml is private data within the CHeader object, so is under the
complete control of the object.

However, somewhere there must be a CXML object that pHead, and
therefore pXml, point to. The CXML object itself lives outside the
CHeader class so other parts of your program must have access to it.
In principle, the CXML object can be changed or destroyed without the
CHeader object knowing. It is up to you as the designer of this
program to make sure that anything that happens to the CXML object
does not cause any of the uses of the CHeader object to break.

Gavin Deane
 
J

Jim Langston

Obelix said:
Gavin Deane ha scritto:


ok. an digit error



that's ok. But, If I pass a non const pointer to a class method, is there
a probability that pHead is wrong or not ? In any case can I consider
pHead right, or sometime, pHead can assume a corrupt state ?

CXML MyXML;
CHeader MyHeader( &MyXML );

Would be fine. Even though MyXML is not constant, CHeader treats it as
constant. CHeader can't change it, but the original MyXML can change it.

You can go from a non constant to a constant without the need of an explicit
cast, you just can't go from constant to non-constant without an explicit
cast.
 

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
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top