A basic C++ question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • QQ

    A basic C++ question

    Hello, I am new on C++, I am reading one source code
    ZThread::ZThrea d(char *threadname):bu g("ZThread")
    ~~~~~~~~~~~~~~~ ~~~~What does it mean?

    Thanks a lot!

    {
    ........
    }

  • Rolf Magnus

    #2
    Re: A basic C++ question

    QQ wrote:
    [color=blue]
    > Hello, I am new on C++, I am reading one source code
    > ZThread::ZThrea d(char *threadname):bu g("ZThread")
    > ~~~~~~~~~~~~~~~ ~~~~What does it mean?[/color]

    Which C++ book are you reading that doesn't explain initializer lists? This
    is a very basic concept of C++.
    The above means that 'bug' is either the name of a base class of ZThread or
    the name of one of its data members. In the former case, the base part of
    your object is initialized using its constructor that takes a const char*.
    In the latter, the data member is initialized with such a constructor.
    Without the initializer, the default constructor would be used.

    Comment

    • Victor Bazarov

      #3
      Re: A basic C++ question

      QQ wrote:[color=blue]
      > Hello, I am new on C++, I am reading one source code
      > ZThread::ZThrea d(char *threadname):bu g("ZThread")
      > ~~~~~~~~~~~~~~~ ~~~~What does it mean?
      >
      > Thanks a lot!
      >
      > {
      > .......
      > }
      >[/color]

      It's called "initialise r list" ("initialize r list"). The member
      named 'bug' is initialised with a const char[] argument (a string
      literal).

      V

      Comment

      • eKIK

        #4
        Re: A basic C++ question

        It means you've got a method called ZThread in a class called ZThread
        (constructor) which take a char pointer as argument and sets the value
        of the variable bug to "ZThread".

        ZThread::ZThrea d(char* threadname)
        {
        bug = "ZThread";
        }

        This is just another way of writing the (nearly) exact same thing.

        Hope this helps!
        // eKIK

        Comment

        • John Dibling

          #5
          Re: A basic C++ question

          That looks like a constructor initialization list.

          </dib>

          Comment

          • Lionel B

            #6
            Re: A basic C++ question

            "eKIK" <trash@boreios. net> wrote in message news:1116364838 .696039.65520@g 49g2000cwa.goog legroups.com...[color=blue]
            > It means you've got a method called ZThread in a class called ZThread
            > (constructor) which take a char pointer as argument and sets the value
            > of the variable bug to "ZThread".[/color]

            not "sets the value"; *initialises* bug with ZThread
            [color=blue]
            > ZThread::ZThrea d(char* threadname)
            > {
            > bug = "ZThread";
            > }
            >
            > This is just another way of writing the (nearly) exact same thing.[/color]

            Terrible advice... not even "nearly":

            1) If bug is declared const it won't compile.

            2) If bug is an instance of a class with a constructor taking parameter char* it either won't compile (if bug's class
            lacks an assignment operator for char*) or otherwise may not do what you expect it to, since bug's char* constructor
            won't be called.

            --
            Lionel B


            Comment

            Working...