1

I'm trying to create a pointer to a pointer of a class. I'm trying not to call the default c'tor because he advance the static integer. so I'm using the copy c'tor to avoid advancing the var, the only problem is that I'm not sure what is the correct syntax. here is the code:

#include <stdio.h>


class A
{
    static int x;
    int m_x;
    int m_age;
public:
    A(){m_x=x;x++;m_age=0;}
    A(const A& that ){m_age =that.m_age; m_x = that.m_x;}
    A(int age){m_age = age;}
    int getX(){return m_x;}
    int getStaticX(){return x;}
    int getAge(){return m_age;}
};

int A::x = 0 ;

int main()
{
    int size = 15;
    A *tmp = new A[size]();
    A *tmp1;

    //I'm not sure here what is the currect symtax to do this:
    for (int i = 0; i< size;i++)
    {
        tmp1[i] = new A(tmp[i]);
    }
    ////////////////////////////////////////////////////        


    for (int i =0 ;i<size;i++)
    {
            //I want the same result in both prints
        printf("tmp1:%d\n",i);
        printf("age:%d  m_int:%d  static:%d\n",tmp1[i].getAge(),tmp1[i].getX(),tmp1[i].getStaticX());
        printf("tmp:%d\n",i);
        printf("age:%d  m_int:%d  static:%d\n",tmp[i].getAge(),tmp[i].getX(),tmp[i].getStaticX());
        printf("EOF\n\n");
    }


    return 0;
}

thanks!

nos
  • 223,662
  • 58
  • 417
  • 506
David
  • 287
  • 2
  • 3
  • 13

4 Answers4

0

you can read and have examples of copy constructor here or here and do:

A(A& that ){m_age =that.m_age; m_x = that.m_x;}

for example.

but the problem here is that you allocated tmp but tmp1 is not allocated and thus this is an assignment to an unallocated memory

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
0

ok i've manged to to that like this:

int main()
{
    int size = 15;
    A *tmp = new A[size]();
    A *tmp1 = tmp;

    for (int i =0 ;i<size;i++)
    {
        printf("tmp1:%d\n",i);
        printf("age:%d  m_int:%d  static:%d\n",tmp1[i].getAge(),tmp1[i].getX(),tmp1[i].getStaticX());
        printf("tmp:%d\n",i);
        printf("age:%d  m_int:%d  static:%d\n",tmp[i].getAge(),tmp[i].getX(),tmp[i].getStaticX());
        printf("EOF\n\n");
    }


    return 0;
}
David
  • 287
  • 2
  • 3
  • 13
0

A way to do it is to allocate unitialized memory and construct the objects in place:

#include <memory>

A* tmp = new A[size]();
std::pair<A*,std::ptrdiff_t> buf = std:get_temporary_buffer(size);
assert(buf.second == size); // the buffer returned might be smaller than requested

A* tmp1 = buf.first;
std::uninitialized_copy(tmp, tmp+size, tmp1);

Note that your class is pretty much broken, unfortunately. Do obey The rule of three and implement the assignment operator and destructor and decrement the counter there, as appropriate for your needs.

Community
  • 1
  • 1
jrok
  • 54,456
  • 9
  • 109
  • 141
0

A *tmp = new Asize; //creating 15 objects of A pointed by Ai. Those 15 object while creation, using new operator call default constructor for 15 times.

Hence you would be getting the output incremented. Rather you should use

   int max_size = 15;         
   A *tmp1[size];                
   for (int i = 0; i< max_size;i++)
          tmp1[i] = &tmp[i];  

I think this solution will work out.

Santosh Sahu
  • 2,134
  • 6
  • 27
  • 51