Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Elements of constructor get deleted by call from another class
Forum Updated to NodeBB v4.3 + New Features

Elements of constructor get deleted by call from another class

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    basil_fawlty
    wrote on last edited by
    #1

    I am defining an array with all its elements in the constructor of one class A. Now I want to use these values in my other class B.

    I am defining an Object A *a = new A() and call the array with a->array Unfortunately all the elements are deleted as soon as I am leaving the constructor from class A. Has anyone an idea why that is so?

    Here an update with a sample-code:

    First I have my class A:
    @
    class A()
    {
    QVector<Point3d> point_s;
    }

    A::A()
    {
    point_s.resize(5);
    for (int i = 0; i < 5; ++i)
    {
    point_s[i].x = i;
    point_s[i].y = i+1;
    point_s[i].z = i+2;
    }
    }
    @
    In class B I want to use the array-values. I am creating an Object from A.
    @
    #include "a.h"
    #include "c.h"

    class B : public QGLWidget
    {
    Q_OBJECT
    public:
    A *a;
    C *c;
    void paintGL();
    B(QWidget *parent = 0);
    }

    B::B(QWidget *parent) : QGLWidget(parent)
    {
    a = new A;
    b = new B;
    }

    void B::paintGL()
    {
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    gluPerspective(70, width() / height(), 0.01, 1000);

    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
    
    c->draw(a->point_s);
    
    glPopMatrix();
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    

    }
    @
    In class C I am defining the following. Maybe it doesnt make sense here but in my program it does and it is necessary.
    @
    class C
    {
    public:
    C();

    void draw_points(QVector<Point3d> point);
    void draw(QVector<Point3d> point) const;
    

    };

    C::C()
    {
    }

    void C::draw_points(QVector<Point3d> point)
    {
    int count =1;
    double angle= 360/count;

    for(int i=0;i<5;i++)
    {
        glRotated(angle,0,1,0);
        draw(point);
    }
    

    }
    void C::draw(QVector<Point3d> point) const
    {
    for(int i=0; i<point.size(); i++)
    {
    glBegin(GL_POINTS);
    glVertex3f(point.value(i).x,point.value(i).y,point.value(i).z);
    glEnd();
    }
    }
    @
    I hope you could help me now. Sorry for the late answer but I hadnt time the other days. The question still remains, why the array values get deleted.

    Thanks in Advance

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      Without seeing any code of yours it is hard to give a solid advice on the issue.
      You need to post some skeletons with the essential part of what you are doing.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lgeyer
        wrote on last edited by
        #3

        [quote author="basil_fawlty" date="1328362457"]Unfortunately all the elements are deleted as soon as I am leaving the constructor from class A.[/quote]
        Most probably because you've declared the array on the stack, which means it is destructed once it gets out of scope.

        [quote author="basil_fawlty" date="1328362457"]I am defining an array with all its elements in the constructor of one class A. Now I want to use these values in my other class B.[/quote]
        Two possibilities:

        • declare the array as member of the class
        • create the array on the heap in the constructor, delete it in the destructor

        In both cases you should use a getter instead of a public variable to comply with the rule of encapsulation.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          basil_fawlty
          wrote on last edited by
          #4

          Sorry for my late update of the question,but I hadnt got time the other days. I thank you Lukas. I thought that my array is a member of my class.
          So you are suggesting to use getters. I have never come across using getters in c++. It looks rather complicated. Maybe you could tell me a bit more how to create a getter.

          Thanks in advance

          1 Reply Last reply
          0
          • B Offline
            B Offline
            basil_fawlty
            wrote on last edited by
            #5

            Hm... Ok i have tried it like this:

            In a.cpp:

            @
            QVector<Point3d> A::get_m()
            {
            return this->point_s;
            }
            @

            In a.h:

            @
            QVector<Point3d> get_m();
            @

            In b.cpp:

            @
            b_points = a->get_m();
            c->draw(b_points);
            @

            Still the same. I am not getting it.

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lgeyer
              wrote on last edited by
              #6

              [quote author="basil_fawlty" date="1328568251"]So you are suggesting to use getters. I have never come across using getters in c++. It looks rather complicated. Maybe you could tell me a bit more how to create a getter.[/quote]

              It is nothing special. Just make your members private instead of public and add methods to get and set the value - like you did in your previous post.

              Your code works fine (apart from some slips of the pen in A #1 and B #17). glVertex3f() is called with the values specified in A::A(). So your error is either in the way you set up your OpenGL viewport or in code not shown here.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved