0

I am a beginner in C++ and am trying to learn it by looking at examples. Here is an example definition for a class that I don't understand the meaning completely:

class MyClass{
public:
  std::string name;
  void *data;

  MyClass(const std::string& t_name, void *t_data) : name(t_name), data(t_data) {}
};

Here is what I understand: name and *data are variables of the class and, MyClass(...) is the constructor. The meaning of : is the left side class is derived from the right hand side class. However, what is the meaning of this part of the code:

MyClass(const std::string& t_name, void *t_data) : name(t_name), data(t_data) {}

Here are the questions:

  1. what are "t_data" and "t_name"? Are they initial values for "data" and "name"? what is the reason t_ is used here?
  2. what is the meaning of : in the above line?
  3. what is {} at the end of that line?

Thanks for the help. TJ

TJ1
  • 7,578
  • 19
  • 76
  • 119
  • Apart from the second question, the same questions can be asked about an ordinary function, if that helps at all. – chris Jul 12 '12 at 17:50
  • Just to give you an idea of `t_`, the prefix `m_` is sometimes used to signify members of a class. I'm not sure what `t_` would stand for here, though. – chris Jul 12 '12 at 17:56
  • One thing is that you can request to translate this code to your favorite programming language – Pooya Jul 12 '12 at 18:12

5 Answers5

5

what are "t_data" and "t_name"? Are they initial values for "data" and "name"?

They are the arguments passed to the constructor. If an object were created as

MyClass thing("Fred", some_pointer);

then, in the constructor, t_name has the value "Fred" and t_data has the value of some_pointer.

what is the reason t_ is used here?

Some people like to tag the arguments to give them different names to the class members, but there's no need to do that unless you want to.

what is the meaning of : in the above line?

That marks the start of the initialiser list, which initialises the class member variables. The following initialisers, name(t_name), data(t_data) initialise those members with the constructor's arguments.

what is {} at the end of that line?

That's the constructor's body, like a function body. Any code in their will be run after the members have been initialised. In this case, there's nothing else to do, so the body is empty.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
4

It is good C++ practice to use initializer lists to initialize members.

  1. t_name, t_data are the parameter names.
    • The "t_" prefix is merely used to distinguish it from the similarly named member fields.
    • The members are being initialized using syntax that resembles a function call, but it is a proper initialization/construction, so you should think of it that way.
  2. The colon signals the beginning of an initializer list
  3. The empty braces {} designate that the body of the constructor (which happens after the members are initialized) is empty.
Prashant Kumar
  • 20,069
  • 14
  • 47
  • 63
  • Thanks for the answer it is a great answer however I only could accept one answer so I just voted you up. – TJ1 Jul 12 '12 at 18:08
2

MyClass(const std::string& t_name, void *t_data) : name(t_name), data(t_data) {}

is constructor of your class, and you should initialize your class with a string and void* parameter, then you initialize your class fields (name and data) with your passed parameters

Pooya
  • 4,385
  • 6
  • 45
  • 73
0

This is a somewhat compressed example for a beginner, but you're asking good questions.

1) t_data and t_name are the parameters going into the constructor. When you create an instance of this class, you'll do something like:

MyClass * myNewClassInstance = new MyClass("My name", &myData);

Now the constructor has "My name" (in std::string form) as t_name and a pointer to myData as t_data.

2) The colon here does not refer to a derived class. Here it says that an "initializer list" is to follow. Check out Prashant's answer's link, or do a little digging about this. Basically, this is setting the member variable name to t_name (which is the std::string that we passed into the constructor in the above example), and data to t_data (the void pointer). (see additional notes below)

3) The {} is the actual definition of the constructor. It's empty -- there's no code here. So all the constructor will do is initialize the member variables (as defined by the initializer list), and nothing more.

Now let's look at the exact same class in a more beginner-friendly format:

// The declaration of MyClass.  This would often show up in a header (.h) file
class MyClass {
public:
  // Member variables
  std::string name;
  void * data;

  // Declaration of constructor -- we haven't defined what actually does here, just
  // what parameters it takes in.
  MyClass(const std::string& t_name, void * t_data);
}

// We declared what the class "looks like" above, but we still have to define what
// the constructor does.  This would usually show up in a .cpp file

// This is the constructor definition.  We have to specify that it's part of the
// The extra "MyClass::" specifies that it's part of the MyClass namespace that we
// declared above.
MyClass::MyClass(const std::string& t_name, void * t_data)
{
  // Set our member variables to what was passed in.
  this.name = t_name;
  this.data = t_data;
}

The above version does exactly the same thing (with some subtle differences between initializer lists and traditional initialization in the constructor that you probably don't care about yet -- again, see other references regarding this if you're curious).

It's pretty obvious that this takes up a lot more space, and that's exactly why the more compressed format is often used. But when you're just starting out, it makes things a lot more clear doing it this way. It's important to understand the difference between a declaration and a definition when it comes to functions (i.e. the constructor), because the example you posted does both at the same time when often they are separated.

gkimsey
  • 517
  • 6
  • 13
0

I am a beginner in C++ and am trying to learn it by looking at examples.

For whatever reason, this is harder to do in C++ than it is in Perl, Python, Java, or even C. I don't recommend this course; instead, you might invest in a good book.

Here is what I understand:

Let's start with that. I don't think you understand as much as you claim to.

name and *data are variables of the class

No. name and data are variables of the class. Their types are std::string and void*, respectively.

MyClass(...) is the constructor.

Yes.

The meaning of : is the left side class is derived from the right hand side class.

No, : means different things in different contexts. In the context in which it is used in your sample, it indicates that an initializer list follows.

However, what is the meaning of this part of the code:

what are "t_data" and "t_name"?

They are local variables in the constructor function. Specifically, they are the passed-in parameters to the function.

Are they initial values for "data" and "name"?

That is how they are being used in this sample. Specifically, they are being passed to the data and name initializers in the initializer list.

what is the reason t_ is used here?

This is purely the convention of this particular author. I've never seen that convention before.

what is the meaning of : in the above line?

It introduces an initializer list. This construct is only used in constructor member functions. Each named member is initialized by the values listed. (Note: the values do not need to come from parameters -- they can be any legal expression, so: MyClass(int ii) : i(ii), j(cos(0) / 2), k(&global_variable) {} might be a legitmate constructor. )

what is {} at the end of that line?

The body of the constructor member function. Since all of the work of the constructor is being done in the initializer list, the body of the function is empty.

Community
  • 1
  • 1
Robᵩ
  • 163,533
  • 20
  • 239
  • 308