Could someone explain some code?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • E. Robert Tisdale

    #16
    Re: Could someone explain some code?

    Gactimus wrote:
    [color=blue]
    > Can anyone explain what the lines with the '*' by them do?
    >
    > -----------
    >
    > #ifndef GUARD_COUNTER_H
    > #define GUARD_COUNTER_H 1
    > #include <iostream>
    > // using namespace std;
    >
    > class Counter {
    > private:
    > int count;
    > public:
    > Counter(void): count(0) { } // default constructor
    > Counter& operator++(void ) { ++count; return *this; }
    > int GetCount(void) { return count; }
    > Counter& SetCount(int c) { count = c;[/color]
    return *this; }
    friend
    std::ostream& operator<<(std: :ostream& os, const Counter& c) {
    return os << c.count;
    }[color=blue]
    > // void PrintCount() { cout << "\n The count is " << count; }
    >
    > };
    >
    > class NewCounter: public Counter {
    > public:
    > NewCounter& operator--(void) { --count; return *this; }
    > };
    >
    > #endif//GUARD_COUNTER_H[/color]

    Comment

    • Gactimus

      #17
      Re: Could someone explain some code?

      Gactimus <gactimus@xrs.n et> wrote in
      news:1102605878 .0eb1675083e856 48bb700b5e41f13 861@bubbanews:
      [color=blue]
      > Can anyone explain what the lines with the '*' by them do?
      >
      > -----------
      >
      > #ifndef _COUNTER_H
      > #define _COUNTER_H
      > #include <iostream>
      > using namespace std;
      >
      > class Counter
      > {
      > public:
      > int count;[/color]




      [color=blue]
      > public:
      > * Counter(){count = 0;}
      > void operator ++() { ++count; }
      > int GetCount() { return count; }
      > void SetCount( int c ) { count = c; }
      > void PrintCount() { cout << "\n The count is " << count; }[/color]

      If this section was protected, how would one access it using a friend?

      [color=blue]
      > };
      >
      > class NewCounter:publ ic Counter
      > {
      > public:
      > * void operator --() { --count; }
      > };
      >
      > #endif[/color]

      Comment

      • David White

        #18
        Re: Could someone explain some code?

        "Gactimus" <gactimus@xrs.n et> wrote in message
        news:1102623238 .bb85af6c6bc249 3985259870e2129 90a@bubbanews.. .[color=blue]
        > Victor Bazarov <v.Abazarov@com Acast.net> wrote in
        > news:3r2ud.1206 2$Ae.5897@newsr ead1.dllstx09.u s.to.verio.net:[color=green]
        > > It's called "the body of the function".[/color]
        >
        > So is it just a more efficient way of writing a function instead of
        > writing a prototype and a seperate body?[/color]

        More efficient to write anyway, and perhaps to read, though maybe not if
        there's too much of it. Also, a function whose body is in the class
        definition is also inline, i.e., the same as putting the body outside, but
        with the 'inline' specification.

        DW



        Comment

        • Francis Glassborow

          #19
          Re: Could someone explain some code?

          In article <I8H56z.9K@news .boeing.com>, Default User
          <first.last@boe ing.com.invalid > writes[color=blue]
          >Frank Looper wrote:
          >[color=green][color=darkred]
          >> > > #include <iostream>
          >> > > using namespace std;
          >> >
          >> > This is a very bad idea. Try to avoid ever putting 'using'
          >> > directives in a header.
          >> >[/color]
          >> Isn't that the standard? It's at least being taught as such...[/color]
          >
          >
          >No, because then everyone who includes your header gets all of
          >namespace std thrust into the global namespace, which might not be what
          >they want.[/color]

          But, unless I have completely lost track, we were not discussing a
          header (I have never come across a header with main() in it.
          [color=blue]
          >
          >Such using declarations should only be in implementation files. Header
          >should just explicitly scope items from std.[/color]

          Much better is to actually understand what using declarations and using
          directives do.

          --
          Francis Glassborow ACCU
          Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
          For project ideas and contributions: http://www.spellen.org/youcandoit/projects

          Comment

          • Victor Bazarov

            #20
            Re: Could someone explain some code?

            Francis Glassborow wrote:[color=blue]
            > In article <I8H56z.9K@news .boeing.com>, Default User
            > <first.last@boe ing.com.invalid > writes
            >[color=green]
            >> Frank Looper wrote:
            >>[color=darkred]
            >>> > > #include <iostream>
            >>> > > using namespace std;
            >>> >
            >>> > This is a very bad idea. Try to avoid ever putting 'using'
            >>> > directives in a header.
            >>> >
            >>> Isn't that the standard? It's at least being taught as such...[/color]
            >>
            >>
            >>
            >> No, because then everyone who includes your header gets all of
            >> namespace std thrust into the global namespace, which might not be what
            >> they want.[/color]
            >
            >
            > But, unless I have completely lost track, we were not discussing a
            > header (I have never come across a header with main() in it.[/color]

            You have completely lost track, then. See the post that started the
            thread. The two lines quoted _were_ in a header. And I don't see any
            'main' in it either.

            V

            Comment

            • Default User

              #21
              Re: Could someone explain some code?

              Francis Glassborow wrote:
              [color=blue]
              > In article <I8H56z.9K@news .boeing.com>, Default User
              > <first.last@boe ing.com.invalid > writes[color=green]
              > > Frank Looper wrote:
              > >[color=darkred]
              > >> > > #include <iostream>
              > >> > > using namespace std;
              > >> >
              > >> > This is a very bad idea. Try to avoid ever putting 'using'
              > >> > directives in a header.
              > >> >
              > >> Isn't that the standard? It's at least being taught as such...[/color]
              > >
              > >
              > > No, because then everyone who includes your header gets all of
              > > namespace std thrust into the global namespace, which might not be
              > > what they want.[/color]
              >
              > But, unless I have completely lost track, we were not discussing a
              > header (I have never come across a header with main() in it.[/color]

              Nope. Here's the original code under discussion:

              -----------

              #ifndef _COUNTER_H
              #define _COUNTER_H
              #include <iostream>
              using namespace std;

              class Counter
              {
              public:
              int count;
              public:
              * Counter(){count = 0;}
              void operator ++() { ++count; }
              int GetCount() { return count; }
              void SetCount( int c ) { count = c; }
              void PrintCount() { cout << "\n The count is " << count; }

              };

              class NewCounter:publ ic Counter
              {
              public:
              * void operator --() { --count; }
              };

              #endif

              [color=blue][color=green]
              > >
              > > Such using declarations should only be in implementation files.
              > > Header should just explicitly scope items from std.[/color]
              >
              > Much better is to actually understand what using declarations and
              > using directives do.[/color]

              Indeed. Feel up to posting a tutorial? I didn't. The people in question
              should find a good book that explains the subject. Now, if we only knew
              of some web site or something that reviewed books ;)




              Brian

              Comment

              • jeffc

                #22
                Re: Could someone explain some code?


                "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message
                news:Gi_td.1205 0$Ae.8510@newsr ead1.dllstx09.u s.to.verio.net. ..[color=blue]
                > Gactimus wrote:[color=green]
                > > Can anyone explain what the lines with the '*' by them do?
                > >
                > > -----------
                > >
                > > #ifndef _COUNTER_H
                > > #define _COUNTER_H[/color]
                >
                > Lose the leading underscore on that macro name.
                >[color=green]
                > > #include <iostream>
                > > using namespace std;[/color]
                >
                > This is a very bad idea. Try to avoid ever putting 'using' directives
                > in a header.
                >[color=green]
                > >
                > > class Counter
                > > {
                > > public:
                > > int count;
                > > public:
                > > * Counter(){count = 0;}[/color]
                >
                > This one declares and defines the default constructor for this class.[/color]

                I thought the * was actually part of the code, and I could not figure out what
                it was supposed to do :-)


                Comment

                Working...