A basic C++ question

Discussion in 'C++' started by QQ, May 17, 2005.

  1. QQ

    QQ Guest

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

    Thanks a lot!

    {
    ........
    }
     
    QQ, May 17, 2005
    #1
    1. Advertising

  2. QQ wrote:
    > Hello, I am new on C++, I am reading one source code
    > ZThread::ZThread(char *threadname):bug("ZThread")
    > ~~~~~~~~~~~~~~~~~~~What does it mean?
    >
    > Thanks a lot!
    >
    > {
    > .......
    > }
    >


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

    V
     
    Victor Bazarov, May 17, 2005
    #2
    1. Advertising

  3. QQ

    Rolf Magnus Guest

    QQ wrote:

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


    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.
     
    Rolf Magnus, May 17, 2005
    #3
  4. QQ

    eKIK Guest

    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::ZThread(char* threadname)
    {
    bug = "ZThread";
    }

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

    Hope this helps!
    // eKIK
     
    eKIK, May 17, 2005
    #4
  5. QQ

    John Dibling Guest

    That looks like a constructor initialization list.

    </dib>
     
    John Dibling, May 17, 2005
    #5
  6. QQ

    Lionel B Guest

    "eKIK" <> wrote in message news:...
    > 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".


    not "sets the value"; *initialises* bug with ZThread

    > ZThread::ZThread(char* threadname)
    > {
    > bug = "ZThread";
    > }
    >
    > This is just another way of writing the (nearly) exact same thing.


    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
     
    Lionel B, May 18, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Duenser

    Basic shifting question

    Stefan Duenser, Dec 7, 2004, in forum: VHDL
    Replies:
    4
    Views:
    447
    Stefan Duenser
    Dec 8, 2004
  2. Replies:
    1
    Views:
    588
    Eric Smith
    Jul 8, 2005
  3. Engineer
    Replies:
    6
    Views:
    643
    Jeremy Bowers
    May 1, 2005
  4. Replies:
    0
    Views:
    452
  5. Ark Khasin

    A basic question question about volatile use

    Ark Khasin, Jul 31, 2008, in forum: C Programming
    Replies:
    12
    Views:
    363
    Ark Khasin
    Jul 31, 2008
Loading...

Share This Page