www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - opCall in struct don't work

reply Heinz <malagana15 yahoo.es> writes:
Hi,

A lot of time without using structs and i'm having a bit of trouble now.

I have in my program a struct and a class:

module Control;

struct Point
{
    int x, y;
    
    Point opCall(int x, int y)
    {
        Point p;
        p.x = x;
        p.y = y;
        return p;
    }
}

class Control
{
    private Point mypoint;
    
    void Hello()
    {
        mypoint = Point(2, 3);
    }
}

This module won't compile. I get the following error:

gui\control.d(22): Error: this for opCall needs to be type Point not type
gui.control.Control
gui\control.d(22): class gui.control.Control member opCall is not accessible

What the hell is wrong with this? I'm using dmd v1.028 under Win32.
Thanks in advance.
Apr 29 2008
parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
You need to make the opCall static.

struct Point {

	// ...

	static Point opCall(int x, int y) {
		// ...
	}

}

Non-static opCalls can be used like this:

Point point;
point(10, 20); // treat point as a function

Heinz wrote:
 Hi,
 
 A lot of time without using structs and i'm having a bit of trouble now.
 
 I have in my program a struct and a class:
 
 module Control;
 
 struct Point
 {
     int x, y;
     
     Point opCall(int x, int y)
     {
         Point p;
         p.x = x;
         p.y = y;
         return p;
     }
 }
 
 class Control
 {
     private Point mypoint;
     
     void Hello()
     {
         mypoint = Point(2, 3);
     }
 }
 
 This module won't compile. I get the following error:
 
 gui\control.d(22): Error: this for opCall needs to be type Point not type
gui.control.Control
 gui\control.d(22): class gui.control.Control member opCall is not accessible
 
 What the hell is wrong with this? I'm using dmd v1.028 under Win32.
 Thanks in advance.
Apr 29 2008
parent Heinz <malagana15 yahoo.es> writes:
Ary Borenszweig Wrote:

 You need to make the opCall static.
 
 struct Point {
 
 	// ...
 
 	static Point opCall(int x, int y) {
 		// ...
 	}
 
 }
 
 Non-static opCalls can be used like this:
 
 Point point;
 point(10, 20); // treat point as a function
 
 Heinz wrote:
 Hi,
 
 A lot of time without using structs and i'm having a bit of trouble now.
 
 I have in my program a struct and a class:
 
 module Control;
 
 struct Point
 {
     int x, y;
     
     Point opCall(int x, int y)
     {
         Point p;
         p.x = x;
         p.y = y;
         return p;
     }
 }
 
 class Control
 {
     private Point mypoint;
     
     void Hello()
     {
         mypoint = Point(2, 3);
     }
 }
 
 This module won't compile. I get the following error:
 
 gui\control.d(22): Error: this for opCall needs to be type Point not type
gui.control.Control
 gui\control.d(22): class gui.control.Control member opCall is not accessible
 
 What the hell is wrong with this? I'm using dmd v1.028 under Win32.
 Thanks in advance.
Hahaha, it worked! thanks for your quick answer.
Apr 29 2008