1

Having problems with array of pointers. I have a custom class called C. C has a variable double c1. I have to sort an array of C-s by c1 using a custom written sorting algorithm. I am guessing that since I have to move objects in array, it would be much more eficient to just move pointers to objects, therefore I have to use not an array of objects, but an array of pointers to objects.

I initialized the array like that:

C** someC;
someC = new C*[size];
for(int i = 0; i < size; i++) {
    // a and b are of type CPoint
    someC[i] = new C(a,b);
}

Am I doing this part correctly? It is the calling of C objects that then causes problems:

someC[i]->a.x

gives me an error: left of '->a' must point to class/struct/union/generic type

I am new to C++ so I may be missing something obvious, but I did some research and did not find anything. Maybe I am not understanding well how pointers work...


UPDATE

The header file of C class:

#pragma once

class C
{
public:
    CPoint a;
    CPoint b;
    double c1;
    C(void);
    C(CPoint,CPoint);
    ~C(void);
};

the implementation:

#include "StdAfx.h"
#include "C.h"
#include <math.h>

C::C(void)
{
}

C::C(CPoint a, CPoint b)
{
    this->a=a;
    this->b=b;

    double c1_x = a.x - b.x;
    double c1_y = a.y - b.y;
    c1= sqrt( (c1_x * c1_x) + (c1_y * c1_y));
}

C::~C(void)
{
}

UPDATE

The problem was in the code I provided in the comments, I did not notice I was calling the array in a wrong way like this:

pDC->MoveTo(someC[i]->a.x, someC->a.y)

So the second call was incorrect. Thank you all for your help

9
  • I don't see anything wrong with it. Maybe there's another part where you declare someC wrongly? Commented Nov 21, 2012 at 21:55
  • Please give the exact line that causes the error, along with the line before it. Also, provide the class definition. Commented Nov 21, 2012 at 21:55
  • @neuromouse This should work. Error is from the code you have not shown here. Is it the same someC you are accessing which you new'ed? Commented Nov 21, 2012 at 21:55
  • First, show us the definition of CPoint and C please, and second, is there a member a in class C ? If not, the above syntax is wrong. We'll know for sure if you give us a little more info. Commented Nov 21, 2012 at 21:59
  • I am actually calling someC in OnDraw method like that: pDC->MoveTo(someC[i]->a.x, someC->a.y); someC is defined as public in the header file Commented Nov 21, 2012 at 21:59

3 Answers 3

2

Philosophy aside, this is pretty telling from your comment (emphasis added):

"I am actually calling someC in OnDraw method like that: pDC->MoveTo(someC[i]->a.x, someC->a.y); someC is defined as public in the header file"

Specifically, this in your parameter list :

someC[i]->a.x, someC->a.y

This tells me one of these is wrong. Judging by your error, I'm going to go with the first one. It would solidify that if we could see the definition of your object that is implementing OnDraw() and where exactly it is getting someC from.

  • If someC is a C* in your containing object, the second parameter is correct, the first is wrong.
  • If someC is a C** in your contained object then the first parameter is correct and the second is wrong.
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks man! I'm really embarrased right now, i DID NOT notice that! Yes, that was it
@neuromouse no problem. I didn't catch it until I glazed over the comments again and picked up the lack of subscripting in the second param. Was a 50/50 shot it was a typo, but I figured i'd roll the dice =P
2

Unless your C objects are really really expensive to copy construct, don't bother implementing your custom sort algorithm, but define a strict total order over C:

bool operator<(C const& lhs, C const& rhs) {
  return lhs.c1 < rhs.c1;
}

and use std::sort on an std::vector<C>. If you do worry about copy construction overhead, you can also directly use an std::set<C> which automatically sorts itself, without copy construction.


After your Edit: Your C seems relatively small and easy to copy, but it's borderline; Your best bet is to give both approaches (set and vector) and benchmark which one is faster.

Comments

0

If your type contains just a double I'd guess that it would be much faster not to use pointers! If the objects contain a std::string or a std::vector<T> (for some time T) the picture probably changes but compared to moving a structure with one or two basic objects, the cost of accessing more or less randomly distributed data is fairly high. Of course, to determine the specific situation you would need to profile both approaches.

2 Comments

It actually contains a double and two CPoints (which is basically a double and 4 integers)
This is in the area where I would start profiling: I would suspect that not using pointers is still faster but the objects get fairly large, i.e., it isn't clear which approach is faster.

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.