0

I have a template class so idea of this template class is that I want to create vector object of type same as my template class abc. Can I pass int or char as T and use something like following

vector<abc<int>> g1;// <<-------how to do this correctly?
        g1.push_back({               //<-----  looks wrong
           {{1,1,1},{2,2,2},{3,3,3}} //<----- this is obvious wrong
    
        })

this is my template class

template <typename T>
class abc: /*drived from what???*/{
private:
    T *px=x;
    T *py=y;
    T *pz=z;
public:
    //these members must be included
    T x[10];
    T y[10];
    T z[10];


abc(const T _px[],T _py[],T _pz[]):x{{10}},y{{10}},z{{10}}
{

}

int writeto_HW()
{
    //member write to ...x,y,z to...  and other work
}
};


int main()
{
    vector<abc<int>> g1;// <<-------how to do this correctly?
    g1.push_back({               //<-----  looks wrong
       {{1,1,1},{2,2,2},{3,3,3}} //<----- this is obvious wrong

    })
    g1.push_back({{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},// <----wrong
                  {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
                  {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    });




}

is it possible? if yes then how to achieve this

I don't need any fancy push_back function from vector I just like to have access to inside of push_back code I will do some business validation on class abc members x[],y[],z[] to stl vector's push_back. so I need just structure what if its possible.

thanks for help

user786
  • 3,902
  • 4
  • 40
  • 72
  • When you say `vector> g1;// <<-------how to do this correctly?` Do you mean you want a vector that can store any type of `abc`, like `abc` and `abc` in the same vector? – NathanOliver Aug 12 '21 at 15:53
  • what kind of "validation" do you want? Note that code that does not achieve what you want to achieve is not sufficient to explain what you want to achieve. What exactly is "this" ? – 463035818_is_not_an_ai Aug 12 '21 at 16:01
  • @NathanOliver you are exactly correct. This is what I want. Thanks for ur comment – user786 Aug 12 '21 at 16:06
  • @463035818_is_not_a_number please look at my last comment and @NathanOliver comment this is exactly what I am looking for. From stl vector and `push_back()` – user786 Aug 12 '21 at 16:08
  • Simple validation just checking for x y z array values with equal = in loop – user786 Aug 12 '21 at 16:10
  • I dont know why question is still blocked for answers. Or anything. I replied and I think it make sense. Just look above this comment. All details available. If not possible then just a line would suffice – user786 Aug 12 '21 at 16:35
  • I think I need something like https://stackoverflow.com/a/68212571/4808760 – user786 Aug 12 '21 at 17:23
  • you are mixing many things. Storing elements of different type in a vector is one thing, some "validation" is a different topic, and the answer you link is about something else entirely (a custom iterator for a class that wraps a vector). Your code does not compile, if you want help to fix the compiler error, including it in the question would already help to make the question more clear – 463035818_is_not_an_ai Aug 12 '21 at 21:34

0 Answers0