C++ polymorphism

I moved to objective c++, and i had to learn polymorphism. I understand basic. Complete basics. But when i seen something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  void __fastcall TForm1::Zapiszjako1Click(TObject *Sender)
{
               if (SaveDialog1->Execute())
           {
             try
             {
             tresc->Lines->SaveToFile(SaveDialog1->FileName);
             nazwapliku = SaveDialog1->FileName;
              }
             catch  (...)
             {
               ShowMessage("Błąd przy zapisaniu pliku!");
             }
 }
}

I was like "sh*t"...
I asked on other forum i told them that iwant to develop games/desktop applications, and they told me to move to c#. What should i do? I want to stay at C++ anyway... :/ I would appreciate if you would help me understanding this... :/
Last edited on
void __fastcall TForm1::Zapiszjako1Click(TObject *Sender)
This is a class function of the class TForm1. It accepts one parameter. That parameter is of type pointer-to-TObject.

__fastcall is a specific instruction to the compiler, only recognised by the MS compiler. It's not standard C++. Don't worry about it.

1
2
{
               if (SaveDialog1->Execute())

Execute the function SaveDialog1->Execute(). If the return value is true (or something that converts to true), then run the contents of the following block.

1
2
  {
             try

This code might throw an exception. If it does, there's a catch at the end to catch the exception.


1
2
3
4
             {
             tresc->Lines->SaveToFile(SaveDialog1->FileName);
             nazwapliku = SaveDialog1->FileName;
              }

Execute a couple of functions. Pretty standard.

1
2
3
4
     catch  (...)
             {
               ShowMessage("Błąd przy zapisaniu pliku!");
             }

If those functions threw an exception, catch it here and show a message.

1
2
 }
}

All done.

All pretty simple so long as you simply look at it line by line.
I see no polymorphism here.

The function takes an argument, but never uses it.

Three variables are mentioned: SaveDialog1, tresc, and nazwapliku.
They are either global, or members of class TForm1, or inherited by class TForm1.

The SaveDialog1 is apparently a pointer to object that has member function Execute() and member variable FileName.

The tresc is apparently a pointer to object that has public member pointer to object that has member function SaveToFile(). That function might indicate an error by throwing an exception.


You must read the definition of class TForm1 to find out the actual types, and then study the definition of those types.
__fastcall is a specific instruction to the compiler, only recognised by the MS compile

Actually the code is C++Builder.

@OP,
it might look a bit scary at the beginning but C++Builder is a good choice for GUI apps.
It also good for simple games.
Actaully i have problem with this line
tresc->Lines->SaveToFile(SaveDialog1->FileName);
Thanks for explaining.
Not sure what tresc is. Lines is a kind of TStringList and SaveToFile does what it's name suggest.
Would be easier to help you if you use English names for your variables.
We do not see the relevant code, so we do not really know.

Nonsense example:
1
2
3
4
5
6
7
8
9
10
11
12
13
struct Bar {
  void SaveToFile( int );
};

struct Foo {
  Bar* Lines;
}

int main() {
  Foo* tresc;

  tresc->Lines->SaveToFile( 42 ); // syntactically correct
}
tresc is memo (c++ builder 6), but i have problem with "->" i was told it's from polymorphism, and i just don't understand it... ;/
if there's any error in the code, sorry i was changing it at this moment...
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Notepad.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
AnsiString filename="";
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------







void __fastcall TForm1::Open1Click(TObject *Sender)
{
           if (OpenDialog1->Execute())
           {
             try
             {
             tresc->Lines->LoadFromFile(OpenDialog1->FileName);
              filename = OpenDialog1->FileName;
              }
             catch  (...)
             {
               ShowMessage("Can't open file, make sure it exists!");
             }
 }
 }
//---------------------------------------------------------------------------
void __fastcall TForm1::Saveas1Click(TObject *Sender)
{
               if (SaveDialog1->Execute())
           {
             try
             {
             tresc->Lines->SaveToFile(SaveDialog1->FileName);
             filename = SaveDialog1->filename;
              }
             catch  (...)
             {
               ShowMessage("Can't save file!");
             }
 }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Saveas1Click(TObject *Sender)
{
    if (filename!="")
    {
        tresc->Lines->SaveToFile(filename);
    }
     else
     {
         Form1->Saveas1Click(MainMenu1);
     }
}
//--------------------------------------------------------------------------- 
a->b

is equivalent to:

(*a).b

In other words, it accesses the member b of the object that you get by dereferencing the pointer a.

It doesn't necessarily have anything to do with polymorphism, although that's one reason you might use it.
tresc is memo (c++ builder 6), but i have problem with "->" i was told it's from polymorphism

Lines is defined as TStrings which is an abstract class, which can't be instantiated. TCustomMemo or TMemo creates a TStringList which inherits from TStrings, so you have polymorphism.
http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/StdCtrls_TMemo_Lines.html

BTW Is there a reason why you use this very old version ?
"It's good for practicing", and i don't want to spend money on something better right now.

Topic archived. No new replies allowed.