1

In the following snippet, MyClass has a static method which returns its shared pointer. To make to code concise, we use the alias MyClassPtr for std::shared_ptr<MyClass>.

However, to accomplish this, we declare the class before declaring the shared pointer alias, which then follows the actual class declaration. It looks verbose.

Is there some way to reorganize the code so that

  • keep the MyClassPtr alias (it is shared across the project)
  • without "declaring" MyClass twice

code below:

class MyClass;
using MyClassPtr = std::shared_ptr<MyClass>;

class MyClass {
public:
    static MyClassPtr createMyClassInstance();

private:
/*Other members & methods*/
}

I'm OK with the current implementation. But I would like to seek experienced guy's advice if the code can be improved.

hackjutsu
  • 8,336
  • 13
  • 47
  • 87

4 Answers4

3

Your question is somewhat vague, as you ask how the code can be simplified, yet you fail to provide any real code, only the outline of a class definition. Instead, I try to answer the more sensible question of how to improve your code. I suggest

  1. Avoid the alias MyClassPtr, it's not really necessary (it should not be used much if you use auto) but more importantly reduces verbosity and hence readability of the code, since it's not obvious from its name that MyClassPtr refers to a shared_ptr.

    If you insist on having a shorthand for the smart pointer, you can define this after the class definition, thus avoiding the forward declaration

  2. Rename MyClass::createMyClassInstance to something more verbose, I suggest MyClass::createSharedPtr (no need to have MyClass in the function name again).

  3. Don't forget the ; after a class definition.

Thus,

class MyClass
{
public:
    static std::shared_ptr<MyClass> createSharedPtr();    
private:
    /* Other members & methods */
};

using MyClassSharedPtr = std::shared_ptr<MyClass>;  // optional

IMHO, good code should be self-explanatory and hence not necessarily most concise/brief, though redundancies must be avoided.

Community
  • 1
  • 1
Walter
  • 44,150
  • 20
  • 113
  • 196
  • Thanks, `createMyClassInstance` is just a random name I pick up to illustrate the method's purpose. I'm wondering is there any way to avoid declaring `MyClass` "twice". – hackjutsu Sep 06 '17 at 23:51
  • There is nothing wrong with that. You only define it once, but can declare it as often as you like. – Walter Sep 06 '17 at 23:52
2

Make Ptr a member type:

class MyClass {
public:
    using Ptr = std::shared_ptr<MyClass>;
    static Ptr createMyClassInstance();

private:
/*Other members & method*/
};

// ...

MyClass::Ptr p = MyClass::createMyClassInstance();
Daniel H
  • 7,223
  • 2
  • 26
  • 41
0

Replace the return type with auto:

class MyClass {
public:
    static auto createMyClassInstance();

private:
/*Other members & method*/
}

Demo:

Steephen
  • 14,645
  • 7
  • 40
  • 47
0

is there some way to reorganize the code so that

(1)keep the MyClassPtr alias (it is shared across the project)

(2) without "declaring" MyClass twice

You you accept to declare MyClassPtr twice, you can declare it inside the class and "export" it outside

#include <memory>

class MyClass
 {
   public:
      using MyClassPtr = std::shared_ptr<MyClass>;

      static MyClassPtr createMyClassInstance();

   private:
      /*Other members & methods*/
 };

using MyClassPtr = MyClass::MyClassPtr;


int main()
 {
   MyClassPtr  np { nullptr };
 }
Community
  • 1
  • 1
max66
  • 65,235
  • 10
  • 71
  • 111