Polymorphism in OA

Find and share HowTos to various installations / configurations!
3 posts • Page 1 of 1
ebaggen
Posts:2
Joined: Fri Oct 05, 2018 5:29 pm

Polymorphism in OA

Post by ebaggen »

Hi!

Quick question about WinCC OA's implementation of polymorphism, particularly late binding.

Here's some example code:

Code: Select all

class SuperClass
{
  public void print()
  {
    DebugN("SuperClass");
  }
}

class SubClass : SuperClass
{
  public void print()
  {
    DebugN("SubClass");
  }
}

main()
{
  SuperClass super = new SuperClass();
  super.print();

  SubClass sub = new SubClass();
  sub.print();

  SuperClass superSub = new SubClass();
  superSub.print();

  shared_ptr superSubPtr = new SubClass();
  superSubPtr.print();
}
The output of this code is:

Code: Select all

SuperClass
SubClass
SuperClass
SubClass
I can very easily reference a parent object as a parent class (super), as well as reference a child object as a child class (sub). What I'm having trouble doing is referencing a child object as a parent class (superSub).

The OA Help article for Object Oriented Scripting states:
you can pass any derived class to a reference of a base class but the executed function will be used from the passed class (if it exists).
This definition is pretty well aligned with my understanding of late binding, so I'm confused why my third example is using the 'print' method from SuperClass instead of SubClass.

I did notice that I'm able to use a shared_ptr to invoke late binding (superSubPtr).


Could any experts out there help clear this up for me? Is late binding simply not supported in OA? Is a shared_ptr a safe alternative, assuming I appropriately manage memory where necessary?

Thanks!

Eric
Attachments
WinCC_OA_OOP.JPG

mkoller
Posts:741
Joined: Fri Sep 17, 2010 9:03 am

Re: Polymorphism in OA

Post by mkoller »

Looking at your code, I see the following problems:

line 16: this is wrong. "new" creates a shared_ptr pointing at a SuperClass instance, which will be assigned to the "super" variable and then be deleted again.
This is not what you want.

Never use "new" except the target is a shared_ptr!
When you want to create a local var of any type, simply define it as you would do with, say an "int", e.g.:

SuperClass super;

done.

The mentioned problem also explains some of your observations.
Line 22 does not what you might think it does.
In fact you create a local var of type SuperClass.
The assignment creates a SubClass, assigns its content to the superSub variable and deletes
the SubClass instance - and of course the assignment
does not change the datatype of the local var!
So the output of "SuperClass" is correct.

P.S.: In the future please do not post a code example as an image but as plain text -
makes my life much simpler to just copy/paste your code.

ebaggen
Posts:2
Joined: Fri Oct 05, 2018 5:29 pm

Re: Polymorphism in OA

Post by ebaggen »

Martin,

Thanks for your reply.

Coming from a C# and Java background, I forgot about properly handling the 'new' operator in C++ / CTRL++. After reviewing OOP and pointers for C++, your reply makes a lot more sense. That also explains why my shared_ptr was doing what I had expected 'SuperClass superSub = new SubClass()' would do.

Also, thank you for the tip about plain text for code entries. I've reformatted my initial post to reflect your suggestion.

3 posts • Page 1 of 1