473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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_ptr < Serializable > (new Bar());
}
}

The client would call the function like this:

boost::shared_ptr < Foo > foo;
XmlSerializer s;
s.serializeObject(foo);

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

test.cpp(72) : error C2664: 'serializeObject' : cannot convert
parameter 1 from 'class boost::shared_ptr<class Foo>' to 'class
boost::shared_ptr<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_ptr < Serializable > p serializeObject()
{
boost::shared_ptr < Serializable > ret;
ret = boost::shared_ptr < Serializable > (new Bar());
return ret;
}
}

And call it like this:

foo = boost::dynamic_pointer_cast< Foo >(s.serializeObject());

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 1373

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

Similar topics

1
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...
3
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> ...
4
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) { }...
2
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...
20
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 ...
1
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...
18
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...
11
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...
0
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 -...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.