Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > upcasting with smart pointers

Reply
Thread Tools

upcasting with smart pointers

 
 
mati-006
Guest
Posts: n/a
 
      12-29-2006
Hi,
I think the code will be the best way to explain what I mean:

#include "arglib/arg_shared.h"
class base {
public:
base() {}
virtual ~base() {}
};

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

int main()
{
arg::counted_ptr<base> bsp;
arg::counted_ptr<derived> dsp(new derived);
base * bp;
derived * dp(new derived);

bp=dp;
bsp=dsp;
}

The smart pointers I'm using are documented here:
http://www.octopull.demon.co.uk/argl...unted_ptr.html

The problem:
bp=dp; - works
bsp=dsp; - doesn't work, compile error:

test.cpp: In function `int main()':
test.cpp:16: error: no match for 'operator=' in 'bsp = dsp'
arglib/arg_shared.h:312: error: candidates are:
arg::counted_ptr<pointee_type>&
arg::counted_ptr<pointee_type>:perator=(const
arg::typed_reference<pointee_type>&) [with pointee_type = base]
arglib/arg_shared.h:324: error:
arg::counted_ptr<pointee_type>&
arg::counted_ptr<pointee_type>:perator=(const
arg::counted_ptr<pointee_type>&) [with pointee_type = base]
make: *** [test.o] Error 1

Question is, what is the best way to deal with upcasting using smart
pointers? Or maybe I'm using not-so-smart pointers and the upcasting
thing is possible with properly written smart pointers?


--
mati
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      12-29-2006
mati-006 wrote:
....
>
> Question is, what is the best way to deal with upcasting using smart
> pointers? Or maybe I'm using not-so-smart pointers and the upcasting
> thing is possible with properly written smart pointers?


The "smart" pointers need to be able to deal with this. This is usually
accommodated by overloading the copy constructor and the assignment
operator with template versions.
 
Reply With Quote
 
 
 
 
JoeC
Guest
Posts: n/a
 
      12-29-2006

mati-006 wrote:
> Hi,
> I think the code will be the best way to explain what I mean:
>
> #include "arglib/arg_shared.h"
> class base {
> public:
> base() {}
> virtual ~base() {}
> };
>
> class derived : public base {
> public:
> derived() {}
> ~derived() {}
> };
>
> int main()
> {
> arg::counted_ptr<base> bsp;
> arg::counted_ptr<derived> dsp(new derived);
> base * bp;
> derived * dp(new derived);
>
> bp=dp;
> bsp=dsp;
> }
>
> The smart pointers I'm using are documented here:
> http://www.octopull.demon.co.uk/argl...unted_ptr.html
>
> The problem:
> bp=dp; - works
> bsp=dsp; - doesn't work, compile error:
>
> test.cpp: In function `int main()':
> test.cpp:16: error: no match for 'operator=' in 'bsp = dsp'
> arglib/arg_shared.h:312: error: candidates are:
> arg::counted_ptr<pointee_type>&
> arg::counted_ptr<pointee_type>:perator=(const
> arg::typed_reference<pointee_type>&) [with pointee_type = base]
> arglib/arg_shared.h:324: error:
> arg::counted_ptr<pointee_type>&
> arg::counted_ptr<pointee_type>:perator=(const
> arg::counted_ptr<pointee_type>&) [with pointee_type = base]
> make: *** [test.o] Error 1
>
> Question is, what is the best way to deal with upcasting using smart
> pointers? Or maybe I'm using not-so-smart pointers and the upcasting
> thing is possible with properly written smart pointers?
>
>
> --
> mati


Here is some work I have done...

class hunit{

unit * p;
int * cnt;
public:
hunit() : cnt(new int(1)), p(new unit) {}
hunit(char);
hunit(const hunit& u) : cnt(u.cnt), p(u.p) {++*cnt;}
hunit& operator = (const hunit&);
hunit(std::istream);
~hunit();
void move();
void attack();
void display();
};

#include "hunit.h"
#include "air.h"
#include "sea.h"

hunit::hunit(char n){

switch(n){
case 'u':
p = new unit;
break;
case 'a':
p = new air;
break;
case 's':
p = new sea;
break;
}

cnt = new int(1);

}

