upcasting with smart pointers

Discussion in 'C++' started by mati-006, Dec 29, 2006.

  1. mati-006

    mati-006 Guest

    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/arglib/class_arg__counted_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>::eek: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>::eek: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
    mati-006, Dec 29, 2006
    #1
    1. Advertising

  2. 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.
    Gianni Mariani, Dec 29, 2006
    #2
    1. Advertising

  3. mati-006

    JoeC Guest

    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/arglib/class_arg__counted_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>::eek: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>::eek: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::eek: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);
    }
    JoeC, Dec 29, 2006
    #3
  4. mati-006

    Moonlit Guest

    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" <> wrote in message
    news:en3kec$isn$...
    > 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/arglib/class_arg__counted_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>::eek: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>::eek: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
    Moonlit, Dec 30, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    Upcasting/ Downcasting in VB.NET?

    =?Utf-8?B?Sm9l?=, Nov 14, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    4,799
    John Murray
    Nov 14, 2005
  2. Judith
    Replies:
    2
    Views:
    360
    tom_usenet
    Nov 17, 2003
  3. Patrick Kowalzick

    upcasting of baseclasspointer

    Patrick Kowalzick, Mar 2, 2004, in forum: C++
    Replies:
    2
    Views:
    2,744
    Patrick Kowalzick
    Mar 3, 2004
  4. MotoK
    Replies:
    59
    Views:
    1,792
    Keith Thompson
    Sep 15, 2006
  5. n2xssvv g02gfr12930

    Smart pointers and member function pointers

    n2xssvv g02gfr12930, Nov 26, 2005, in forum: C++
    Replies:
    3
    Views:
    463
    n2xssvv g02gfr12930
    Nov 27, 2005
Loading...

Share This Page