CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2017
    Posts
    2

    question of structured data

    I can understand the first row and last two rows.
    However, does anyone know what the remaining part means ????(line3-line16)
    especially for line3-line5


    1 struct Bracket
    2 {
    3 Bracket(char type, int position):
    4 type(type),
    5 position(position)
    6 {}

    7 bool Matchc(char c)
    8 {
    9 if (type == '[' && c == ']')
    10 return true;
    11 if (type == '{' && c == '}')
    12 return true;
    13 if (type == '(' && c == ')')
    14 return true;
    15 return false;
    16 }

    17 char type;
    18 int position;
    19 };

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: question of structured data

    line 3 defines the struct constructor (think of class - struct is class with all public by default).
    line 4, 5 initialises the struct variable type to the value of the parameter type. Same with position.
    lines 7 - 16 is a struct member function Matchc() which uses the value of the member variable type as part of its condition tests. Note that Matchc() can be simplifed to
    Code:
    bool Matchc(char c)
    {
        return (type== '[' && c == ']') || (type == '{' && c == '}') || (type == '(' && c == ')');
    }
    Last edited by 2kaud; June 21st, 2017 at 04:56 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jun 2017
    Posts
    2

    Re: question of structured data

    Thank you.
    I think I need to learn something about constructor. I didn't know that before.

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: question of structured data

    constructors are part of c++ OOP and are usually used with class (although can be used with struct). See http://www.learncpp.com/cpp-tutorial...d-programming/

    In c++ OOP, there is really no difference between a class and a struct. The only difference is that for struct all members are default public whilst with class all members are private by default.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Feb 2017
    Posts
    677

    Re: question of structured data

    Quote Originally Posted by 2kaud View Post
    In c++ OOP, there is really no difference between a class and a struct. The only difference is that for struct all members are default public whilst with class all members are private by default.
    There's a second difference that's of importance in OOP: Classes have private inheritance by default while structs have public.
    Last edited by wolle; June 21st, 2017 at 10:48 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured