Create a class in CTRL with an attribute which type is another class

Find and share HowTos to various installations / configurations!
3 posts • Page 1 of 1
ange.ogawin
Posts:18
Joined: Thu Mar 14, 2019 10:51 am

Create a class in CTRL with an attribute which type is another class

Post by ange.ogawin »

Hi,
I want to create a class with an attribute which has as type another class.
For example,in libFile.ctl:

class A
{
public int att;
public A(int att){ this.att=att;}
};

class B
{
public A a;// line X
public B(A a){this.a=a;}
};

in initialize event of graphic object:
#uses "libFile"

main()
{
A a=new A(1);
B b=new (a);//here the error "Argument missing in function" and referencing the line X.
}

Any idea to solve this?
Thank;

ozangor
Posts:44
Joined: Thu Sep 22, 2011 2:57 pm

Re: Create a class in CTRL with an attribute which type is another class

Post by ozangor »

Not tested, but first thing i see is,

when creating stack objects don't use "new" keyword. Rather use:
A a = A(1);

the problem is in line
B b=new (a);
it should be:
B b = B(a);

Hope it works.

dvribeira
Posts:24
Joined: Mon Mar 18, 2019 11:53 am

Re: Create a class in CTRL with an attribute which type is another class

Post by dvribeira »

Hi

Assuming you want to store a copy of a in b, and not a reference, then the following code would work:

Code: Select all

class A {
    public int att;
    public A(int att = 0){ this.att=att;}
};

class B {
    public A a;
    public B(A a){this.a=a;}
};

main() {
    A a = A(1);
    B b = B(a);
    DebugN(a, b);
}
Output:

Code: Select all

WCCOActrl2:[instance 000002808588FF70 of class A
WCCOActrl2:"att" : 1][instance 000002808588FA70 of class B
WCCOActrl2:"a" : instance 000002808588FAB0 of class A
WCCOActrl2:	"att" : 1]
As you can see the first instance of A (the object named a) is different than the one stored within b, so changes to one won't affect the other. If you wanted to store the same, then have a look at shared pointers.

The two problems with your code would be then:
a) the usage of the "new" keyword, which should be used to fetch a shared pointer instead as pointed out by @ozangor
b) you should provide a default argument to A's constructor. WinCC OA will try to construct the B property of type A before its own constructor gets executed so if you don' provide a default argument, it will fail. If you would have used a shared pointer, then it would have been initialized as nullptr before construction.

Alternatively, to solve b), you could also initialize the property on declaration and avoid putting a default argumet in A's constructor:

Code: Select all

class A {
    public int att;
    public A(int att){ this.att=att;}
};

class B {
    public A a = A(0);
    public B(A a){this.a=a;}
};

main() {
    A a = A(1);
    B b = B(a);
    DebugN(a, b);
}
I hope it helps!
Daniel

EDIT: for what I see: a) is not a problem any more I guess since the new features of P013... so you can now use the new keyword both for objects and shared pointers. For the latter, that's the only allowed option.

3 posts • Page 1 of 1