473,772 Members | 2,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Base and Derived Class

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?

Thanks in advance,
Michael

Aug 1 '06 #1
5 1961
"Michael" <mi*******@gmai l.comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
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?
No.

The only thing you have listed are declarations. They could be set to point
the same address space, namely an address of an object of type Derived.

Then you could call any member functions of Base from the Base point and any
members of Derived from the derived pointer.

Note: Any virtual functions called from the address contained in Base* would
run from the derived classes data.

--
LTP

:)

Aug 2 '06 #2
Michael wrote:
...
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;
No, they are not the same.
Does it mean ptr1 is the same as ptr2?
I don't understand what you mean here. Types of 'ptr1' and 'ptr2'? Values of
'ptr1' and 'ptr2'? Something else?

--
Best regards,
Andrey Tarasevich
Aug 2 '06 #3

Luc The Perverse wrote:
"Michael" <mi*******@gmai l.comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
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?

No.

The only thing you have listed are declarations. They could be set to point
the same address space, namely an address of an object of type Derived.

Then you could call any member functions of Base from the Base point and any
members of Derived from the derived pointer.

Note: Any virtual functions called from the address contained in Base* would
run from the derived classes data.

--
LTP

:)
So a Base point could point to either Base object (complete object) or
Derived object (incomplete object). A Derived point could only point to
Derived object? Right?

Why not use Base* points to Base object and Derived points to Derived
object?

Aug 2 '06 #4
"Michael" <mi*******@gmai l.comwrote in message
news:11******** *************@m 79g2000cwm.goog legroups.com
Luc The Perverse wrote:
>"Michael" <mi*******@gmai l.comwrote in message
news:11******* *************** @i42g2000cwa.go oglegroups.com. ..
>>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?

No.

The only thing you have listed are declarations. They could be set
to point the same address space, namely an address of an object of
type Derived.

Then you could call any member functions of Base from the Base point
and any members of Derived from the derived pointer.

Note: Any virtual functions called from the address contained in
Base* would run from the derived classes data.

--
LTP

:)

So a Base point could point to either Base object (complete object) or
Derived object (incomplete object). A Derived point could only point
to Derived object? Right?

Why not use Base* points to Base object and Derived points to Derived
object?
Suppose you want to store pointers to Base objects and pointers to Derived
objects in the same container --- or, more likely, pointers to Derived1
objects and pointers to Derived2 objects. How would you do it, given that
the container will only store one type of pointer? Answer: by making the
container store base Base pointers.
--
John Carson

Aug 2 '06 #5
Michael wrote:
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?

Thanks in advance,
Michael
You can assign a Derived* to ptr1, but you cannot assign a Base* to ptr2.

Aug 2 '06 #6

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

Similar topics

2
2040
by: Gabriel Genellina | last post by:
Hi In the following code sample, I have: - a Worker class, which could have a lot of methods and attributes. In particular, it has a 'bar' attribute. This class can be modified as needed. - a Base class (old-style) which defines a default class attribute 'bar' too. I can't modify the source code of this class. Note that Workes does not inherit from Base. - a Derived class which must inherit from Base and is a wrapper around Worker: it...
9
4806
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I am using vector class(STL), the compiler does not allow me to do this. I do realize there is a pitfall in this approach(size of arrays not matching etc), but I wonder how to get around this problem. I have a class hierachy with abstract base...
7
6625
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
5
3164
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits CommonPageBase - Contains myPage which inherits PageBase Each of these classes overrides OnInit and ties an event handler
4
5247
by: Jeff | last post by:
The derived class below passes a reference to an object in its own class to its base calss constructor. The code compiles and will run successfully as long as the base class constructor does not attempt to access the object -- since m_object is not actually created and initizialized until after the base constructor has been called. Any thoughts on the practice below? class Base { public:
6
2501
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
5
2625
by: Dennis Jones | last post by:
Hello, I have a couple of classes that look something like this: class RecordBase { }; class RecordDerived : public RecordBase {
15
3086
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
2
2413
by: cmonthenet | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual static function (which I know is illegal), but, is there a way to provide something similar? The class that is the target of my inquiry is a template class that interfaces to one of several derived classes through a pointer to a base class. The...
5
1494
by: alexl | last post by:
I have 2 classes, class base { public: some virtual functions } class derived : public base { public:
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
7461
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5355
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4009
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
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2851
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.