Initializer list with inheritance

Hello. I wish to create a constructor that simultaneously uses a initialization list and calls the constructor of the base class

This is what I tried to do
 
inline Vec4f (): w(0) Vec3f () {}


With that, I expected to initialize w with 0, while calling one of the Vec3f constructor. Specifically, this one:

inline Vec3f () : x (0), y (0), z (0) {}

I have another class where it calls the base class, but does the initialization does happen inside the brackets.

So, what I am trying to do is achievable with another syntax? Or I will have to life with the initialization happening inside the {} ?

Thanks for the support.
closed account (SECMoG1T)
look at this simple case see if it might help: initializing base classes


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>

class foo
{
    public:
        foo(int dt):_data(dt){}
        virtual void show() = 0;
        virtual ~foo(){};
    protected:
        int _data;
};

class bar: protected foo
{
    public:
        bar(int dt,const std::string& nm):foo(dt),_name(nm){} ///see here
        void show() override{std::cout<<"data: "<<_data<<" name: "<<_name;}
        ~bar(){};
    private:
        std::string _name;
};

int main()
{
   bar m_bar(1900,"bar");
   m_bar.show();
    return 0;
}
Last edited on
Try C++17 aggregate initialization

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <cassert>

struct Vec3f {
    double x = 0;
    double y = 0;
    double z = 0;
};

struct Vec4f : Vec3f {
    double w = 0;
};

int main() {
    Vec4f v{1,2,3,4};
    assert(v.x == 1);
    assert(v.y == 2);
    assert(v.z == 3);
    assert(v.w == 4);
}
Try C++17 aggregate initialization


Unfortunately, even C++11 is beyond my system.

Anyway, using the suggestion of SECMoG1T, I did:

1
2
inline Vec4f (): Vec3f(), w(0) {}
      inline Vec4f (float a,float b,float c,float d): Vec3f (a,b,c), w(d) {}


However, when trying to compile it, I receive:
|4|error: no match for call to ‘(Vec4f) (float, float, float, float)’ |5|error: no match for call to ‘(Vec4f) (float, float, float, float) |6|error: no match for call to ‘(Vec4f) (float, float, float, float) |7|error: no match for call to ‘(Vec4f) (float, float, float, float)|


These lines are
1
2
3
4
row1 (1.0f,0.0f,0.0f,0.0f);
   row2 (0.0f,1.0f,0.0f,0.0f);
   row3 (0.0f,0.0f,1.0f,0.0f);
   row4 (0.0f,0.0f,0.0f,1.0f);


and all these row are private Vec4f from another class
if that's the exact code on those lines, then you are not calling the constructor, but trying to use operator()

> and all these row are private Vec4f from another class
these kind of description are useless
¿is it really too much to ask for a minimal testcase that does reproduce your issue?
these kind of description are useless
¿is it really too much to ask for a minimal testcase that does reproduce your issue?


But No one did it. Anyway here is a test case:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Vec3f {
   public:
      inline Vec3f () : x (0), y (0), z (0) {}
      inline Vec3f (float xx, float yy, float zz) : x (xx), y (yy), z (zz)  { }
   private:
      float x,y,z;
};

class Vec4f : Vec3f
{
   public:
      inline Vec4f (): Vec3f(), w(0) {}
      inline Vec4f (float a,float b,float c,float d): Vec3f (a,b,c), w(d) {}
   private:
      float w;
};

class Matrix4x4 {
   public:
      void setIdentity ();
   private:
      Vec4f row1, row2, row3, row4;
};

void Matrix4x4::setIdentity () {
   row1 (1.0f,0.0f,0.0f,0.0f);
   row2 (0.0f,1.0f,0.0f,0.0f);
   row3 (0.0f,0.0f,1.0f,0.0f);
   row4 (0.0f,0.0f,0.0f,1.0f);
}


main () {
   Matrix4x4 a;
   a.setIdentity ();
}
Last edited on
1
2
3
4
5
6
void Matrix4x4::setIdentity () {
   row1 = Vec4f(1.0f,0.0f,0.0f,0.0f);
   row2 = Vec4f(0.0f,1.0f,0.0f,0.0f);
   row3 = Vec4f(0.0f,0.0f,1.0f,0.0f);
   row4 = Vec4f(0.0f,0.0f,0.0f,1.0f);
}


This seems rather a long-winded way of creating a matrix class. Outside of special relativity why do you want to create 4-dimensional objects from 3-dimensional ones?
@lastchance: search for projective space
this allows you to represent translation as a matrix
1 0 0 tx
0 1 0 ty
0 0 1 tz
0 0 0 1
then to apply the transformation v = T u
then you may stack it with rotations and scale v = T R S u = M u

while you are at it, check out how to create circles with bezier lines
Topic archived. No new replies allowed.