Hi, I've been programming a lot of c code,
and now I'm trying to set my mind into the c++ way of doing things.
so I'm trying to avoid pointers, and use refs instead.
As far as I understand pointers are to be avoided using stl's for instance.


I have 2 matrix classes that I want to contain in another class.
But at runtime I only want one of the matrixobjects allocated and instantiated.

In good old c, I would have made a pointer to the object,
and then only allocated the one I wanted.

But It seems that you need to instantiate everything in a class.

Or am I trying to do this in a odd way?

I think the problem is in class "aCollector" at line 20-27

#include <iostream>

class aClass{
 public:
  float f1;
  float f2;
  int *array;
  aClass(float var1, float var2){printf("norm class construct\n");f1=var1;f2=var2;}
};

template <typename T>
class aTemplate{
  float f1;
  float f2;
  T *array;
  aTemplate(float var1, float var2){printf("template class constructor\n");f1=var1;f2=var2;}
};


class aCollector {
public:
  int a;
  int b;
  aCollector(int chooseType,int f, int g): (chooseType==0)?myTemplate(f,g):myobj(fg){}
  aTemplate<int> myTemplate;
  aClass myobj;
};

int myFun(aStruct as){
  return 0;
}

int main(){
  aCollector acol(2,3);
  return 0;
}

thanks in advance

What's wrong with doing the following:

class aCollector {
public:
  int a;
  int b;
  aTemplate<int> myTemplate;
  aClass myobj;

  aCollector(int chooseType,int f, int g){
      if(chooseType==0)
         myTemplate = aTemplate<int>(f,g);
      else
         myobj = aClass(fg);
      }
};

For the above to work, you will need aClass::aClass() and aTemplate::aTemplate() defined.

Thanks, I thought it was possible to avoid instantiation,
untill runtime, and then choose.
But I'll define my aClass and aTemplate with a pointer then.

thanks again.

1. the c++ way of doing things != trying to avoid pointers, and use refs instead It's Java and C# ways of doing things ;).
2. Another common approach to defer an object initialization: add init member function to your class. Declare empty objects then allocate memory with init function call at a proper moment.

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.