2

I have a class like this:

class Wall
{
    private :
        Quad faces[6];  
};

I have the constructor like this :

Wall::Wall(Quad f[], const float &mass, Vector3 center)

I want to initialize faces to be f(or copy f to faces),Quad is struct that doesn't have a default constructor.

Now I solved the problem by using faces{f[0],f[1],f[2],f[3],f[4],f[5]} in the initializer list but this requires c++11 which I'm afraid some of my friends don't have it, and I need to pass my code to them.

There are many similar questions but all of them seem to not have solutions other than switching to vector or using some complicated code which I don't want, as you can understand from the classes' name, a Wall doesn't need a vector(it only has 6 faces so why a vector).

Is this really hopeless ? isn't there any way ?

PS

Whether in the constructor body or in the initializer list, it doesn't matter.

changing to dynamic arrays(Quad *) doesn't matter either but keeping with static arrays is preferable.

4
  • 2
    Is there a reason you can't add for(int i = 0; i < 6; i++) faces[i] = f[i]; and a check for the length of f to your constructor? Commented Apr 21, 2015 at 18:18
  • @Guvante I tried that , it says "no matching for Quad::Quad()" Commented Apr 21, 2015 at 18:20
  • You may want to verify C++11 being a non-starter as minimal searchs point to no standard way of doing this unfortunately. Commented Apr 21, 2015 at 18:27
  • What about std::array<Quad 6>? reference std::array -edit: now see you mention can't use C++11. Commented Jul 24, 2015 at 17:46

1 Answer 1

1

Several options. The easiest is probably to subclass Quad with something that has a default constructor:

class Wall {
public:
  Wall(Quad f[], ...) {
    for (int i = 0; i < 6; ++i) faces[i] = f[i];
  }
private:
  class MyQuad : public Quad {
    MyQuad() : Quad(...) {}
  }

  MyQuad faces[6];
};

Another option is to use placement new - note that the code below doesn't work out of the box since it is not doing proper alignment/padding and dealing with some aliasing issues, which are left as an exercise to the reader. It should give you a starting point though.

class Wall {
public:
  Wall(Quad f[], ...) {
    for (int i = 0; i < 6; i++) {
      // TODO: take padding into account
      new (&faces_data + sizeof(Quad) * i) Quad(f[i]);
    }
  }
  ~Wall() {
    for (int i = 0; i < 6; i++) {
      face(i).~Quad();    
    }
  }
  Quad& face(int idx) {
    // TODO: take padding into account
    return (reinterpret_cast<Quad*>(faces_data))[idx];
  }

private:
  // TODO: force proper alignment and take padding into account
  char faces_data[sizeof(Quad) * 6];
};
Sign up to request clarification or add additional context in comments.

2 Comments

sorry Aaronl but if I want a default constructor I wouldn't define a new subtype but simply put it in the Quad class. hmmm the second solution semms C-style ?!!
I assumed that you couldn't or didn't want to modify the Quad class. The second solution is about as C++ as it gets, with placement new and all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.