473,543 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 16220
yp*********@ind iatimes.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
12072
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 member function?
7
3040
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
2061
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
1944
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
3055
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: 'base::~base' : cannot access protected member declared in
9
3453
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
3572
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 derived type 3 - this function then 'grows' the derived type object (by pushing onto a vector).
10
4051
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 allowed to access the protected data members of the base object. This surprises me. Can someone explain why this is? I suspect there is a good...
3
1401
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
7412
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7697
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4899
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3394
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1830
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
979
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
648
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.