1

I'm very new to c++, please help me to figure out how these operators work

class MyCalss : public State // MyClass inherits State?
{
private
MyClass(){} // Constructor for MyClass?
MyClass(const MyClass&);  // const means that invoking object will be not changed? What the meaning of '&' symbol? MyClass&

MyClass& operator = (const MyClass&) // What this statement exactly do? is it some kind operation overloading?

}

For example I have constructor

MyClass2::MyClass2(int i):BaseEntity(id),
m_iTotal(5),
m_iMonye(5),
m_iLevel(10){}

What is :BaseEntity(id) here means?

and can I rewrite it like this?

MyClass2::MyClass2(int i):BaseEntity(id)
{
m_iTotal = 5;
m_iMonye = 5;
m_iLevel = 10;
}
Yesman
  • 437
  • 1
  • 4
  • 5
  • 3
    Please read a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) before asking such basic question. I would recommend C++ Primer. :) – Prasoon Saurav Sep 21 '10 at 16:05
  • 4
    No offense, but you really need to get a book on C++. You are not going to be able to effectively learn an entire programming language, especially one as complex as C++, by trial and error coupled with asking questions on SO. – Tyler McHenry Sep 21 '10 at 16:05
  • @Tyler: Heh ... thats how I learned C. Mind thats without stackoverflow, as the internet barely existed back in those days ;) – Goz Sep 21 '10 at 16:21
  • @Goz Well, I said "effectively". Of course you *can* learn just about anything by trial and error, but it will take you a whole lot longer, and you'll likely miss out on a number of important subtleties. The main issue is that people who try to learn C++ by trial-and-error-and-SO generate innumerable questions that are all answered in the first twenty-five pages of any decent text. – Tyler McHenry Sep 21 '10 at 16:24
  • Also, I'd prefer to have more individual questions and fewer shotgun questions on the site. – David Thornley Sep 21 '10 at 16:31
  • @Tyler: I don't disagree with ya at all. Afterall I figured these things out without online resources and hassling people. I was just being facetious really :D As an aside: I did use a book to teach myself C++ but, in the end, it only proved useful for learning wtf things like dynamic binding were. The rest was pretty easy to pick up. Mind i already knew Pascal before learning either of these languages :) – Goz Sep 21 '10 at 16:36

3 Answers3

2

1) Yes MyClass inherits State. The public means that all its public functions will still be public in the derived class.
2) Yes thats a constructor.
3) Const DOES mean the object won't be changed. The "&" means you are passing in a reference to the object. This means you don't copy the whole object across the stack. It simply says this is where the object it is (ie refers to it). This saves some stack space and copying time.
4) operator = is an operator overload. It means that you can do the following:

MyClass a;
MyClass b;

// Populate and b.
a = b; // Overwrite a with b.

5) The BaseEntity( id ) means that the constructor calls the base class, BaseEntity's, constructor. id in this case is presumably a global or static class member otherwise the code won't compile.
6) You "could" re-write it like that but generally its better to do the former.

Goz
  • 61,365
  • 24
  • 124
  • 204
0

BaseEntity is just another of the list of initializers. You could indeed rewrite the code as suggested and achieve nearly the same result. Please see:

http://www.cprogramming.com/tutorial/initialization-lists-c++.html

0

Ok, taking your questions one at a time:

class MyClass : public State // MyClass inherits State?

exactly so. MyClass inherits (publicly, which is usually the only kind of inheritance you care about) from State. Literally, MyClass "is a" State - everything true about State is also True about MyClass.

private
MyClass(){} // Constructor for MyClass?

Yes, MyClass(){} is a constructor for MyClass, specifically in this case taking no arguments and doing nothing (nothing special - memory will still be allocated, etc). However, because you've made MyClass() private, nothing else can call it, meaning you can't ever create a MyClass object (with no parameters) except from within MyClass methods.

MyClass(const MyClass&);  // const means that invoking object will be not changed? What the meaning of '&' symbol? MyClass&

Yes, const means that the object passed as a parameter won't be changed. This is, in fact, the Copy Constructor - the special constructor used to create a MyClass as a clone of an existing object. Again, it's private, so most of the world can't call it. The & indicates passing the parameter by reference - this prevents the compiler having to copy the object, and is often a good thing.

MyClass& operator = (const MyClass&) // What this statement exactly do? is it some kind operation overloading?

This is the copy-assignment operator, used to allow statements like `instance_a = instance_b;".

Note that, unless you define them yourself, the compiler will generate default versions of all of these three for you.

MyClass2::MyClass2(int i):BaseEntity(id),
m_iTotal(5),
m_iMonye(5),
m_iLevel(10){}

BaseEntity(id) is very similar to the other three lines; it's telling the compiler that when MyClass2 is constructed, it should call the parent class BaseEntity's constructor with the parameter id. Your second form of this constructor is very similar to your first form, but the first one - using an initialisation list - is considered better form.

Chowlett
  • 45,935
  • 20
  • 116
  • 150
  • in my case MyClass is singleton, thats why constructor is private – Yesman Sep 21 '10 at 18:25
  • I thought it might be, but that seemed beyond the scope of this question (given how long my answer was anyway!) Glad to be of help. – Chowlett Sep 22 '10 at 07:48