hunit& hunit:perator = (const hunit& h){
++*h.cnt;
if(--*cnt == 0){
delete p;
delete cnt;
}
p = h.p;
cnt = h.cnt;
return *this;
}


hunit::hunit(std::istream){

}

hunit::~hunit(){
if(--*cnt == 0){
delete p;
delete cnt;
}
}

void hunit::move(){
p->move();
}

void hunit::attack(){
p->attack();
}

void hunit::display(){
p->display();
}


class unit{
protected:
int locx;
int locy;

int atk;
int dfce;
void create();
public:
unit();
unit(std::istream);
virtual ~unit(){};
virtual unit* copy() const;
virtual void move();
virtual void attack();
virtual void display();
};


#include"unit.h"

unit::unit(std::istream in){
create();
}

unit::unit(){
create();
}

void unit::create(){
locx = 0;
locy = 0;

atk = 0;
dfce = 0;
}

void unit::move(){
std::cout<<"Moving"<<std::endl;
}


void unit::attack(){
std::cout<<"Charge! \n";
}

void unit::display(){
std::cout<<"Here I am \n";
std::cout<<locx<<" "<<locy<<"\n";
}

unit* unit::copy() const{
return new unit(*this);
}

 
Reply With Quote
 
Moonlit
Guest
Posts: n/a
 
      12-30-2006
Hi,

From my weak/strong smartptr class, I have something like this:

template<class A>
MSRefPtr<T>& operator=( const MSRefPtr<A>& SRefPtr )
{
T *TmpPtr = this->Ptr;
this->Ptr = SRefPtr.Ptr;
if( this->Ptr ) this->Ptr->IncCnt();
if( TmpPtr ) TmpPtr->DecCnt();

return *this;
}

For instance this would be valid for T as base type and A as derived (but
not the other way around).

You probaly need something similar.


Regards, Ron AF Greve

http://moonlit.xs4all.nl

"mati-006" <(E-Mail Removed)> wrote in message
news:en3kec$isn$(E-Mail Removed)...
> Hi,
> I think the code will be the best way to explain what I mean:
>
> #include "arglib/arg_shared.h"
> class base {
> public:
> base() {}
> virtual ~base() {}
> };
>
> class derived : public base {
> public:
> derived() {}
> ~derived() {}
> };
>
> int main()
> {
> arg::counted_ptr<base> bsp;
> arg::counted_ptr<derived> dsp(new derived);
> base * bp;
> derived * dp(new derived);
>
> bp=dp;
> bsp=dsp;
> }
>
> The smart pointers I'm using are documented here:
> http://www.octopull.demon.co.uk/argl...unted_ptr.html
>
> The problem:
> bp=dp; - works
> bsp=dsp; - doesn't work, compile error:
>
> test.cpp: In function `int main()':
> test.cpp:16: error: no match for 'operator=' in 'bsp = dsp'
> arglib/arg_shared.h:312: error: candidates are:
> arg::counted_ptr<pointee_type>&
> arg::counted_ptr<pointee_type>:perator=(const
> arg::typed_reference<pointee_type>&) [with pointee_type = base]
> arglib/arg_shared.h:324: error:
> arg::counted_ptr<pointee_type>&
> arg::counted_ptr<pointee_type>:perator=(const
> arg::counted_ptr<pointee_type>&) [with pointee_type = base]
> make: *** [test.o] Error 1
>
> Question is, what is the best way to deal with upcasting using smart
> pointers? Or maybe I'm using not-so-smart pointers and the upcasting
> thing is possible with properly written smart pointers?
>
>
> --
> mati



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Smart Pointers: Is there something similar to smart pointers in C? MotoK C Programming 59 09-15-2006 07:03 PM
Smart pointers and member function pointers n2xssvv g02gfr12930 C++ 3 11-27-2005 10:51 AM
Upcasting/ Downcasting in VB.NET? =?Utf-8?B?Sm9l?= ASP .Net 3 11-14-2005 08:48 PM
upcasting of baseclasspointer Patrick Kowalzick C++ 2 03-03-2004 05:29 PM
upcasting pointer-to-pointer-to-class Judith C++ 2 11-17-2003 04:21 PM



Advertisments