473,387 Members | 1,423 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,387 software developers and data experts.

why can't derived class pointer can't point to base class object

Dear All,
According to OOPs , a base class pointer can to point to derived class
object....call this as fact1
But somehow I am not comfortable while understanding this concept.
The explanaition to the fact1 is given as since the derived object
always consists of the base part , the base class pointer will always
point to the base part in the derived object unless otherwise the
function in the base class are declared as virtual and are overrided in
the derived class.
My confusion is, since the derived class always consists of the base
class part in it,it should be more logical to think that the pointer to
the derived class can store the address of the base class object as it
will be always carrying the base class related services and data with
it.
This confusion gives me lot of headech while studying the COM and other
advanced concept . will someone please give me any good explaination or
justification of where my thinking process is going wrong(straying??)?.
I will be very grateful to him..

regds,
Yogesh Joshi

Jul 22 '05 #1
1 16200
yp*********@indiatimes.com wrote:
Dear All,
According to OOPs , a base class pointer can to point to derived class
object....call this as fact1
But somehow I am not comfortable while understanding this concept.
The explanaition to the fact1 is given as since the derived object
always consists of the base part , the base class pointer will always
point to the base part in the derived object unless otherwise the
function in the base class are declared as virtual and are overrided in
the derived class.
My confusion is, since the derived class always consists of the base
class part in it,it should be more logical to think that the pointer to
the derived class can store the address of the base class object as it
will be always carrying the base class related services and data with
it.
This confusion gives me lot of headech while studying the COM and other
advanced concept . will someone please give me any good explaination or
justification of where my thinking process is going wrong(straying??)?.
I will be very grateful to him..


class A
{
public:
void f();
};

class B : public A
{
public:
void g();
};
Now, objects of type A can call f() and objects of type B can call
either f() or g().
int main()
{
A a;
B b;

a.f(); // ok
a.g(); // error

b.f(); // ok
b.g(); //ok

A *pa = &b; // ok
B *pb = &a; // that's illegal, but what if

pa->f(); // ok
pa->g(); // error

pb->f(); // ok
pb->g(); // ok !!! pb actually points to an A and As don't
// have g() !
}

By having pb point to a A, the compiler accepted the call to g(), even
if the object we are pointing at (actually a A) does not contain such
member function. That's because the function calls are checked on the
static type of the object (which is A*), not on the dynamic type (which
is B*).

You can implicitly cast pointers to a base class, because the compiler
is 100% sure the object pointed to *is a* base class (it is perhaps more
than that, such as a B or a C, but it is at least a base class). You
can not implicitly cast pointers to a derived class, because the
compiler cannot check if the object *is a* derived class.

void f(B *a)
{
// does a really point to a B?
}

void g1()
{
A a;
f(&a); // no it does not, so that must be illegal
}

void g2()
{
B b;
f(&b); // yes it does, so that's legal
}

There are (unfortunately) some circumstances where *you* know for sure
an A* is a B*, so you are able to do the cast explicitly. Using
dynamic_cast over static_cast can make things a bit more safer.
Jonathan
Jul 22 '05 #2

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

Similar topics

3
by: Roy Yao | last post by:
Hello, I need to pass a pointer to a callback function to the lower level modules. But the function is thought to be a virtual member one. How can I get the real address of the virtual...
7
by: Sean J. Fraley | last post by:
This code illustrates what I'm confused about: template<typename T> class foo { public: template<typename U> void fooFunction(const foo<U>& x) {
9
by: jon wayne | last post by:
OK! I had this nagging doubt Consider (without worrying abt access specifiers) class Kid : public Parent{...}; Parent::someFunc() { Kid k; }
5
by: Michael | last post by:
Hi, Could you tell me whether the following two statement are the same? Derived class is derived from Base class. 1. Base* ptr1; 2. Derived * ptr2; Does it mean ptr1 is the same as ptr2?
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
10
by: Dom Jackson | last post by:
I have a program which crashes when: 1 - I use static_cast to turn a base type pointer into a pointer to a derived type 2 - I use this new pointer to call a function in an object of the...
10
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not...
3
by: Pranav | last post by:
include <iostream> #include <unistd.h> #include <stdlib.h> class base{ public : virtual void disp(){ std::cout << "Base Class\n";} void show(){ std::cout << "Base Show\n"; } };
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.