Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a C++ class declaration

Tags:

c++

I want to know if I can split a C++ class declaration

Original class

    class P
    {
        private: 
           int id;
           //some really secret method
           int secretMethod();
       protected:
           int x;
       public:
           P();
           int getX();
    };

I want to show in the .h only the public and protected method and attributes, and declare somewhere else the private, where the user of the class can't see it.

Wanted class declaration:

    class P
    {
       protected:
           int x;
       public:
           P();
           int getX();
    };

Edit: I want that for:

  1. I can change the implementation of the class and for the user of the class is transparent
  2. Is easier for the user to see less information than more
  3. If I change the implementation of the class, change the private atributes and method I don't want to change the .h for the user of the class
like image 354
Troncador Avatar asked May 04 '12 21:05

Troncador


People also ask

Why is it good practice to put a class declaration in one file and the implementation in another?

Because in most cases, you'll want to use the class somewhere besides the file in which you implement it. If you make the whole program in one file, you don't need the separation.

What is partial class in Visual Studio?

Applies to: Visual Studio Visual Studio for Mac Visual Studio Code. You can use the partial keyword ( Partial in Visual Basic) to divide the declaration of a class or structure among several declarations. You can use as many partial declarations as you want. The declarations can be in one or in multiple source files.

What is class declaration in C Plus Plus?

A class is defined in C++ using keyword class followed by the name of class. The body of class is defined inside the curly brackets and terminated by a semicolon at the end. Declaring Objects: When a class is defined, only the specification for the object is defined; no memory or storage is allocated.


2 Answers

Yes, it is possible but not in the direct kind of way. Here is what you do:

my_object.h:

struct my_object {
  void fun();

  my_object();
  my_object(my_object const&);
  ~my_object();

  my_object& operator = (my_object);

protected:
  void pfun();

private:
  struct impl;
  std::unique_ptr<impl> pimpl;
};

my_object.cpp:

struct my_object::impl {
  void fun() { do stuff...}

  void pfun() { do other stuff... }

  int private_member;
};

my_object::my_object() : pimpl(new impl) {}
my_object::my_object(my_object const& o) : pimpl(new impl(*o.pimpl) {}
my_object::~my_object() {}

my_object& my_object::operator = (my_object o) { swap(pimpl, o.pimpl); return *this; }

void my_object::fun() { pimpl->fun(); }
void my_object::pfun() { pimpl->pfun(); }

As you can see, it's a lot of work and requires the heap. Everything in balance...use when you need to.

like image 185
Edward Strange Avatar answered Sep 21 '22 13:09

Edward Strange


If your purpose is to simply reduce clutter in your header, you can include a file in the middle of your class:

class P
{
#include "P.private_parts"

   protected:
       int x;
   public:
       P();
       int getX();
};
like image 20
Benjamin Lindley Avatar answered Sep 20 '22 13:09

Benjamin Lindley