Class owner

Hello,
I have a problem with coding something on C++. It is a bit hard for me to explain the problem, using the technical language (simply because I am unsure what terms to use), so I will desicribe a task, wich leads to the problem.

Lets assume, that we have some curve y=k*x+b, wich is given by k and b. Lets store each curve we have a copy of a class. Then we have another class - a point, given by it's cordinates x,y. Each point must belong to some curve, this info is needed for some operations I am performing on points, belonging to the same curve (because these operations require knowledge of k and b from curve equation).
So basicly I need a way to easily determine owner of a copy of class point.

The first solution wich comes up is simply to pass curve info (eg. pointer) to constructor of class point, but this is inconvinient. I suppose there should an easy way to accomplish the task I need, but I simply have no idea where to look.
Hope I described the problem good enough for others to understand.

Alexey.

P.S: the actuall problem is point addition and substraction on elliptic curves ( elliptic curve cryptography), where I want to have class curve and class point, and when adding points I need to know some parameters of the curve equation.

Edit:
Right now I have something like this:
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
class curve
{
 int k,b;
};

class point
{
point(int xx, int yy, curve *c = 0)
{
  x=xx;
  y=yy;
  owner=c;
}

int x,y;
curve *owner;
};

int main(void)
{
curve C;
point p(1,1,&C);
point q(0,0,&C);

return 0;
}


And passing address of curve each time is inconvinient while coding, a much more better way would be to implent something like this:

1
2
3
4
5
6
7
int main(void)
{
curve C;
C::point p(1,1), q(0,0);

return 0;
}


But I am even unsure if this is possible or where to read about language abilities, allowing to organise such class structure.
Last edited on
What about this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class point;

class curve
{
  // ...

  point getPoint(int xx, int yy) {
    return point(xx, yy, this);
  }
};

// ...

int main()
{
  curve C;
  point p = C.getPoint(1,1);
  point q = C.getPoint(0,0);
}

Last edited on
Actually, my suggestion uses more complex code than you had in the first place.

You might also consider using a reference instead of a pointer. Generally, if you'll never change what the pointer points to, and it will never point to null, it can be more convenient to use a reference instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class point
{
  point(int xx, int yy, curve &c) : owner(c)
  {
    x=xx;
    y=yy;
  }
  int x,y;
  curve &owner;
};

int main(void)
{
  curve C;
  point p(1,1,C);
  point q(0,0,C);
}
Last edited on
Topic archived. No new replies allowed.