473,405 Members | 2,262 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,405 software developers and data experts.

Ctor initialization problem with shared_ptr (boost)

class B;
typedef boost::shared_ptr<BBPtr;

class A
{
public:
explicit A(BPtr b) : _b1(b) {}
A(BPtr b1, BPtr b2) : _b1(b1), _b2(b2) {}

void f() {
if (_b1) {
// do some stuff with _b1
}
if (_b2) {
// do some stuff with _b2
}
}
private:
BPtr _b1;
BPtr _b2;
};

I'd like to construct an object A using the second c-tor but with _b1
and _b2 sharing the same object. To achieve that, I write

BPtr b(new B);
A a(b, b);

Now, I'd like to do the initialization from a derived class D.

class D
{
public:
D() : A(?, ?) {}
};

Is there a way to achieve this without modifying the semantic of the
base class A ?

Thank you.

Sep 7 '07 #1
2 1675
<bo****@gmail.comwrote in message
news:11**********************@y42g2000hsy.googlegr oups.com...
: class B;
: typedef boost::shared_ptr<BBPtr;
:
: class A
: {
: public:
: explicit A(BPtr b) : _b1(b) {}
: A(BPtr b1, BPtr b2) : _b1(b1), _b2(b2) {}
:
: void f() {
: if (_b1) {
: // do some stuff with _b1
: }
: if (_b2) {
: // do some stuff with _b2
: }
: }
: private:
: BPtr _b1;
: BPtr _b2;
: };
:
: I'd like to construct an object A using the second c-tor but with _b1
: and _b2 sharing the same object. To achieve that, I write
:
: BPtr b(new B);
: A a(b, b);
:
: Now, I'd like to do the initialization from a derived class D.
:
: class D
: {
: public:
: D() : A(?, ?) {}
: };
:
: Is there a way to achieve this without modifying the semantic of the
: base class A ?

Possibly with an intermediate function.
Something like:

inline A makeADoubleB(BPtr b) { return A(b,b); }

class D
{
public:
D() : A(makeADoubleB(new B)) {}
};
I hope this helps,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com

Sep 7 '07 #2
Hi Ivan,

Thank you :-). I did not think to pass through the copy c-tor of the
base class.

Finally, I found also another solution: Passing through an
intermediate class between A and D, as follows:

class C: public A
{
protected:
C(BPtr _b) : A(_b, _b) {}
};

class D : public C
{
public:
D() : C(boost::shared_ptr<B>(new B)) {}
};

This avoids using copy c-tor, which could be impossible (when A non
copyable)

Christophe
On 7 sep, 16:09, "Ivan Vecerina"
<_INVALID_use_webfo...@ivan.vecerina.comwrote:
<bou...@gmail.comwrote in message

news:11**********************@y42g2000hsy.googlegr oups.com...
: class B;
: typedef boost::shared_ptr<BBPtr;
:
: class A
: {
: public:
: explicit A(BPtr b) : _b1(b) {}
: A(BPtr b1, BPtr b2) : _b1(b1), _b2(b2) {}
:
: void f() {
: if (_b1) {
: // do some stuff with _b1
: }
: if (_b2) {
: // do some stuff with _b2
: }
: }
: private:
: BPtr _b1;
: BPtr _b2;
: };
:
: I'd like to construct an object A using the second c-tor but with _b1
: and _b2 sharing the same object. To achieve that, I write
:
: BPtr b(new B);
: A a(b, b);
:
: Now, I'd like to do the initialization from a derived class D.
:
: class D
: {
: public:
: D() : A(?, ?) {}
: };
:
: Is there a way to achieve this without modifying the semantic of the
: base class A ?

Possibly with an intermediate function.
Something like:

inline A makeADoubleB(BPtr b) { return A(b,b); }

class D
{
public:
D() : A(makeADoubleB(new B)) {}
};

I hope this helps,
Ivan
--http://ivan.vecerina.com/contact/?subject=NG_POST<- email contact form
Brainbench MVP for C++ <>http://www.brainbench.com

Sep 10 '07 #3

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

Similar topics

2
by: krema2ren | last post by:
Hi I've the following header problem that I need two classes to know each other through a boost::shared_ptr. Does any of you smart guys have a solution? A.h ---------------------- #include...
14
by: PengYu.UT | last post by:
In the following program, I want an iterator contain pointer pointing to constant object not const pointer. If it is possible would you please let me know how to do it? #include...
5
by: Boris | last post by:
I've a class C with a smart pointer (I use boost::shared_ptr) which is initialized in the constructor: class C { boost::shared_ptr<D> d; public: C() : d(new d()) { } }; When the program...
4
by: Dennis Jones | last post by:
Given the following definition: std::vector< std::pair< boost::shared_ptr<Class1>, boost::shared_ptr<Class2> > > PairsVector; What is the best way to insert objects into the vector (without...
2
by: Dennis Jones | last post by:
Hello, I have a class that will eventually look something like this: class TTableHolder { private: boost::scoped_ptr<TSessionFSession; boost::shared_ptr<TTableFTable;
1
by: limcore | last post by:
How to solve following problem: A1 A2 - two parents, they must have enable_shared_from_this | | | | \ / B - B class | C - C class
9
by: Tim H | last post by:
Why is the following code not valid? I mean, I see the code and it doesn't allow it, but I am curious about the rationale? boost::shared_ptr<intpi = new int; pi = new int; Thanks Tim
13
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
5
by: number774 | last post by:
I've used Boost for this example; in fact we have our own pointer class, for historic reasons. #include "boost\shared_ptr.hpp" // A heirarchy of classes class G1 {}; class G2: public G1 {};...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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
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...
0
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,...

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.