By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,436 Members | 1,344 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,436 IT Pros & Developers. It's quick & easy.

upcasting with smart pointers

P: n/a
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<basebsp;
arg::counted_ptr<deriveddsp(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>::operator=(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>::operator=(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
Dec 29 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.
Dec 29 '06 #2

P: n/a

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<basebsp;
arg::counted_ptr<deriveddsp(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>::operator=(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>::operator=(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::operator = (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);
}

Dec 29 '06 #3

P: n/a
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" <lo********@gazeta.NOSPAM.plwrote in message
news:en**********@inews.gazeta.pl...
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<basebsp;
arg::counted_ptr<deriveddsp(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>::operator=(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>::operator=(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

Dec 30 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.