473,586 Members | 2,754 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how do i declare a reference to a boost shared pointer

Can I declare a pure virtual member function which accepts as input a
boost shared pointer to an object of a base class, such that
- concrete implementations of the function can redirect the pointer to
a new object
- clients of the function can pass in as input a boost shared pointer
to an object of a derived class
?

Consider the following classes in the problem domain:

class Serializable {};
class Foo : public Serializable {};
class Bar : public Foo {};

Here is the pure virtual member function I would like to declare:

class Serializer {
public:
virtual void serializeObject (boost::shared_ ptr < Serializable >
&) = 0;
}

And the relevant subset of one of its concrete implementations :

class XmlReader : public Serializer {
virtual void serializeObject (boost::shared_ ptr < Serializable >
&p) {
p = boost::shared_p tr < Serializable > (new Bar());
}
}

The client would call the function like this:

boost::shared_p tr < Foo > foo;
XmlSerializer s;
s.serializeObje ct(foo);

The above code doesn't compile, the error message from VC6 is

test.cpp(72) : error C2664: 'serializeObjec t' : cannot convert
parameter 1 from 'class boost::shared_p tr<class Foo>' to 'class
boost::shared_p tr<class Serializable> &'
A reference that is not to 'const' cannot be bound to a
non-lvalue

The closest I can get is to declare the function like this:

class XmlReader : public Serializer {
virtual boost::shared_p tr < Serializable > p serializeObject ()
{
boost::shared_p tr < Serializable > ret;
ret = boost::shared_p tr < Serializable > (new Bar());
return ret;
}
}

And call it like this:

foo = boost::dynamic_ pointer_cast< Foo >(s.serializeOb ject());

But that's not ideal for my purposes, I would prefer to have a function
which modifies its inputs and returns void.

I can't implement the function as a template because I need it to be
virtual.

Any assistance gratefully received.

Regards,
Eric

Nov 26 '05 #1
0 1387

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5900
by: James Mastro | last post by:
This one has been driving me nuts all day. In a function I have the following: std::list< boost::shared_ptr<MyObject> > * objectList; When debugging my code, the debugger (gdb) dies upon entering the function with this in it. It says "The Debugger has exited due to signal 11 (SIGSEGV).The Debugger has exited due to signal 11...
3
16328
by: yinglcs | last post by:
I am reading the Boost scoped_ptr library, and I wonder what is the advantage of using that. Here is an example from Boost.org web site: #include <boost/scoped_ptr.hpp> #include <iostream> struct Shoe { ~Shoe() { std::cout << "Buckle my shoe\n"; } };
4
4185
by: aaronfude | last post by:
Hi, Please consider the following class (it's not really my class, but it's a good example for my question): class Vector { int myN; double *myX; Vector(int n) : myN(n), myX(new double) { } double &operator()(int i) { return myX; }
2
3803
by: Rafał Maj Raf256 | last post by:
How can I manually (other then http://www.boost.org/libs/smart_ptr/enable_shared_from_this.html) create a shared, and a weak pointer to "this"? Of course I dont want the shared / weak pointer to cause object to be deleted when shared/weak ptr will go out of scope. -- Wymiana starych układów... na nowe układy - prawie jak walka z...
20
1657
by: Protoman | last post by:
OK, this code compiles, links, and executes, but, how do I setup, like, a spinlock to query the DataBase object's status to let the SmrtPtr know that the object's been deleted?: #pragma once class SmrtPtrDB { public: SmrtPtrDB():num(0){}
1
2763
by: ank | last post by:
Hi, all. I've come to think of the idea of automatic initialization/deinitialization of non-local reference count pointer. I've made an assumption that the user of the pointer only read pointer after acquire the reference (increment the ref count) and when finished using it, the user will release the ref count.
18
9063
by: happyvalley | last post by:
Hi, basically, the test function get a char pointer, and assigned a string to it. then the string is passed back by the call-by-reference mechanism. in test(), I reallocate some memory for the pointer, the size is not fixed. I remember, all new statement should be followed by a delete statement, is there some memory leak here?
11
2766
by: Dennis Jones | last post by:
Hello, I would like to know if there is a known pattern for creating a map of reference-counted objects, such that when the last reference to an object is deleted, the element referring to that object is removed from the map. Since Boost's shared_ptr class allows a custom deleter to be specified, I was thinking about something like this:
0
1571
by: phlip | last post by:
Nick Keighley wrote: CC'd to the correct newsgroup. Yes, the destructor of the shared pointer will delete the object. Then its former address will convert to a reference. At some point - possibly the conversion - Undefined Behavior will begin. And then when whoever collects the reference uses it, Undefined Behavior will continue.
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7954
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...
0
8215
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3836
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...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
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
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
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...

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.