473,686 Members | 2,101 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

upcasting with smart pointers

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_pt r<basebsp;
arg::counted_pt r<deriveddsp(ne w 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:31 2: error: candidates are:
arg::counted_pt r<pointee_type> &
arg::counted_pt r<pointee_type> ::operator=(con st
arg::typed_refe rence<pointee_t ype>&) [with pointee_type = base]
arglib/arg_shared.h:32 4: error:
arg::counted_pt r<pointee_type> &
arg::counted_pt r<pointee_type> ::operator=(con st
arg::counted_pt r<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
3 2848
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

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_pt r<basebsp;
arg::counted_pt r<deriveddsp(ne w 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:31 2: error: candidates are:
arg::counted_pt r<pointee_type> &
arg::counted_pt r<pointee_type> ::operator=(con st
arg::typed_refe rence<pointee_t ype>&) [with pointee_type = base]
arglib/arg_shared.h:32 4: error:
arg::counted_pt r<pointee_type> &
arg::counted_pt r<pointee_type> ::operator=(con st
arg::counted_pt r<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::istr eam);
~hunit();
void move();
void attack();
void display();
};

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

hunit::hunit(ch ar 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(st d::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::istre am);
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<<"Mov ing"<<std::endl ;
}
void unit::attack(){
std::cout<<"Cha rge! \n";
}

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

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

Dec 29 '06 #3
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********@gaz eta.NOSPAM.plwr ote 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_pt r<basebsp;
arg::counted_pt r<deriveddsp(ne w 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:31 2: error: candidates are:
arg::counted_pt r<pointee_type> &
arg::counted_pt r<pointee_type> ::operator=(con st
arg::typed_refe rence<pointee_t ype>&) [with pointee_type = base]
arglib/arg_shared.h:32 4: error:
arg::counted_pt r<pointee_type> &
arg::counted_pt r<pointee_type> ::operator=(con st
arg::counted_pt r<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2140
by: christopher diggins | last post by:
I would like to survey how widespread the usage of smart pointers in C++ code is today. Any anecdotal experience about the frequency of usage of smart pointer for dynamic allocation in your own code or other people's code you have come across would be appreciated. I am also trying to identify the likelihood nad frequency of scenarios where smart pointer solutions would not be appropriate, i.e. for some reason such as performance or...
27
3397
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined as ff: typedef struct { float *data ; int count ;
8
5142
by: Axter | last post by:
I normally use a program call Doxygen to document my source code.(http://www.stack.nl/~dimitri/doxygen) This method works great for small and medium size projects, and you can get good documentation like the following: http://axter.com/smartptr Now I'm on a client site, and I'm trying to create the same type of documentation on a very large project. I ran the Doxygen program, and it ran for over 16 hours, before I had
92
5078
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers, but I just don't know. I don't like them because I don't trust them. I use new and delete on pure pointers instead. Do you use smart pointers?
33
5056
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the actual counting. Here is the latter's definition: // --- Begin ReferenceCountable.h ---------- class ReferenceCountable
54
11973
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining the advantages of smart pointers endlessly (which are currently used in all our C++ software; we use the Boost smart pointers) as I'm seriously concerned that there is a shift to raw pointers. We are not developing system software but rather...
4
2483
by: majsta | last post by:
Hello, I have the following code. #include <vector> #include <iostream> class Foo { public: Foo(){} virtual void print() const { std::cout << "foo" << std::endl;} };
0
8585
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8770
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,...
0
7603
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6441
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
4309
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
4534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2947
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
2208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1940
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.