help with pointer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Obelix

    #1

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

    #2
    Re: help with pointer

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

    Comment

    • Gavin Deane

      #3
      Re: help with pointer

      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

      Comment

      • Obelix

        #4
        Re: help with pointer

        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

        Comment

        • Gavin Deane

          #5
          Re: help with pointer

          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

          Comment

          • Jim Langston

            #6
            Re: help with pointer

            "Obelix" <Obelix@asterix .gawrote in message
            news:euj7i9$9f8 $1@mophus.csi.i t...
            Gavin Deane ha scritto:
            >On 30 Mar, 15:03, Obelix <Obe...@asterix .gawrote:
            >
            >>
            >CHeader(cons t 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.


            Comment

            Working...