what is the type of this pointer and also object?
-
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 }
-
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 }
-
Since
p
is declared as a type ofA
then the object returned fromnew
will effectively be downcast to a typeA
object, so you will not be able to use any methods or properties unique to classB
.I must get a clever new signature for 2011.
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] -
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 }
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
-
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
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]
-
Since
p
is declared as a type ofA
then the object returned fromnew
will effectively be downcast to a typeA
object, so you will not be able to use any methods or properties unique to classB
.I must get a clever new signature for 2011.
downcast or upcast? A is at higher level in hierarchy.
-
downcast or upcast? A is at higher level in hierarchy.
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.