C++将类的指针分配给unique_ptr或shared_ptr

C++ Assigning this pointer of a class to either a unique_ptr or a shared_ptr

本文关键字:ptr unique shared 分配 指针 C++      更新时间:2023-10-16

我有一个基类要从中继承,在声明其任何派生类之前,必须首先声明基类的至少一个实例。我正在考虑将基类的this pointer存储到它自己的unique_ptr成员变量中,而不是使用static_ptr。此外,基类将跟踪其派生类的所有实例,直到声明另一个基类为止。以下是我的类声明:

#include <vector>
#include <map>
#include <memory>
#include <string>
class Parent {
public: {
enum ChildType {
CHILD_NONE = 0,
CHILD_A,
CHILD_B,
CHILD_C,
}; // ChildType
protected:
ChildType type_; // Type of Child Class when inheritance is used
std::string childName_; // Name of Child Class when inheritance is used
private:
const std::string myName_; // Name of this parent class
bool parentConstructed_ = false; // Flag if this parent was constructed
const static Parent* s_firstParent_; // static pointer
std::unique_ptr<Parent>  me_; // Or
std::shared_ptr<Parent>  me_;
// Some Typedefs for containers
typedef std::vector<std::shared_ptr<Parent>> Children;
typedef std::vector<std::shared_ptr<Parent>> OtherParents;
typedef std::vector<std::shared_ptr<Parent>> Siblings;
typedef std::vector<std::string> Names;
typedef Names ChildrenNames, OtherParentsNames, SiblingsNames;
// Containers and map associations of names.
Children children_;
ChildrensNames namesOfChildren_;
std::map< ChildrensNames, Children > mapChildren_;
OtherParents otherParents_;
OtherParentsNames associatedParentsNames_;
std::map< OtherParentsNames, OtherParents > mapParents_;
Siblings siblings_;
SiblingsNames namesOfSiblings_;
std::map< SiblingsNames, Siblings > mapSiblings_;
public:
// This constructor must be called at least once as a base class instantiation
// Before Any of its derived members can be declared.
explicit Parent( const std::string& parentName );
// Other necessary constructors and operators for move semantics
Parent( Parent &&self );
Parent& operator=( Parent &transfer );
Parent( Parent const& ) = delete;
Parent& operator=( Parent const & ) = delete;
// Other functions that are part of the public interface that
// may be overridden by the inherited types
virtual void printName() const; 
const std::string& getName() const;
// Other public methods that are common among all types including
// derived and base types.
protected:
// Constructor Signature to be Used for Derived Types
Parent( const std::string& parentName, std::string& childName, ChildType type );
// Other Protected Members for use with derived types
};

所有派生类型都将从此基础公开继承,例如:

class ChildA : public Parent {
public:
ChildA( const std::string& parentName, std::string& myName, ChildType type );
};

我脑海中使用这个类层次结构的想法如下

#include "ChildA.h"
#include "ChildB.h"
#include "ChildC.h"
int main() {
// If we try to do this:
ChildA a( std::string( "Parent" ), std::string( "ChildA" ), Parent::ChildType::CHILD_A );
// The Last parameter would not be needed to be set since that would
// be done automatically by default for all derived types, but just shown here for demonstrative purposes.
// This construction of derived type would throw an exception because at
// least one base was not declared.
// This must be done first:
Parent parent( std::string( "Parent1" );
// Then this would be valid
ChildA a( std::string( "Parent1" ), std::string( "ChildA" ) );
ChildB b( std::string( "Parent1" ), std::string( "ChildB" ) );
// Then if we created a 2nd parent base
Parent parent2( std::string( "Parent2" );
// The next set of child classes might be nested under this parent since
// it is a 2nd instance and the name doesn't match.
// if one was to call this constructor above as is but passed in 
// the same string as from a previous instance it would also throw an exception.
// In this next line of code it would not necessarily be nested automatically
// to Parent2 because it would check the string name passed in for the
// parents name and nest it accordingly if it found that string.
ChildC c( std::string( "Parent1 or 2" ), std::string( "ChildC" ) );          
return 0;    
}

我知道如何创建一个带有静态函数的静态指针,以便在应用程序运行时从类中获取该指针,使其只有一个实例。我只想知道是否可以使用shared_ptr或unique_ptr,然后在成功创建至少一个基类型的实例后,先将(这个)指针保存到任何一种类型的智能指针中?我更喜欢做一个唯一的,父类"拥有"它自己的指针。

如果你需要更多信息,请告诉我,我会根据要求更新这个问题,比如显示我的基类的构造函数的定义。

您能做的最好的事情就是使用轻量级模式。

您将创建每个类一次(使用std::shared_ptr似乎是最明智的),但随后您将使用一个轻量级类来封装每个类,这些类代表您只创建过一次的类,即您将创建一个Parent和Child类(没有继承层次结构),然后将这些类传递给ParentFlyweight和ChildFlyweight,其中ChildFlyweith从ParentFlyweight继承。