Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. what is the type of this pointer and also object?

what is the type of this pointer and also object?

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aesthetic crazy
    wrote on last edited by
    #1

    Hi, when i write the following, kindly guide me that the type of pointer *p and also the type(A or B) of object made in the same line (line 3). Class A {} Class B : public A {} main () { A *p = new B; // line 3 }

    L A 2 Replies Last reply
    0
    • A aesthetic crazy

      Hi, when i write the following, kindly guide me that the type of pointer *p and also the type(A or B) of object made in the same line (line 3). Class A {} Class B : public A {} main () { A *p = new B; // line 3 }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Since p is declared as a type of A then the object returned from new will effectively be downcast to a type A object, so you will not be able to use any methods or properties unique to class B.

      I must get a clever new signature for 2011.

      CPalliniC A 2 Replies Last reply
      0
      • L Lost User

        Since p is declared as a type of A then the object returned from new will effectively be downcast to a type A object, so you will not be able to use any methods or properties unique to class B.

        I must get a clever new signature for 2011.

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        However, he still could exploit polymorphism... :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • A aesthetic crazy

          Hi, when i write the following, kindly guide me that the type of pointer *p and also the type(A or B) of object made in the same line (line 3). Class A {} Class B : public A {} main () { A *p = new B; // line 3 }

          A Offline
          A Offline
          Aescleal
          wrote on last edited by
          #4

          The type of the pointer is determined by the left hand side of the expression. The type of the created object is determined by the right hand side of the expression. One interesting thing about C++ and C (to a lesser extent) is that when you declare a pointer you're not saying "this variable is a pointer to fixed class" but you're saying "this variable is a pointer to something with an interface of this type." In c this really only shows up when you convert to and from void * but in C++ it crops up a lot more (it's how C++ implements substitutability, AKA the Liskov Principle). From this lot, when you write the expression:

          A *p = new B;

          you're saying two things to the compiler "p is a pointer to something with the interface of A" and "initialise that pointer with the address of a dynamically allocated B." Cheers, Ash

          C 1 Reply Last reply
          0
          • A Aescleal

            The type of the pointer is determined by the left hand side of the expression. The type of the created object is determined by the right hand side of the expression. One interesting thing about C++ and C (to a lesser extent) is that when you declare a pointer you're not saying "this variable is a pointer to fixed class" but you're saying "this variable is a pointer to something with an interface of this type." In c this really only shows up when you convert to and from void * but in C++ it crops up a lot more (it's how C++ implements substitutability, AKA the Liskov Principle). From this lot, when you write the expression:

            A *p = new B;

            you're saying two things to the compiler "p is a pointer to something with the interface of A" and "initialise that pointer with the address of a dynamically allocated B." Cheers, Ash

            C Offline
            C Offline
            Chris Meech
            wrote on last edited by
            #5

            Some answers should have a "Really Good Answer" button. :)

            Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

            1 Reply Last reply
            0
            • L Lost User

              Since p is declared as a type of A then the object returned from new will effectively be downcast to a type A object, so you will not be able to use any methods or properties unique to class B.

              I must get a clever new signature for 2011.

              A Offline
              A Offline
              aesthetic crazy
              wrote on last edited by
              #6

              downcast or upcast? A is at higher level in hierarchy.

              L 1 Reply Last reply
              0
              • A aesthetic crazy

                downcast or upcast? A is at higher level in hierarchy.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                aesthetic.crazy wrote:

                downcast or upcast?

                You are casting from a derived class to a base class, and in English base is considered the lowest level, thus downcasting. I agree it's difficult because most diagrams give the impression that A (the base class) is at the top of the hierarchy.

                I must get a clever new signature for 2011.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups