Smart pointer class implementing shared ownership
0
votes
1answer
18 views
CQtCreator using MinGW cannot find std::shared_pointer
I just installed QtCreator (using MinGW) on Ubuntu RR, from the Ubuntu Software Center. I have some code that uses features from C++11, specifically:
Strongly typed enums - i.e.: enum class Color ...
6
votes
4answers
257 views
c++11 - Ownership and getters
I'm new to C++ and I have troubles wrapping my head around ownership, specifically with a getter. Here's some example code:
class GameObject {
public:
Transform *transform();
private:
Transform ...
1
vote
2answers
34 views
boost::shared_ptr vs std::tr1::shared_ptr on multi os compilation
I have maintained different code for browser plugin(c++) for windows and mac system. The difference of the code is only for shared pointer.
In windows version I am using std::tr1::shared_ptr and on ...
1
vote
2answers
49 views
Setting std::shared_ptr source at a later stage
I'm just starting out with C++ and trying to get my head around smart pointers. Obviously, the following code will crash (I suppose it's because the assignment created a copy of the shared_ptr?). Is ...
1
vote
0answers
49 views
boost::shared_ptr and Return Type Resolver idiom
I am currently working on a concept of Object known in Java or C# for C++. It would be similar to variant type like boost::any, however having wider functionality. For that purpose I am using ...
-1
votes
1answer
45 views
replacing scoped_array with non-contiguous memory in C++
I have a C++ class which is something like
class Block {
...
scoped_array<Columns> columns_
...
}
Now as i understand because of using an array, the memory for Columns array will be ...
0
votes
2answers
38 views
Using std::queue with shared_ptr?
Consider the following bit of code:
#include <queue>
#include <memory>
std::shared_ptr<char> oneSharedPtr(new char[100]);
std::queue<std::shared_ptr<char>> ...
1
vote
2answers
76 views
c++ vector of boost::shared_ptr
I just started learning boost shared pointers.
I wrote a short program, results look good but I'm not sure if memory is deallocating well with my code. I would like to ask, if someone could look at ...
2
votes
1answer
205 views
C++11 Composite Pattern with Smart Pointers
I am working on a personal project to familiarize myself with C++11 and Boost.
I have a inheritance relationship with a UrlExtractor base class, with a TagUrlExtractor derived class and a ...
2
votes
3answers
52 views
Boost shared_ptr passing a derived class
So I have something like so
class baseclass {
....
}
class derived : public baseclass {
...
}
void func(boost::shared_ptr<baseclass>& a){
//stuff
}
boost::shared_ptr<derived> foo;
...
0
votes
1answer
38 views
Compiler-specific error: can't match function with const arguments
I'm pretty new to C++, so I'm trying to figure out exactly what's going on here. I'm trying to make (someone else's) code compile. It runs fine using mingw, but I'm also crosscompiling onto an ...
1
vote
1answer
36 views
Why is a shared pointer used for the string if the function call results in pass by reference?
In this boost async udp server example:
http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/tutorial/tutdaytime6/src.html
boost::shared_ptr<std::string> message(
new ...
0
votes
1answer
51 views
how to compile c++ program using boost/shared_ptr.hpp on linux with g++
I have written a program using boost shared_ptr header file. How to compile this program? what library i mention with -l option of g++ while compiling.
1
vote
1answer
44 views
Boost Python Runtime error when passing object of derived type from python to C++ function expecting a shared_ptr to base type
I have a function that takes a std::shared_ptr, and I want to pass an object of Derived type to this function from python. Here's my class definitions:
struct AbstractBase {
virtual void foo() = ...
3
votes
2answers
205 views
What is the return value of shared_ptr<T>::get() after it is released?
After releasing a std::shared_ptr<T>, when I do ptr.get() is the return value NULL or nullptr? In order to compare, I used this:
std::shared_ptr<int> ptr(new int(44));
ptr.reset();
int ...
5
votes
1answer
102 views
Why does C++ shared pointer not behave like standard pointer for iterators?
I'm about to make a random number generator in C++ and in order to avoid copying too big vectors, I wanted to pass pointers to them. I don't want to take care of garbage collection myself. That's why ...
0
votes
1answer
68 views
Using arbitrary owning pointer without templating
I would like to pass (shared) ownership of an object to a function foo::bar.
The thing is I do not care, whether the ownership is exclusive or shared.
I see class foo as an interface where I do not ...
1
vote
2answers
45 views
how does passing shared pointer by value and accepting as base class argument work?
in this program :http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/example/chat/chat_server.cpp
class chat_session
: public chat_participant,
chat_session inherits chat_participant
in one ...
0
votes
2answers
44 views
why doesnt new_session shared_ptr not get destroyed here?
from http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/example/chat/chat_server.cpp
typedef boost::shared_ptr<chat_session> chat_session_ptr;
void start_accept()
{
...
3
votes
2answers
76 views
In gdb, I can call some class functions, but others “cannot be resolved”. Why?
I have not worked on shared pointers yet .. I just know the concept. I'm trying to debug functions in the following c++ class, which stores data of an XML file (read-in via the xerces library).
// ...
6
votes
1answer
154 views
make_shared and emplace functions
I was trying to find some easy way to emplace elements in a std::vector<std::shared_ptr<int>> but couldn't come with anything. std::shared_ptr takes pointers as parameters, so I can still ...
2
votes
1answer
35 views
C++/CLI “could not import member” warning when using shared_ptr as argument
I have the following interface in C++/CLI:
public interface class ISharedPtrInterface
{
void PrintSharedPtr(std::shared_ptr<std::wstring> ptr);
};
Which is implemented as follows:
public ...
1
vote
1answer
126 views
Using std::shared_ptr to share data between producer/consumer threads
I am trying to use std::shared_ptr to point to the data being produced by one thread and consumed by another. The storage field is a shared pointer to the base class,
Here's the simplest Google Test ...
2
votes
4answers
69 views
Forward declarations and shared_ptr
I'm trying to refactor my code so that I use forward declarations instead of including lots of headers. I'm new to this and have a question regarding boost::shared_ptr.
Say I have the following ...
2
votes
1answer
147 views
Is it possible to downcast shared_ptr without copy?
#include <memory>
struct a {};
struct b : public a {};
std::shared_ptr<b> get()
{
std::shared_ptr<a> temp(new b);
return std::static_pointer_cast<b>(temp); // atomic ...
-3
votes
2answers
89 views
Polymorphism with smart pointers?
I've searched SO a bit but couldn't find anything that answers correctly my problem (I've read this, this and this )
I'm currently trying to use smart pointers with polymorphism.
When I try to ...
-1
votes
1answer
100 views
c++ vector of shared pointer. If casted outside of vector, will it change the pointer in vector?
I have a base class Base and 2 derived classes Child_A and Child_B.
By the time an object My_Object is instantiated (as a shared pointer), I don't know it's Child_A or Child_B. So it is instantiated ...
-2
votes
0answers
69 views
Destructor is not called [closed]
I have the following scheme in my application (simplified):
class CLauncher_Base
{
public:
CLauncher_Base() {}
virtual ~CLauncher_Base() = 0 {};
};
class CLauncher :
public ...
0
votes
1answer
73 views
sort on a vector of pointers changes data in a copy of the vector?
I have a vector of pointers to objects, and at some point, making a second vector with sub-elements of that vector. Now, sorting the original vector changes the elements in the second vector (there ...
0
votes
1answer
43 views
Boost shared pointer “runtime error” after it gets end of scope
I am practicing with boost and now I am testing boost shared pointers. I have a Util class which can read files. After I read the file, my "Read" method gives back a boost::shared_ptr which points to ...
1
vote
3answers
74 views
Confirmation of thread safety with std::unique_ptr/std::shared_ptr
My application has an IRC module that essentially is a normal client. Since this is heavily threaded, I stand the risk of a plug-in retrieving, for example, a users nickname - it is valid at the time, ...
-1
votes
2answers
88 views
Why shared_ptr<T> expects copy/move constructor in T?
I have the following code:
#include <memory>
using namespace std;
template<typename U> class A;
template<typename U>
class B
{
private:
shared_ptr<const ...
1
vote
3answers
48 views
boost::shared_ptr error at end of class
class SomeData{};
typedef boost::shared_ptr<SomeData> data_ptr;
class ABC {
public: ABC(){}
~ABC(){cached_ptr.reset(); }
data_ptr get_ptr() {data_ptr x; return x;} // it ...
1
vote
3answers
85 views
Copy boost::shared_ptr
typedef boost::shared_ptr<SomeData> data_ptr;
data_ptr cached_ptr; // class member
bool someWork(data_ptr& passed_ptr)
{
// must copy passed_ptr = cached_ptr under some conditions
// ...
-2
votes
2answers
102 views
Linked list with smart pointers
Out of boredom I've decided to mess around with the overused code:
#include <iostream>
#include <cassert>
#include <memory>
struct Node
{
Node* next;
int val;
};
int ...
0
votes
1answer
58 views
Compilation error when creating template & boost::shared_ptr based generic factory
I am using c++98 unfortunately.
template <class bT>
class Creator
{
public:
virtual bT* create() = 0;
};
template <class bT>
struct CreatorPtr
{
typedef boost::shared_ptr< ...
2
votes
2answers
76 views
Cannot dynamic cast when using dynamic_pointer_cast
Why does this code not work?
std::shared_ptr<Event> e = ep->pop();
std::shared_ptr<TrackerEvent> t;
t = std::dynamic_pointer_cast<TrackerEvent>(e);
I get the following error:
...
0
votes
0answers
70 views
initialized pointers are not passed through constructor
I have two classes server and Broker . server is a member in Broker and some of its members are initialized when Broker members are initialized.
It is simple if you look at their constructors and some ...
3
votes
3answers
183 views
Is it good practice to bind shared pointers returned by functions to lvalue references to const?
Although it took me a while to get used to it, I now grew the habit of letting my functions take shared pointer parameters by lvalue-reference to const rather than by value (unless I need to modify ...
-2
votes
1answer
48 views
Arrays and smart pointers [closed]
1.1) Is there a possibility of a memory leak when using std::vector, QVector, boost::array (not quite understand the difference between them in the use of memory and what are the advantages of each)? ...
6
votes
6answers
111 views
Const correctness with objects containing shared_ptr
Consider the object:
class Obj
{
public:
Obj() : val(new int(1)) {}
int& get() {return *val;}
const int& get() const {return *val;}
private:
...
5
votes
1answer
156 views
std::shared_ptr not working with range for
I'm trying to iterate over a temporary object in a range for loop. It looks like the object gets desctucted before the loop begins executing. Is this a standard compliant behaviour? I'm using gcc 4.8.
...
2
votes
3answers
115 views
Shared pointers and raw pointers in same container
I need to populate container with shared pointers and raw pointers at same time.
I guess shared_ptr<T> may be forced to behave like T*, if constructed with no-op deleter and no-op ...
1
vote
1answer
59 views
compilation error with std::priority_queue derived class using std::shared_ptr
I derived from std::priority_queue to implement a few specialized methods. One of these
methods somekind of a fixed queue when I add an element and the queue is full, the smallest element is dropped ...
0
votes
0answers
122 views
std::shared_ptr & boost::shared_ptr difference
I've the following code:
// interface.h
#ifndef INTERFACE_H
#define INTERFACE_H
#include <memory>
class IInterface {
public:
virtual ~IInterface() = 0;
virtual void ...
7
votes
3answers
488 views
C++11 When clearing shared_ptr, should I use reset or set to nullptr?
I have a question about C++11 best practices. When clearing a shared_ptr, should I use the reset() function with no parameter, or should I set the shared_ptr to nullptr? For example:
...
0
votes
1answer
80 views
Copy-on-write pointer object in C++
I tried to follow this article
http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Copy-on-write on how to implement copy on write pointers in C++. The problem is, it doesn't work for me.
The crux of ...
2
votes
1answer
74 views
shared boost::shared_ptr<> variable is thread safe? [duplicate]
boost::shared_ptr<A> g_a;
void func1(boost::shared_ptr<A> v)
{
g_a = v;
}
void func2()
{
boost::shared_ptr<A> a = g_a;
// a is good?
}
When func1() and func2() is ...
0
votes
1answer
143 views
Passing a variable between callback and main
I'm using ROS with C++ and after receiving data from a topic in void callback(), I need to pass this data to a variable in int main(). What I've found out so far is that I can do it using a boost ...
0
votes
1answer
114 views
Global smart pointer is not cleaning up properly
I have a c++ interface, and the derived class of that interface in one DLL, I am using the class in another process by including the interface header file, and importing a factory function that ...