• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

namespaces in classes - Valid alternative required

Hi,

Is there anyway to have a class that contains seperate namespaces depending on the methods required? For example:

class MyClass{
public:
     namespace methodSet1{
          void A1(unsigned int);
     }
     namespace methodSet2{
          unsigned int A1();
     }
};

Whil the above code does not work, is there some workaround for a similar effect without placing the methods in methodSet1 in a seperate class than methodSet2?

Thanks,
Uni
0
Unimatrix_001
Asked:
Unimatrix_001
  • 7
  • 6
1 Solution
 
josgoodCommented:
In the example you show, the methods differ because they have different signatures, so they don't need to be in different namespaces.

What are you trying to accomplish?
0
 
Unimatrix_001Author Commented:
True, but nevertheless it would be nice for the methods to be organised.

I have got a class with a number of methods that all do a very similar thing but in a slightly different way. It's getting a bit silly me having to essentially name a method after a description, as the method names then tend to be overly large. Splitting them up into namespaces would be perfect, but splitting the methods into classes would remove the idea of all the methods being of a common purpose.
0
 
josgoodCommented:
My first thought was to suggest defining an interface that each similar activity specialize -- that would maintain the idea of all classes having the same purpose.  That won't work, though, because you have different signatures.

You could define SuperA1, perhaps, that calls one A1 or another, based on the activity you're doing.  The problem with that, of course, is that SuperA1 needs all arguments that the set of A1 functions require.

Still thinking, but wanted to respond in a timely fashion...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Unimatrix_001Author Commented:
>> That won't work, though, because you have different signatures.
Don't get too wrapped up in the two methods I gave, they were just there there to illustrate the example.
0
 
josgoodCommented:
Could you show a slice of the actual problem?  Just header information is all I need to see.

(If you can define an interface that all similar activities can meet, that would be my personal preference.)
0
 
Unimatrix_001Author Commented:
Okay:

class BlockManager{
public:
      void setBlockSize(const vector<char> &rawBlock);
      void setBlockSize(const vector<char> &rawBlock);
      unsigned int getBlockSize();
      void getBlockSize(vector<char> &rawBlock);
};

This is the typical issue. See I don't want to have to add an extra parameter to the setBlockSize method to indicate how setBlockSize carries out its job (as both the methods there would do it a slightly different way). Similarly I don't want to have to rename them something like:

setBlockSizeUniformDistribution
setBlockSizeScatteredDistribution
setBlockSizeSlicedDistribution
...and the other variations I have...

Also, the getBlockSize is meant to match up with with a certain setBlockSize method, so again I don't want to be renaming the getBlockSize methods just to match up.
0
 
josgoodCommented:
This sounds like a problem for which generic programming is the solution -- meaning a template class.  Does that scare you away or are you comfortable with it?
0
 
Unimatrix_001Author Commented:
Well, I'm okay with a template class, but I don't know how that would help with splitting the class into a better segmented fashion?
0
 
josgoodCommented:
I'm working on an example...need a few minutes
0
 
Unimatrix_001Author Commented:
:)
0
 
Unimatrix_001Author Commented:
Off to bed now. Look forward to your ideas. :)
0
 
josgoodCommented:
Well!  That was a bit of a struggle.  I'm using Scott Meyer's "Effective C++ Third Edition" as my reference.  Item 47 "Use traits classes for information about types" is the section that got me through this.  I highly recommend reading that section for an explanation of the code below.  I've commented the code -- I hope it communicates.

(grin) Sometimes I think I know more than I actually do!

Anyway,  my basic thought is to define a struct for each type of distribution, where the struct contains the distribution's data and a "trait" that says what kind of distribution this struct contains.  Different distribution types require operations that are similar but differ in detail, and a separate function is written for each difference.  I believe this aligns with your current implementation.

The trick is, then, to define a single function that encompasses the similarity.  In my example code, this is setBlockSize.  setBlockSize calls one of several overloaded doSetBlockSize functions, where each setBlockSize function operates on a particular kind of distribution.

When the header is complete, the main program looks like magic.  Templates can be a real pain in the proverbial location, but they can produce a wonderful result.

I hope this meets your needs.  Enjoy!

#include <iostream>
#include "traits.h"
using namespace std;

int main()
{
   UniformDistribution uniform;
   ScatteredDistribution scattered;
   
   setBlockSize(uniform);
   setBlockSize(scattered);
   cout << "UniformDistribution vector size is " << uniform.rawBlock.size() << endl;
   cout << "ScatteredDistribution vector size is " << scattered.rawBlock.size() << endl;
}


// ==========Traits.h Header==========
#include <vector>
using namespace std;
//
//  Define some types that will indentify the distribution being used.
struct Distribution_tag { };
struct UniformDistribution_tag : Distribution_tag { };
struct ScatteredDistribution_tag : Distribution_tag { };
//
//
template <typename DisT>
struct distribution_traits
{
   typedef typename DisT::distribution_category distribution_category;
};
//
//  Each distribution struct typedef's distribution_category, which
//  will allow the compile to select among template functions,
//  based on function signature.
struct UniformDistribution
{
   std::vector<char> rawBlock;
   typedef UniformDistribution_tag distribution_category;
};

struct ScatteredDistribution
{
   vector<char> rawBlock;
   typedef ScatteredDistribution_tag distribution_category;
};
//
// setBlockSize for Uniform distribution
template <typename T>
void doSetBlockSize(T& distribution, UniformDistribution_tag)
{
   cout << "Uniform Distribution" << endl;
   distribution.rawBlock.resize(42);
};
//
// setBlockSize for Scattered distribution
template <typename T>
void doSetBlockSize(T& distribution, ScatteredDistribution_tag)
{
   cout << "Scattered Distribution" << endl;
   distribution.rawBlock.resize(84);
};

template <typename T>
void setBlockSize(T& distribution)
{
   doSetBlockSize(distribution, typename distribution_traits<T>::distribution_category());
};


Program output is
Uniform Distribution
Scattered Distribution
UniformDistribution vector size is 42
ScatteredDistribution vector size is 84
0
 
jkrCommented:
Why don't you split that into a set of classes using inheritance? I.e.

class BlockManager{
public:
      void setBlockSize(const vector<char> &rawBlock);
      void setBlockSize(const vector<char> &rawBlock);
      unsigned int getBlockSize();
      void getBlockSize(vector<char> &rawBlock);
};

class ScatteredDistribution : public BlockManager{
public:
      void setBlockSize(const vector<char> &rawBlock);
      void setBlockSize(const vector<char> &rawBlock);
      unsigned int getBlockSize();
      void getBlockSize(vector<char> &rawBlock);
};

class UniformDistribution : public BlockManager{
public:
      void setBlockSize(const vector<char> &rawBlock);
      void setBlockSize(const vector<char> &rawBlock);
      unsigned int getBlockSize();
      void getBlockSize(vector<char> &rawBlock);
};

?
0
 
Unimatrix_001Author Commented:
josgood: Quite a unique way there, works nice, thank you. :)

jkr: Thanks for the comment, although I don't really want to change the main class if I could help it and keep it as a whole. Thanks anyways, it's appreciated. :)

Uni.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now