A basic C++ question

Q

QQ

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!

{
........
}
 
V

Victor Bazarov

QQ said:
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
 
R

Rolf Magnus

QQ said:
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.
 
E

eKIK

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
 
L

Lionel B

eKIK said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top