ask a small question!!

F

foodhigh

can someone explain to me?i feel puzzled
1, CBase * pba = new CDerived;
2, CBase * pbb = new CBase;

CBase is Base class and CDerived is Derived class,but what is the
difference between 1 and 2?

thanks a lot!
 
D

Douglas Dude

can someone explain to me?i feel puzzled
1, CBase * pba = new CDerived;
2, CBase * pbb = new CBase;

CBase is Base class and CDerived is Derived class,but what is the
difference between 1 and 2?

thanks a lot!

the differences can be found on many books of C++
I don't remember, but I use both based on what each can and can't do

You sure can write a progrm to test, something simple as follows

class base{
public:
void show(){cout<<"bbbbbb\n";};
};

class dbase:public base{
public:
void show(){cout<<"dddbbb\n";}
};

int main(){
base* pb=new base();
base*db=new dbase();
pb->show();
db->show();
delete pb;
delete db;
return 0;
}

there are of course many other points depending upon construction of
functions, polymorphism, including speed increase, interator concepts,
type relations in inheritance hierarchy in which such base pointers
might become useful with their own meanings etc
 
E

eriwik

can someone explain to me?i feel puzzled
1, CBase * pba = new CDerived;
2, CBase * pbb = new CBase;

CBase is Base class and CDerived is Derived class,but what is the
difference between 1 and 2?

The differance is that 1 creates a new CDerived object and 2 created a
new CBase object. This can be very useful together with virtual
functions:

#include <iostream>

struct base {
virtual void print();
}

struct derived : public base {
virtual void print();
}

void base::print() {
std::cout << "base\n";
}

void derived::print() {
std::cout << "derived\n";
}

int main() {
base* b = new base();
base* d = new derived();

b->print();
d->print();
}

Output:
base
derived

So, by using virtual funtions and base-pointers (works with references
too) we can use a derived object just like a base object (for example
pass it to functions like void foo(base*) ) but it will still act like
a derived object. So you can modify the behaviour of a function in the
derived class but still use it in all the places where a base could be
used. This is polymorphism.
 
R

Ron Natalie

Douglas said:
there are of course many other points depending upon construction of
functions, polymorphism, including speed increase, interator concepts,
type relations in inheritance hierarchy in which such base pointers
might become useful with their own meanings etc

They might also do well to poitn out your example program causes
undefined behavior as the base class does not have a virtual
destructor and you destroy the derived object through the base
pointer.
 
F

foodhigh

The differance is that 1 creates a new CDerived object and 2 created a
new CBase object. This can be very useful together with virtual
functions:

#include <iostream>

struct base {
virtual void print();
}

struct derived : public base {
virtual void print();
}

void base::print() {
std::cout << "base\n";
}

void derived::print() {
std::cout << "derived\n";
}

int main() {
base* b = new base();
base* d = new derived();

b->print();
d->print();
}

Output:
base
derived

So, by using virtual funtions and base-pointers (works with references
too) we can use a derived object just like a base object (for example
pass it to functions like void foo(base*) ) but it will still act like
a derived object. So you can modify the behaviour of a function in the
derived class but still use it in all the places where a base could be
used. This is polymorphism.

thanks i think i understand a little,you mean even we define like that:
derived* d=new derived()
but we can still use dynamic_cast<base*>d
so we can make the same result??
is it right?
 
R

red floyd

thanks i think i understand a little,you mean even we define like that:
derived* d=new derived()
but we can still use dynamic_cast<base*>d
so we can make the same result??
is it right?

There's an implicit cast from derived to base. Going from a derived
class to a base class is called an "upcast". You don't need a cast
operator for upcasting. Going the other way (base to derived) is a
downcast. You need either static_cast or (better) reinterpret_cast to
downcast.

class base
{
public:
virtual ~base() { }
};

class derived : public base
{
};

derived *pd = new derived;
base *pb = pd;
pd = dynamic_cast<derived*>(pb);
 
I

Ivan Novick

thanks i think i understand a little,you mean even we define like that:
derived* d=new derived()
but we can still use dynamic_cast<base*>d
so we can make the same result??
is it right?

Ok. The main point is that you can have pointers to base class objects
and call a virtual function on the pointers and at run-time the
function for either the base or derived class will be called depending
on if the pointer actually points to a base or derived object. This is
called polymorphism.

As far as dynamic_cast, the purpose is to convert a base pointer (or
reference) to a derived pointer and at runtime to check that your
pointer really does point to a derived object. If it doesn't then the
conversion will return a 0 pointer (or throw exception for reference)
and prevent you from trying to call a derived object function on an
object that really is not a derived object.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

There's an implicit cast from derived to base. Going from a derived
class to a base class is called an "upcast". You don't need a cast
operator for upcasting. Going the other way (base to derived) is a
downcast. You need either static_cast or (better) reinterpret_cast to
downcast.

No, reinterpret_cast should not be used if the types you are converting
between are related, for those things you have static_cast and
dynamic_cast, but not reinterpret_cast.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,172
Latest member
NFTPRrAgenncy
Top