-1

I'm doing my homework (and learn how C++ works). My task is:

Define some class with field...(never mind) create an vector and array from these object and iterate it! (listing, average by field,etc).

Now it's correctly works with vector, but array doesnot work:

static Cipo* cipok;  // object array
static int cep = 0;  // endpoint index
static int ccap = 0; // array size

Default assignmet opearator for Cipo:

public: Cipo& operator=(const Cipo &c)
{
    return ((Cipo&)c);
}

Initalization:

cipok = (Cipo*) malloc(sizeof(Cipo*)*100); // new Cipo[num] doesn't work..
ccap = 100;

Test code:

for (int i = 0; i < 5; i++)
{
    Cipo c(43.5, "str", 12670, false, false);
    std::cout << c.ar <<" ";
    cipok[cep] = c;
    std::cout << cipok[cep].ar << " ";
    cep++;
}

And the result:

12670 0 12670 0 12670 0 12670 0 12670 0

But objects not "disappeared" if I use vector, push_back() the objects and read from the vector with direct indexing (or with iterators). Why do they exhibit this behaviour?

David G
  • 94,763
  • 41
  • 167
  • 253
Dankó Dávid
  • 302
  • 3
  • 17
  • The thing is that `cipok` is a pointer. In no way does its type reflect that it's an array. Something like `std::array` or `std::vector` clearly shows what it is and cleans itself up properly. And even though you shouldn't use `new`, it should be preferred over `malloc`. The fact that it's not working means you should make it work, not fall back to something else that's bound to break. – chris Jun 01 '13 at 01:12
  • 3
    Please fix the formatting of your code. Don't use HTML -- highlight the code and press ctrl-K to format. – Joe Jun 01 '13 at 01:12
  • Woah, I just noticed how wacky that assignment operator is. For an example on the canonical way, see [operator overloading](http://stackoverflow.com/questions/4421706/operator-overloading). You're also allocating 100 pointers worth of memory, not 100 objects. – chris Jun 01 '13 at 01:18
  • Uhm... you're allocating space for 100 pointers to Cipo instances, which you then treat as space for 100 Cipo instances. Repeat after me: A pointer to a Cipo isn't a Cipo anymore than a picture of a cake is a cake. – Nik Bougalis Jun 01 '13 at 01:22
  • @chris: How did I miss *that*? – Nik Bougalis Jun 01 '13 at 01:27
  • `operator =` is awesome too - does absolutely nothing - not sure what one expect of such operator. If copy constructor have the same behavior... – Alexei Levenkov Jun 01 '13 at 01:27
  • Yes operator= "do nothing" but required for vector to work (else compiler throw error). The "Cipo" class have no empty constructor because it have only constant field. and i readed: "new (keyword) initializes the allocated memory by calling the constructor (if it's an object)." With the malloc call i would to allocate empty space for object without calling any constructor, then give value (address) for element in array. How can i do it? Also vector can do is somehow. – Dankó Dávid Jun 01 '13 at 02:17
  • I Solved it! I sore the Object's pointer, in vector (vector) and in array (Cipo**) and commonly way a can access the fields: (*cipok[i])->ar and (*interator)->ar – Dankó Dávid Jun 01 '13 at 03:10

2 Answers2

1

You immediate problem is likely caused by whacky implementation of operator = that does absolutely nothing. I'd recommend step through the code in debugger to see it. operator = (and copy constructor) should properly copy values into destination object.

There are many other issues with the code - your naming convention is ... interesting, you seem to try to cast whatever you have to whatever result is required for code to compile without reasoning what should actually be done. malloc in C++ code is very rarely needed...

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

I think the general problem is, I'm programming always in java (but now in university i must prog. in C/C++, naming conventions, like in java and in hungarian the Cipő is meaning Shoe). And in Java there is no pointers, and all object always acces by reference, but looks like (as i tested ) if i create a new object array the C++ will not allocate only 100 pointer which points to the object (where the object data starts), it allocated 100*sizeof(object) and for this place i can add data trougth assing operator.

it's my teory true?

So i tried to manage Object acces like in java.

Why copy the Object if itself alredy exist? (I don't like to "clone" objects).

Dankó Dávid
  • 302
  • 3
  • 17