Sorry if this is hard to understand what I'm trying to do here, but, I do not know how to approach this.

I am using method overloading in my constructors of a class, and I have multiple "Window" functions which are all classes that can be used and I need to initialise an object of one of these classes depending on which is passed through. For example:

I have an enum which stores the types of Windows available:

    enum WindowType {

        Barlett     =   0,
        Blackman    =   1,
        Hamming     =   2, 
        Hanning     =   3,
        Nuttall     =   4,

    };

And inside my class I have the following:

template<typename Inverse>
STFT(Inverse begin, Inverse end, size_t x, size_t y, WindowType type)
{                                   

   switch(type)
   {
       case 0:
               // I need to initialise an object, in this case "Barlett"
               // Window::Barlett barlett(100); // e.g. 
           break;       
              // etc..
    }
}

The only problem is that: What if I need to pass this to another method inside a class? For example:

Transform(std::vector<double> &values, Window& {?}); 

My theory was to use an associative way, and, store it in the class members and use a template, like so:

template<typename T>
Window T;

But this, obviously would not work.

Does anyone have any suggestions to how to approach this?

EDIT:

I realise at main I could initialise which window I want to use and then pass it through to the constructor, however, I just wanted to see if there are any alternatives to the way I'm currently wanting to implement this. My main currently looks like:

STFT frquency(

    // Data
    std::begin(signal),
    std::end(signal),
    // Sizes 
    100,
    256, 
    Window::Hanning
);

I'm sorry, but I am not able to make heads or tails of what you are trying to do. Can you show a minimal "toy" example that shows what you want to do?

Use this as the starting point:

class Foo {
  public:
    enum FooType {
      A = 0,
      B = 1
    };

    Foo(FooType flag) {
      // ??
    };

  private:
    // ?? (data members?) 

};

int main() {
  Foo f( (Foo::A) );
};

Just write the "complete" code that you would like to have working (not just comments saying "here is where I'd like do something", but actual (invalid) code, and pointing out what doesn't work).

@Mike, thank you for your reply.

Assume I have the following class:

class Foo {

    public:

        enum dataMember {

            A = 0,
            B = 1
        };


        Foo();
        Foo(dataMember member)
        {
            switch(member) 
            {
                case 0:

                A a;
                break;

                case 1:

                B b;

                break;
            }
        };  

        void ProcessCall(Object& obj); // How do I access either "A" or "B" depending on what is passed through in main?
};

I can pass the following:

Foo f(A);

Let's say A is an object, I need to initialise that object and use it in other functions, so, let's assume now I have the following method:

void processCall(Object& ob)
{
   // Note how I've passed in an object. 
}

So, from passing in either A or B the constructor should know which object to initialise and therefore use. (Assuming "A" and "B" are both classes)

I need to make 'A' and 'B' accessable to all the functions in the class.

Does this make anymore sense? Thank you for the help :)

There are many ways to solve this problem, including just basic OOP-style polymorphism (make A and B be derived from some base-class with the necessary virtual functions you need, and create a new object of type A or B in the constructor).

The most obvious way to solve this is using boost::variant. As so:

class A { /*...*/ };
class B { /*...*/ };

class Foo {
  private:
    boost::variant< A, B > data;

  public:

    enum dataType {
      A_type = 0,
      B_type = 1
    };


    Foo();
    Foo(dataType member_type)
    {
      switch(member_type) 
      {
        case Foo::A_type:
          data = A();
          break;
        case Foo::B_type:
          data = B();
          break;
      };
    };  

    void ProcessCall(Object& obj) {
      switch(data.which()) 
      {
        case Foo::A_type:
          A& a = get<A>(data);
          /*...*/
          break;
        case Foo::B_type:
          B& a = get<B>(data);
          /*...*/
          break;
      };
    };
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.