Click here to Skip to main content
15,886,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Experts
I have a C++ template class CompositeElement that contains a field / property CompositeElement** (pointer to pointer). I know that it is not the exact intention of composite to consist a data structure holding multidimensional polymorphic derivations and/or template specializations of itself (name is not the issue in my question).

How can I construct sample instance with 4 levels recursive embedding class sets inside itself (under the CompositeElement** property/field).

I need to solve this without using std::vector / std::set / std::map, just plain C++98 C99 pointers and constructors and assignments. It must be in a shared library ("*.so") specialization workaround on instantiation with dummy function of "*.cpp" file. Construction from client *.cpp file using this library.

some experimental immature I developed


thanks

What I have tried:

developed example .
C++
#ifndef COMPOSITION_LIBRARY_HEADER
#define COMPOSITION_LIBRARY_HEADER

namespace compositionlib{

   template <class T>
   class __attribute__ ((visibility("default"))) CompositeElement{
   public:

      // Default Constructor
      explicit __attribute__ ((visibility("default"))) CompositeElement( );

      // Specialized Template Argument Constructor
      explicit __attribute__ ((visibility("default"))) CompositeElement( T const& payload );

      // Copy Constructor
      explicit __attribute__ ((visibility("default"))) CompositeElement( CompositeElement const& other );

      // Copy Constructor with contents
      explicit __attribute__ ((visibility("default"))) CompositeElement( CompositeElement const& other, T const& payload );

      const std::auto_ptr< T >& getData() const;
      const size_t & getSize() const;

   private:
      
      const size_t m_size;
      const std::auto_ptr<T> m_data;
   public:
      bool m_isCopiedConstructed;
     //  CompositeElement __attribute__ ((visibility("default"))) m_CompositeElementData; incomlete type
      CompositeElement __attribute__ ((visibility("default"))) *m_ptrOfCompositeElementData;
      CompositeElement __attribute__ ((visibility("default"))) **m_setOfCompositeElementData;


   };
}

#endif
//COMPOSITION_LIBRARY_HEADER
Posted
Updated 8-Jan-20 3:16am
v2
Comments
Stefan_Lang 8-Jan-20 11:03am    
I've added the C++ tag to your formatting. That said, it's still hard to understand if you don't offer any information on what __attribute__ and ((visibility("default"))) means.

Besides, the 'explicit' keyword is C++11 or later - I thought you wanted C++98?
Audiory Researcho 9-Jan-20 5:24am    
Thanks, I am a beginner questions asker /: ...
Rick York 8-Jan-20 11:09am    
I have no idea what "4 levels recursive embedding class sets inside itself" means. I know what nested or embedded classes are but where does recursive fit into this? It is also unclear why you must have "4 levels."

Are you sure you need __attribute__ statements for all of the constructors and members? I do it for just the class itself since you nearly always want to export the entire class and not just certain methods.

Lastly, you should reconsider using auto_ptr because it is flawed and has been deprecated. Now one should use unique_ptr or shared_ptr. Generally, unique_ptr will suffice but if you have to transfer ownership of a pointer then shared_ptr should be used.
Audiory Researcho 9-Jan-20 5:13am    
I try to avoid a simplified initialization of 1D/2D/3D where it can be defined with arrays in a simple manner, 4D represent a complicated multidimensional setup to prevent easy escape from understanding the context of my interest in this question - building sparse multidimensional features vector space for machine learning. (in final usage it might go up to 1000 dimensions features vector space ). I am sure the __attribute__ is not necessary in some of the places, it emphasis that these data fields/properties will be accessed from outside the *.dll/*.so module (that is the client module - might be built with different compile/pre-processor/link switches)
sorry if it raise other not reverent thoughts.
Stefan_Lang 9-Jan-20 2:43am    
If you want C++98 (or any later standard) compliant code, you can't use GCC extensions such as __attribute__, and there is no type size_t in the global namespace: for C++98 it's only available within the namespace std.

Moreover, AFAIK for C++98 the entire namespace std was still hidden inside the sub-namespace tr1, and nothing of it was technically part of the standard. Most C++98 compliant compilers (if not all) provided std::tr1 implementations, but if they did, they either provided STL container implementations along with auto_ptr, or no STL at all. So if you think you can use auto_ptr, there's no reason not to use vector or another STL container.

If you are limited to use a very specific compiler, e. g. GCC X.Y, then say so, not C++98! That is a considerable difference!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900