472,791 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,791 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 16090
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"; } };
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.