logo
down
shadow

Check if two (smart) pointers point to the same derived class


Check if two (smart) pointers point to the same derived class

By : cristian muñoz
Date : November 20 2020, 03:01 PM
it fixes the issue You can use a RTTI typeid, but generally, it is a bad design to must have to use dynamic_cast because it may violate the liskov substitution principle
code :
std::unique_ptr<B> p1, p2;
if(typeid(*p1) == typeid(*p2)) // correction by Justin


Share : facebook icon twitter icon
allocating memory for derived class members based on boost smart pointers in the base class through CRTP

allocating memory for derived class members based on boost smart pointers in the base class through CRTP


By : Hans
Date : March 29 2020, 07:55 AM
it should still fix some issue The address of the shared_ptr is known at compile time for the reasons given in the above answers, but the shared_ptr itself is still uninitialised because the derived-class constructor has not yet been called, and thus has not had a chance to implicitly call its instance members' constructors, including the default constructor for shared_ptr. Therefore, when you call reset() to assign the shared_ptr, it first tries to release (and possibly delete) the object at whatever spurious address it contains (to avoid leaking an existing referent) before assigning and referencing the new object. That first step, I believe, is what causes the segfault.
If the shared_ptr constructor ran first, it would null its contained raw pointer, preventing the subsequent reset() call from trying to release an object at the spurious address.
Using smart pointers to store instances of a derived class in a vector

Using smart pointers to store instances of a derived class in a vector


By : wekkew
Date : March 29 2020, 07:55 AM
this one helps. Derived private inherit from Base which means Derived has a Base not Derived is a Base. Change to
code :
class Derived : public Base
//              ^^^^^^
using smart pointers in a derived class constructor

using smart pointers in a derived class constructor


By : Mert Düzen
Date : March 29 2020, 07:55 AM
I wish this helpful for you As I suggested in the comments, perhaps a polymorphic approach would be easier...
code :
class MultiComponent{
  public:
    typedef std::vector<std::shared_ptr<Component>> components_vec;
    components_vec components;

    MultiComponent(components_vec& cv){
        components = cv;
    }
}

class Widget: public MultiComponent {
  public:

    Widget(MultiComponent::components_vec& cv)
        : MultiComponent(cv){}
}
inheritance and smart pointers to instance of derived class

inheritance and smart pointers to instance of derived class


By : Sam
Date : March 29 2020, 07:55 AM
Hope this helps when i do this, i usually end up with two classes, an acceptor and a connection. the acceptor waits for a connection request and passes that off to a socket consumer that create a new connection thread and starts it listening. maybe something like this.
How to dereference derived class objects stored in a vector of smart pointers?

How to dereference derived class objects stored in a vector of smart pointers?


By : Ayush Gupta
Date : March 29 2020, 07:55 AM
I wish this helpful for you I'm building a chess game. I have a base class gamePiece and a derived class called rook.
Related Posts Related Posts :
  • Determining if a pack is empty or not
  • What is template<> template<> syntax in the context of template-alias for?
  • Understanding of a specific function Determinant of a Matrix
  • Dynamically add QWebEngineView to layout
  • Linux platform tag for python module built with pybind11
  • Copy and Base Constructor don't initialize C++
  • Xcode gives Mach-O linker error: "_JNI_CreateJavaVM", referenced from: _main in main.o
  • Variadic deduction guide not taken by g++, taken by clang++ - who is correct?
  • Write a proxy class in C++
  • Qt testing when dependent on Network
  • RGB color in 24 bitmap image and 16 bitmap image
  • shadertoy to SFML
  • Inserting Objects into an array with dynamic memory (No vectors allowed) C++
  • Find parent node function for binary tree
  • Rotate Binary Tree
  • Boost asio async_read_until stop reading on matched condition
  • How can I change the QGraphicView selected Item color
  • Understanding object slicing
  • How to change pointer so that it point to somewhere else?
  • Dynamically add overlay widgets
  • Compressing 3 number data in a byte
  • OpenGL - Uniform not being passed properly?
  • C++ Visual Studio "Non-standard syntax; use '&' to create a pointer to member"
  • Doxygen won't resolve reference to typedef in namespace
  • How to test for assert in debug build, but returned value in release?
  • use boost spirit parse int pair to vector
  • How #define works in Programming when define has value with operator?
  • C++: Passing objects by value to a member function of the same class
  • Confusion in C++ constructor
  • Implicit conversion operator doesn't kick in with operator overloading
  • From tuple of N elements to tuple of N/2 pairs
  • Operators Not Working With Class Objects?
  • GDAL Not Linking
  • C++ aligning characters to display neatly with any input implemented
  • c++, method to display content in hash table.Using double linked list
  • How to apply a function to a variadic argument list and cat them to a tuple
  • Doubly linked list c++ crashing after 3rd node
  • Transform a specific type of argument using templates
  • Function with recursion is causing stack overflow
  • working with multi-word typedefs
  • Calling std::to_string or std::to_wstring based on given typedef
  • Ensure a `std::vector` won't move its pointer
  • Appopriate Container that acts like normal static array?
  • Using enable_if to disable a template constructor of a template class
  • define implicit conversion from template class to primitive type based on other template
  • C++ class template, how to overload [ ] operator in a specific stuation?
  • What is the purpose of using an iterator in this function's return value?
  • C++ overriding the value of inherited static const variables
  • C++: what is special about punning character arrays according to the standard?
  • Producer consumer pthreads program not finishing
  • How do I start two thread teams with different thread functions at the same time?
  • OpenGL 3.3: GL_INVALID_OPERATION when calling glBindBuffer
  • Boost Karma generator for composition of classes
  • Can the storage of trivially copyable objects be safely reallocated with realloc?
  • Can I force a C++ class to be non-abstract?
  • The QPropertyAnimation is automatically deleted when using QParallelAnimationGroup?
  • Failing to specialize function for function pointer
  • Getting an error when using delete[] cpp
  • recursion in variadic template function of different argument types
  • QTWidgets QTableWidget crash with segfault
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org