473,395 Members | 2,079 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,395 software developers and data experts.

help with pointer

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.
Mar 30 '07 #1
5 1501
Obelix wrote:
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) {};
>

It's right ?
But, if I would like to not declare pXml as const.... Have someone any
idea ?
Mar 30 '07 #2
On 30 Mar, 15:03, Obelix <Obe...@asterix.gawrote:
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

Mar 30 '07 #3
Gavin Deane ha scritto:
On 30 Mar, 15:03, Obelix <Obe...@asterix.gawrote:
>
CHeader(const CXML *pHead) : pXml(pHead) {};
ok. an digit error

>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.
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
Mar 30 '07 #4
On 30 Mar, 15:54, Obelix <Obe...@asterix.gawrote:
GavinDeaneha scritto:
On 30 Mar, 15:03, Obelix <Obe...@asterix.gawrote:
CHeader(const CXML *pHead) : pXml(pHead) {};

ok. an digit error
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.

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

Mar 30 '07 #5
"Obelix" <Ob****@asterix.gawrote in message
news:eu**********@mophus.csi.it...
Gavin Deane ha scritto:
>On 30 Mar, 15:03, Obelix <Obe...@asterix.gawrote:
>>
CHeader(const CXML *pHead) : pXml(pHead) {};

ok. an digit error

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

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.
Mar 30 '07 #6

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

Similar topics

3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
5
by: ali | last post by:
Hi, I'm trying to understand the reason for different output on the following codes Code1: #include <iostream.h> int main()
2
by: dinks | last post by:
Hi, I'm new to C++ and have been assigned a task which i dont completely understand. Any help would be greately appreciated. Here is the problem: The class "linkedListType" use the "assert"...
5
by: jhon02148 | last post by:
hi this hw have four files: 1. for the main program 2. listp.cpp (the source file) 3. listp.h (the header file) 4. exception.h if there is anybody who could help me with this hw i really...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
2
by: leo2100 | last post by:
Hi, I need help with this program. The program is supposed to take a text file and identify the words in it, then it should print them and count how many times a word is repeated. At first main...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
6
by: sandy | last post by:
I am creating a class (or so I hope) which is to be a JobCollection, linked list of 'Job'. Job is another class already created. JobCollection is just the linked list manager. I have to have...
0
by: Pucca | last post by:
Hi I'm using vs2005. I am getting a bunch of compiler warnings after I made some changes to my code that was compiling clean. I'm also getting memory errors when I run my program and it's...
3
by: Chris AtLee | last post by:
Sorry for the repeat post...I'm not sure if my first post (on May 30th) went through or not. I've been trying to write a PAM module using ctypes. In the conversation function (my_conv in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.